Elasticsearch匹配精确术语 [英] Elasticsearch match exact term

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

问题描述

我有一个Elasticsearch存储库和一个aplication,用于为我们称为资产"的文档创建文档.我需要防止用户创建具有相同标题"的资产".

I have an Elasticsearch repo and a aplication that create documents for what we call 'assets'. I need to prevent users to create 'assets' with the same 'title'.

当用户尝试创建资产"时,我正在查询带有标题的存储库,如果存在匹配项,则会向用户显示错误消息.

When the user tries to create an 'asset' I am querying the repo with the title and if there is a match an error message is shown to the user.

我的问题是,当我查询标题时,会得到多个结果(对于类似的匹配项).

My problem is that when I query the title I am getting multiple results (for similar matches).

这是我到目前为止的查询:

This is my query so far:

GET assets-1/asset/_search
{
  "query": {
    "match": {
      "title": {
        "query": "test",
        "operator": "and"
      }
    }
  }
}

我有很多记录的标题为:"test 1","test 2","test bla",只有一个标题为"test"的记录.

I have many records with title: 'test 1', 'test 2', 'test bla' and only one with the title 'test'.

但是我得到了以上所有内容.

But I am getting all of the above.

我是否需要添加任何条件或属性,以便与该术语完全匹配?

Is there any condition or property I have to add to the query so I will exact match the term?

推荐答案

您的标题字段可能已被分析,因此 test 令牌将与包含该令牌的任何标题匹配.

Your title field is probably analyzed and thus the test token will match any title containing that token.

要实现完全匹配,您需要具有一个 not_analyzed 字段并对其进行 term 查询.

In order to implement an exact match you need to have a not_analyzed field and do a term query on it.

您需要将 title 字段的映射更改为此:

You need to change the mapping of your title field to this:

curl -XPUT localhost:9200/assets-1/_mapping/asset -d '{
   "asset": {
      "properties": {
         "title": {
            "type": "string",
            "fields": {
               "raw": {
                   "type": "string",
                   "index": "not_analyzed"
               }
            }
         }
      }
   }
}'

然后,您需要重新索引数据,然后就可以运行完全匹配查询,如下所示:

Then you need to reindex your data and you'll then be able to run an exact match query like this:

curl -XPOST localhost:9200/assets-1/asset/_search -d '{
   "query": {
      "term": {
         "title.raw": "test"
      }
   }
}'

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

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