Elasticsearch 短语匹配过滤器

Elasticsearch Phrase Match In Filter

我有一个查询,它按给定的时间间隔在文本字段中搜索给定的术语。我想将短语匹配添加到此查询中,我该如何添加;例如,我会将 "has parti" 作为短语查找,但文本中不应包含 "ahmet" 字词。我怎样才能做到这一点;代码在这里;

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "text": [
                  "has",
                  "parti"
                ]
              }
            },
            {
              "range": {
                "date": {
                  "gt": "2015-08-27",
                  "lte": "2015-08-28"
                }
              }
            }
          ]
        }
      }
    }
  }
}

Elasticsearch 提供 Phrase matching, but I don't think you can use it in the filter,或者至少我没能让它工作。我有一个解决方案,match_phrasequery,条件是text不包含ahmet,而时间间隔在filter .检查它是否适合您。

{
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "must": [
                        {
                            "match_phrase": {
                                "text": "has parti"
                            }
                        }
                    ],
                    "must_not": [
                        {
                            "match": {
                                "text": "ahmet"
                            }
                        }
                    ]
                }
            },
            "filter": {
                "bool": {
                    "must": [
                        {
                            "range": {
                                "date": {
                                    "gt": "2015-08-27",
                                    "lte": "2015-08-28"
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}

顺便说一下,您的 date 看起来像是被映射为字符串,否则您的请求将失败并显示

ElasticsearchParseException[failed to parse date field [2015-08-22], tried both date format [date_time], and timestamp number]; nested: IllegalArgumentException[Invalid format: \"2015-08-22\" is too short]; }]

我建议使用适当的映射,但这与您的问题并没有真正的关系

更新:

刚回来补充 we did the right thing:过滤器不适用于 全文 搜索

更新:

the filtered query has been deprecated 以来,在新版本中应重写查询,以便将过滤器移动到 bool 查询中:

{

    "query": {
        "bool": {
            "must": [{
                "match_phrase": {
                    "text": "has parti"
                }
            }],
            "must_not": [{
                "match": {
                    "text": "ahmet"
                }
            }],
            "filter": {
                "bool": {
                    "must": [{
                        "range": {
                            "date": {
                                "gt": "2015-08-27",
                                "lte": "2015-08-28"
                            }
                        }
                    }]
                }
            }
        }
    }

}

您将需要使用 phrase match query。 但是由于这是一个查询并且您正在寻找过滤器,因此您需要将其包装在 query filter.

完成此操作后,您应该能够实施短语匹配过滤器。 接下来,当你需要一个否定时,将你的语句放在 bool 过滤器的 must_not 中。您可以使用术语过滤器。

所以最后你的查询应该看起来像这样 -

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must_not": [
            {
              "term": {
                "title": "ahmet"
              }
            }
          ],
          "must": [
            {
              "range": {
                "date": {
                  "gt": "2015-08-27",
                  "lte": "2015-08-28"
                }
              }
            },
            {
              "constantScore": {
                "filter": {
                  "query": {
                    "match_phrase": {
                      "title": "has parti"
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}