弹性搜索 - 过滤复杂的逻辑查询

时间:2018-05-24 11:32:18

标签: elasticsearch

我需要找到一种方法来处理如何在过滤器上下文中处理逻辑查询,以便对获得的结果应用过滤器。

举个例子 - ((" Fox" AND" Wolf")或(#34; Hate" AND" Seafood")或&# 34;福克斯"。)

我想应用一个过滤器,它给出了与此查询相对应的文档,但也包含文档所在的位置。对应的值是175。

以下查询是我在Kibana传递的内容

{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "value": {
            "value": 175
          }
        }
      },
      "should": [
            {
              "bool": {
                "must": [
                  {
                    "match": {
                      "desc": "Fox"
                    }
                  },
                  {
                    "match": {
                      "desc": "Wolf"
                    }
                  }
                ]
              }
            },
             {
              "bool": {
                "must": [
                  {
                    "match": {
                      "desc": "Hate"
                    }
                  },
                  {
                    "match": {
                      "desc": "Seafood"
                    }
                  }
                ]
              }
            },
            {
                "match": {"desc": "Fox"}
            }
          ]
    }
  }
}

这为我提供了所有结果,这些结果仅对应于那里提供的过滤器 - 其中值== 175.我设法绕过这一点的一种方法是为查询提供0.0000001的最低分数要求,以便消除冗余值。但我更喜欢更清洁的解决方案。

将整个查询集成到过滤器上下文中以使其被Elastic缓存更有益,但我似乎无法找到方法。

以下是使用上述查询时提供的结果列表。

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 2.1192918,
    "hits": [
      {
        "_index": "keywordprocessor",
        "_type": "my_type",
        "_id": "6",
        "_score": 2.1192918,
        "_source": {
          "desc": "I Hate Seafood",
          "value": 175
        }
      },
      {
        "_index": "keywordprocessor",
        "_type": "my_type",
        "_id": "7",
        "_score": 1.4723401,
        "_source": {
          "desc": "I Hate Seafood",
          "value": 175,
          "vendor": "Seafood"
        }
      },
      {
        "_index": "keywordprocessor",
        "_type": "my_type",
        "_id": "8",
        "_score": 0,
        "_source": {
          "desc": "I Hate peanuts",
          "value": 175,
          "vendor": "Seafood"
        }
      },
      {
        "_index": "keywordprocessor",
        "_type": "my_type",
        "_id": "11",
        "_score": 0,
        "_source": {
          "desc": "I Hate peanuts",
          "value": 175
        }
      }
    ]
  }
}

任何帮助都会受到赞赏 - 我已经查看过布尔查询等文档,并且似乎无法对此做出正面或反面。

1 个答案:

答案 0 :(得分:1)

要在filter上下文中合并bool查询,您应该使用bool子句,如下所示:

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "value": 175
              }
            }
          ],
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "match": {
                      "desc": "Fox"
                    }
                  },
                  {
                    "match": {
                      "desc": "Wolf"
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "match": {
                      "desc": "Hate"
                    }
                  },
                  {
                    "match": {
                      "desc": "Seafood"
                    }
                  }
                ]
              }
            },
            {
              "match": {
                "desc": "Fox"
              }
            }
          ]
        }
      }
    }
  }
}
相关问题