简体   繁体   English

如何在弹性搜索中进行精确匹配?

[英]how to do exact match in elastic search?

I want to find exact match.if there is no match.I don't want to null .我想找到完全匹配。如果没有匹配。我不想null

I am search this id 6a8c283f-1e75-ec11-8943-000d3a15f525 .我正在搜索这个 id 6a8c283f-1e75-ec11-8943-000d3a15f525 it is giving me this output它给了我这个 output

  • fcf0dd4e-abbc-ec11-983f-0022482588e9 fcf0dd4e-abbc-ec11-983f-0022482588e9

But these it is not matched only ec11 matched但是这些它不匹配只有ec11匹配

I tried this search query我试过这个搜索查询

 result = await client.search({
          index: 'products',
          "query": {
            "match": {
              "sysid":sysid
            }
          }
        })

The problem is because your sysid field is of type text and is thus analyzed at indexing time and tokenized into fcf0dd4e , abbc , ec11 , 8943 and 000d3a15f525 .问题是因为您的sysid字段是text类型,因此在索引时被分析并标记为fcf0dd4eabbcec118943000d3a15f525

Also when searching using a match query, the searched term is analyzed as well into the following tokens 6a8c283f , 1e75 , ec11 , 8943 , 000d3a15f525 .此外,当使用匹配查询进行搜索时,搜索到的词也会被分析为以下标记6a8c283f1e75ec118943000d3a15f525

As you saw, the match works because ec11 matches.如您所见,匹配成功是因为ec11匹配。

For an exact match, what you need to do is to run your query against a keyword field instead.对于完全匹配,您需要做的是改为针对keyword字段运行查询。 Maybe you're lucky and your mapping already contains a sysid.keyword sub-field.也许您很幸运,并且您的映射已经包含一个sysid.keyword子字段。 If not, you need to change your mapping and reindex your data.如果不是,则需要更改映射并重新索引数据。

Then you'll be able to run your term query like this and get your exact match:然后你就可以像这样运行你的term查询并得到你的精确匹配:

  "term": {
     "sysid.keyword":sysid
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM