Elasticsearch反向匹配短语

问题描述 投票:3回答:2

请考虑以下文件:

{
  "Title": "Western Europe"
}

我想对标题字段运行这样的搜索查询

  • 西欧的苹果
  • 东欧的苹果

我可以运行一个简单的匹配查询:

POST /_search
{
  "query": {
    "match": {
      "Title": "Apple in Western Europe"
    }
  }
}

很明显,无论我要使用哪个搜索词组,它都会匹配并带回去。但是我想进行一个查询,该查询仅在Title字段phrase match我的搜索查询中才可以带回我的文档。那可能吗?还有其他参数吗? phrase matching似乎是相反的情况。

如果没有,我应该考虑使用带状疱疹为我的数据重新编制索引吗?

因此,在这种情况下,运行此命令(带有其他参数)将不会得分并不会带回我的文档。

POST /_search
{
  "query": {
    "match": {
      "Title": "Apple in Eastern Europe"
    }
  }
}

tl; dr

如果我的搜索查询中存在文档的所有字段(我正在搜索的)标记,那么我将如何编写一个查询来检索文档?例如,我在文档中的字段仅包含这两个标记:

  • abc
  • xyz

并且如果我的搜索词组是例如Lorem ipsum dolor sit amet,consectetur adipiscing elit abc xyz,则文档是brought back

如果是Lorem ipsum dolor sit amet,consectetur adipiscing elit xyz,它是not not back

elasticsearch
2个回答
0
投票
尝试使用具有不同参数的“间隔查询”,这可以为您提供帮助。

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-intervals-query.html


0
投票
我知道Stefan在评论中给出了一个简单而有效的解决方案,但您可能还想将Span Queries视为FYI!

我已经创建了示例映射,文档,查询和响应:

映射:

PUT my_span_index { "mappings": { "properties": { "Title":{ "type": "text" } } } }
样本文件:

POST my_span_index/_doc/1 { "Title": "Western Europe" } POST my_span_index/_doc/2 { "Title": "Eastern Europe" } //slop - distance between words Western and Europe here is 13 POST my_span_index/_doc/3 { "Title": "As far as Western culture is America, we see gradually more and more of the same in Europe" }
跨度查询:

POST my_span_index/_search { "query": { "span_near" : { "clauses" : [ { "span_term" : { "Title": "western" } }, { "span_term" : { "Title": "europe" } } ], "slop" : 12, <---- Distance Between Words "in_order" : true <---- If order is important } } }
请注意,我使用了Span NearSpan Term Query,并注意上面的注释。 

响应:

{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.5420371, "hits" : [ { "_index" : "my_span_index", "_type" : "_doc", "_id" : "1", "_score" : 0.5420371, "_source" : { "Title" : "Western Europe" } }, { "_index" : "my_span_index", "_type" : "_doc", "_id" : "3", "_score" : 0.028773852, "_source" : { "Title" : "As far as Western culture is America, we see gradually more and more of the same in Europe" } } ] } }
注意,在响应中还会返回具有id:3的文档,但是,如果将斜率更改为较小的值,则不会出现。 

痛苦的是,如果您的请求将有更多令牌,您最终将在应用程序端编写/生成长查询。

希望我帮助了!

© www.soinside.com 2019 - 2024. All rights reserved.