elasticsearch 过滤器中的弹性搜索短语匹配,elasticsearch,filter,phrase,elasticsearch,Filter,Phrase" /> elasticsearch 过滤器中的弹性搜索短语匹配,elasticsearch,filter,phrase,elasticsearch,Filter,Phrase" />

elasticsearch 过滤器中的弹性搜索短语匹配

elasticsearch 过滤器中的弹性搜索短语匹配,elasticsearch,filter,phrase,elasticsearch,Filter,Phrase,我有一个查询,它按给定的时间间隔搜索文本字段中的给定术语。我想在这个查询中添加短语匹配,如何添加;例如,我会寻找“hasparti”作为一个短语,但是文本中不应该有“ahmet”这个词。我怎样才能做到这一点;代码在这里 { "query": { "filtered": { "filter": { "bool": { "must": [ { "terms": {

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

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "text": [
                  "has",
                  "parti"
                ]
              }
            },
            {
              "range": {
                "date": {
                  "gt": "2015-08-27",
                  "lte": "2015-08-28"
                }
              }
            }
          ]
        }
      }
    }
  }
}
Elasticsearch提供了,但我不认为您可以在中使用它,或者至少我没有设法让它工作。我有一个解决方案,在
查询
中使用
匹配短语
,以及
文本
不包含
ahmet
,而时间间隔保留在
过滤器
中的条件。看看对你来说是否足够好

{
    "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"
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}
顺便说一下,您的
日期
看起来像是映射为字符串,因为否则您的请求将失败

ElasticsearchParseException[未能解析日期字段[2015-08-22],尝试了日期格式[date_time]和时间戳编号];嵌套:IllegalArgumentException[无效格式:\'2015-08-22\'太短];}]

我建议使用适当的映射,但这与您的问题并不相关

更新:

刚刚回来补充说:过滤器不是用于全文搜索的

更新:

因为,在新版本中,为了在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"
                            }
                        }
                    }]
                }
            }
        }
    }

}
您将需要使用。 但是,由于这是一个查询,并且您正在寻找一个过滤器,因此需要将其封装在中

完成后,您应该能够实现短语匹配过滤器。 接下来,当你需要一个否定时,把你的语句放在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"
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

QueryParsingException不会失败吗?嵌套:QueryParsingException[[]没有为[ConstantCore]注册筛选器]