【问题标题】:How to join ElasticSearch query with multi_match, boosting, wildcard and filter?如何使用 multi_match、boosting、通配符和过滤器加入 ElasticSearch 查询?
【发布时间】:2019-05-12 13:18:40
【问题描述】:

我正在努力实现这个目标:

  1. 通过布尔查询过滤掉结果,例如“status=1”
  2. 通过布尔范围查询过滤掉结果,例如“discance: gte 10 AND lte 60”
  3. 通过匹配 int 数组中的至少一个 int 值过滤掉结果
  4. 通过计算文档分数在多个字段中搜索单词。有些字段需要通配符,有些需要提升,例如 importantfield^2, somefield*, someotherfield^0.75
  5. 以上所有点均由 AND 运算符连接。用 OR 运算符将所有术语单点连接。

现在我写了这样的东西,但是通配符不起作用。搜索“abc”在“名称”字段中找不到“abcd”。 如何解决?

{
  "filtered": {
    "query": {
      "multi_match": {
        "query": "John Doe",
        "fields": [
          "*name*^1.75",
          "someObject.name",
          "tagsArray",
          "*description*",
          "ownerName"
        ]
      }
    },
    "filter": {
      "bool": {
        "must": [
          {
            "term": {
              "status": 2
            }
          },
          {
            "bool": {
              "should": [
                {
                  "term": {
                    "someIntsArray": 1
                  }
                },
                {
                  "term": {
                    "someIntsArray": 5
                  }
                }
              ]
            }
          },
          {
            "range": {
              "distanceA": {
                "lte": 100
              }
            }
          },
          {
            "range": {
              "distanceB": {
                "gte": 50,
                "lte": 100
              }
            }
          }
        ]
      }
    }
  }
}

映射:

{
  "documentId": {
    "type": "integer"
  },
  "ownerName": {
    "type": "string",
    "index": "not_analyzed"
  },
  "description": {
    "type": "string"
  },
  "status": {
    "type": "byte"
  },
  "distanceA": {
    "type": "short"
  },
  "createdAt": {
    "type": "date",
    "format": "yyyy-MM-dd HH:mm:ss"
  },
  "distanceB": {
    "type": "short"
  },
  "someObject": {
    "properties": {
      "someObject_id": {
        "type": "integer"
      },
      "name": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
  },
  "someIntsArray": {
    "type": "integer"
  },
  "tags": {
    "type": "string",
    "index": "not_analyzed"
  }
}

【问题讨论】:

  • 请加入您的映射。
  • 对不起,我忘了。

标签: elasticsearch


【解决方案1】:

如果您想为多个字段应用通配符并同时为各个字段应用各种boosting值,您可以使用Query String:

以下是您的查询方式:

POST <your_index_name>/_search
{  
   "query":{  
      "bool":{  
         "must":[  
            {  
               "query_string":{  
                  "query":"abc*",
                  "fields":[  
                     "*name*^1.75",
                     "someObject.name",
                     "tagsArray",
                     "*description*",
                     "ownerName"
                  ]
               }
            }
         ],
         "filter":{  
            "bool":{  
               "must":[  
                  {  
                     "term":{  
                        "status":"2"
                     }
                  },
                  {  
                     "bool":{  
                        "minimum_should_match":1,
                        "should":[  
                           {  
                              "term":{  
                                 "someIntsArray":1
                              }
                           },
                           {  
                              "term":{  
                                 "someIntsArray":5
                              }
                           }
                        ]
                     }
                  },
                  {  
                     "range":{  
                        "distanceA":{  
                           "lte":100
                        }
                     }
                  },
                  {  
                     "range":{  
                        "distanceB":{  
                           "gte": 50,
                           "lte":100
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

请注意,对于someIntsArray 字段,我使用了"minimum_should_match":1,这样您就不会得到没有这些值的文档。

更新答案:

通过更新的评论,您可以使用 query_string 使用的带有通配符搜索的字段,并且您可以使用 simple match query 和 boosting,如下所示。在组合 should 子句中包含这两个查询(甚至可以根据您的要求添加更多匹配查询)。这样,您可以控制可以使用通配符查询的位置以及不可以使用的位置。

{  
   "query":{  
      "bool":{  
         "should":[  
            {  
               "query_string":{  
                  "query":"joh*",
                  "fields":[  
                     "name^2"
                  ]
               }
            },
            {  
               "match":{  
                  "description":{  
                     "query":"john",
                     "boost":15
                  }
               }
            }
         ],
         "filter":{  
            "bool":{  
               "must":[  
                  {  
                     "term":{  
                        "status":"2"
                     }
                  },
                  {  
                     "bool":{  
                        "minimum_should_match":1,
                        "should":[  
                           {  
                              "term":{  
                                 "someIntsArray":1
                              }
                           },
                           {  
                              "term":{  
                                 "someIntsArray":5
                              }
                           }
                        ]
                     }
                  },
                  {  
                     "range":{  
                        "distanceA":{  
                           "lte":100
                        }
                     }
                  },
                  {  
                     "range":{  
                        "distanceB":{  
                           "lte":100
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

如果这有帮助,请告诉我

【讨论】:

  • 通配符有效,但现在我无法控制将使用哪些字段通配符 - 现在它在所有字段中。还过滤“范围”而不是过滤结果。我的测试查询pastebin.com/0iLh2R4H
  • @TomaszKane 我更新了我的答案并在Updated Answer 部分添加了新查询。请你检查一下。您可以修改答案并进行相应的测试。请注意您在搜索查询中使用的字段名称和值以及您在文档/映射中提供的内容。过滤器查询在我的最后工作正常。
猜你喜欢
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多