【问题标题】:multi word query in elasticsearch with filter带有过滤器的弹性搜索中的多词查询
【发布时间】:2017-08-07 16:02:10
【问题描述】:

我将一个数据集放入 ES 中,包含以下字段:

Category,Question,Answer

尝试在问题字段或答案字段中查询我的弹性搜索索引以查找短语“世界系列”,但它也必须仅将其结果/搜索限制为我指定的类别

{
   "query" : {
      "constant_score" : { 
         "filter" : {
            "bool" : {
              "should" : [
                 { "term" : {"Question" : "world series"}},
                 { "term" : {"Answer" : "world series"}} 
              ],
              "must" : {
                 "term" : {"Category" : "Sport"} 
              }
           }
         }
      }
   }
}'

但是,当我这样做时,它不允许我使用多个单词(即“世界系列”)。

如何搜索短语/术语,但只搜索我想要的类别(即运动)?

【问题讨论】:

    标签: search elasticsearch filter


    【解决方案1】:

    试试这个

    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "Category": "Sport"
              }
            },
            {
              "multi_match": {
                "query": "world series",
                "type": "cross_fields",
                "operator": "and",
                "fields": [
                  "Question",
                  "Answer"
                ]
              }
            }
          ],
          "must_not": [],
          "should": []
        }
      },
      "from": 0,
      "size": 10,
      "sort": [],
      "aggs": {}
    }
    

    【讨论】:

      【解决方案2】:

      您可以添加一个名为 Searchtext 的额外字段,用于分析,Searchtext 可能包括类别、问题、答案

      那么;

      {
      "query": {
          "constant_score": {
              "filter": {
                  "bool": {
                      "must": {
                          "term": {
                              "Category": "Sport"
                          },
                          "match": {
                              "SearchText": {
                                  "query": "world series",
                                  "operator": "and"
                              }
                          }
                      }
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多