如何在弹性搜索中确切匹配?

发布于 2025-01-21 22:22:46 字数 452 浏览 2 评论 0原文

我想找到确切的匹配。如果没有匹配。我不想 null

我正在搜索此ID 6A8C283F-1E75-EC11-8943-000D3A15F525。 它给了我这个输出

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

但它们不匹配ec11

我尝试了此搜索查询

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

I want to find exact match .if there is no match .I don't want to null.

I am search this id 6a8c283f-1e75-ec11-8943-000d3a15f525.
it is giving me this output

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

But these it is not matched only ec11 matched

I tried this search query

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

長街聽風 2025-01-28 22:22:46

问题是因为您的sysid字段是类型text,因此在索引时间进行分析,并将其标记为fcf0dd4eabbc ,EC118943000D3A15F525

同样,当使用匹配查询搜索时,也将搜索术语分析到以下令牌6a8c283f1E75ec118943000D3A15F525

如您所见,匹配有效,因为ec11匹配。

对于确切的匹配,您需要做的是针对关键字字段运行查询。也许您很幸运,您的映射已经包含sysid.keyword子场。如果没有,您需要更改映射并重新指标数据。

然后,您将能够运行术语查询,并确切匹配:

  "term": {
     "sysid.keyword":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.

Also when searching using a match query, the searched term is analyzed as well into the following tokens 6a8c283f, 1e75, ec11, 8943, 000d3a15f525.

As you saw, the match works because ec11 matches.

For an exact match, what you need to do is to run your query against a keyword field instead. Maybe you're lucky and your mapping already contains a sysid.keyword sub-field. 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": {
     "sysid.keyword":sysid
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文