如何在弹性搜索中在同一个字段上进行精确的值和匹配查询? [英] How to make exact values and match queries on same field in elasticsearch?

查看:137
本文介绍了如何在弹性搜索中在同一个字段上进行精确的值和匹配查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个字段存储的格式如下: number / year ,类似23/2014,24/2014,12/2015等。

So I have a field that stores a value in the format: number/year, something like 23/2014, 24/2014, 12/2015, etc...

所以如果这个字段被映射为一个 not_analyzed 一个,我可以使用term过滤器进行精确的搜索,如果我在这个确切的结构中搜索一个值(类似于1/2014,15/2014,...),它就像 sql equals(=)一样工作。

so if this field is mapped as a not_analyzed one, I can make exact value searches with term filter, if I search for a value in that exact structure(something like 1/2014, 15/2014,...) it works, like the sql equals(=).

{
  "query": {
    "filtered": {
      "filter": {
        "term": {
          "processNumber": "11/2014"
        }
      }
    }
  }
}

所以,用不同于11 /,/ / 2014的东西进行搜索不会返回命中。这是很好的。

So, searching with something different like 11/, or /2014 wouldn't return hits. This is fine.

但是,如果我将字段定义为 not_analyzed ,我不能使 sql LIKE 使用 match_phrase 查询键入搜索。

But if I define the field as not_analyzed, I can't make sql LIKE type searches with the match_phrase query.

{
  "query": {
    "match_phrase": {
      "processNumber": "11/201"
    }
  }
}

在这种情况下,搜索11,11 /,/ 2014或2014应该返回命中,但他们没有
事情是,如果该字段未映射为 not_analyzed 一个,则该查询将起作用。所以看来我必须使用一个或另一个,问题是该字段应该支持不同的查询的两个选项,我在这里缺少一些东西?

In this case searching for 11,11/,/2014 or 2014 should return hits, but they don't. The thing is, this query works if the field is not mapped as a not_analyzed one. So it seems I have to either use one or the other, the problem is that the field should support both options for different queries, am I missing something here?

推荐答案

您可以使用 processNumber 当前/ _multi_fields.html#_multi_fieldsrel =nofollow noreferrer> fields 属性在映射中:

You can analyze the same field processNumber in different ways using the fields property in the mapping:

例如,如果你想分析和未分析的版本of ProcessNumber的映射将是:

For example if you want the analyzed and unanalyzed version of ProcessNumber the mapping would be :

 {
   "type_name": {
      "properties": {
         "processNumber": {
            "type": "string",
            "index": "not_analyzed",
            "fields": {
               "analyzed": {
                  "type": "string",
                  "index": "analyzed"
               }
            }
         }
      }
   }
}

其中未分析的字段在查询中被引用为 em> processNumber

Where the not-analyzed field is referred in query as processNumber .

要引用该域的分析视图,请使用 processNumber.analyzed

To refer to the analyzed view of the field use processNumber.analyzed

对术语11/201,11等的查询将是:

The queries for terms 11/201, 11 etc would be :

示例过滤器:

 { "query" : { "filtered" : { "filter" : { "term" : { "processNumber" : "11/2014" } } } } }

术语过滤器不分析搜索字符串,因此输入在这种情况下,将与字段颠倒索引匹配:11/2014反对字段。

Term filter it does not analyze the search string so an input would be matched as it is with the fields inverted index in this case : 11/2014 against the field.

示例Match_Phrase_prefix:

Example Match_Phrase_prefix:

{ "query": { "match_phrase_prefix": { "processNumber": "11/201" } } }

match_phrase_prefix 尝试检查最后一个术语在短语中是索引中术语的前缀。
如果指定了分析器,则分析搜索字符串。这就是您在这里使用未分析的版本的原因。如果我们使用 processNumber.analyzed 搜索查询(例如 11-201,11 | 201 )也将匹配

match_phrase_prefix tries to check if the last term in the phrase is a prefix of terms in index . It analyzes the search string if an analyzer is specified. This is the reason you need to use the unanalyzed version of the field here . If we use processNumber.analyzed search queries such as 11-201 , 11|201 would also match

示例匹配:

  { "query": { "match": { "processNumber.analyzed": "11" } } }

这是直截了当的 match ,因为默认分析器(通常是标准分析器)将标记为11/2014

This is straight forward match since default analyzer (usually standard analyzer) will tokenize 11/2014 to terms 11, 2014 .

您可以使用analyze api查看默认分析器如何分析特定文本。

You can use the analyze api to see how a particular text gets analyzed by default analyzer.

curl -XPOST "http://<machine>/_analyze?text=11/2014"

这篇关于如何在弹性搜索中在同一个字段上进行精确的值和匹配查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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