Elasticsearch反向match_phrase [英] Elasticsearch reverse match_phrase

查看:112
本文介绍了Elasticsearch反向match_phrase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下文档:

{
  "Title": "Western Europe"
}

我想对 Title 字段运行此类搜索查询

I'd like to run such search queries against Title field


  • 西欧的苹果

  • 东欧的苹果

  • Apple in Western Europe
  • Apple in Eastern Europe

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

I could run a simple match query:

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

很明显,它会匹配并将其带回我会使用哪个搜索词组。但是我想进行一个查询,该查询仅在标题字段短语匹配我的搜索查询时才带回我的文档。那可能吗?还有其他参数吗? 短语匹配

It obiviously would match and bring it back irregardless of which search phrase I'd use. But I'd like to make a query that would bring my document back only if Title field phrase matches my search query. Is that possible? Are there any additional parameters? It seems like a reverse case for phrase matching.

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

If not, should I consider reindexing my data with shingles?

因此在这种情况下,运行此(

So in this scenario running this (with additional parameters) wouldn't score and bring back my document.

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

tl; dr

我该如何写一个查询,如果它的所有字段(我正在搜索的)标记都出现在我的搜索查询中,它将带回文档?
例如,我在文档中的字段仅包含以下两个标记:

How do I write a query that would bring back document if all of its field (the one I'm searching on) tokens are present in my search query? For instance my field in document contains these two tokens only:


  • abc

  • xyz

如果我的搜索词是,例如 Lorem ipsum dolor sit amet,则可以方便地使用elit abc xyz ,文档已带回

And if my search phrase is, for instance Lorem ipsum dolor sit amet, consectetur adipiscing elit abc xyz, document is brought back.

如果是 Lorem ipsum dolor amet,则可能会导致adipiscing adipiscing elit xyz ,它没有不带回来

推荐答案

我知道Stefan在评论中提供了一种简单有效的解决方案,但您可能还需要查看跨度查询仅供参考!

I know Stefan has given a simple and efficient solution in the comments, but you may also want to look at Span Queries as an FYI!!

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

I've created sample mapping, documents, query and response:

PUT my_span_index
{
  "mappings": {
    "properties": {
      "Title":{
        "type": "text"
      }
    }
  }
}



示例文档:



Sample Documents:

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"
}



< h2> Span Query:

Span Query:

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
        }
    }
}

请注意,我使用了跨近& 跨度查询并注意上面的评论。

Note that I made use of Span Near & Span Term Query and do note the comments above.

{
  "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 的文档,但是如果将斜率更改为较小的值,它不会出现。

Note that in the response the doc having id:3 is also returned, however if you change the slop to lesser value, it would not appear.

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

The pain would be that you'd end up writing/generating long query at your application side if your request is going to have more tokens.

希望我帮助了!

这篇关于Elasticsearch反向match_phrase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆