如何让 Elastic Engine 理解一个字段不进行精确匹配分析? [英] How to make Elastic Engine understand a field is not to be analyzed for an exact match?

查看:42
本文介绍了如何让 Elastic Engine 理解一个字段不进行精确匹配分析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题基于上一篇文章 基于 MatchMatchPhrasePrefix 的精确搜索不起作用.

The question is based on the previous post where the Exact Search did not work either based on Match or MatchPhrasePrefix.

然后我在这里找到了类似的帖子,其中搜索字段在映射定义中设置为 not_analyzed(@Russ Cam).

Then I found a similar kind of post here where the search field is set to be not_analyzed in the mapping definition (by @Russ Cam).

但我正在使用

package id="Elasticsearch.Net" version="7.6.0" targetFramework="net461"
 package id="NEST" version="7.6.0" targetFramework="net461"

并且可能是因为这个原因解决方案不起作用.

and might be for that reason the solution did not work.

因为如果我通过SOME",它会与SOME"和SOME OTHER LOAN"匹配,这不应该是这种情况(在我之前的产品价值"帖子中).

Because If I pass "SOME", it matches with "SOME" and "SOME OTHER LOAN" which should not be the case (in my earlier post for "product value").

如何使用 NEST 7.6.0 做同样的事情?

How can I do the same using NEST 7.6.0?

推荐答案

好吧,我不知道您当前的映射看起来如何.我也不知道 NEST,但我会解释

Well I'm not aware of how your current mapping looks. Also I don't know about NEST as well but I will explain

如何让 Elastic Engine 理解一个字段是不进行精确匹配分析的?

How to make Elastic Engine understand a field is not to be analyzed for an exact match?

以使用弹性 dsl 的示例为例.

by an example using elastic dsl.

对于完全匹配(区分大小写),您需要做的就是将字段类型定义为 keyword.对于 keyword 类型的字段,数据按原样编入索引,无需应用任何分析器,因此非常适合精确匹配.

For exact match (case sensitive) all you need to do is to define the field type as keyword. For a field of type keyword the data is indexed as it is without applying any analyzer and hence it is perfect for exact matching.

PUT test
{
  "mappings": {
    "properties": {
      "field1": {
        "type": "keyword"
      }
    }
  }
}

现在让我们索引一些文档

Now lets index some docs

POST test/_doc/1
{
  "field1":"SOME"
}

POST test/_doc/2
{
  "field1": "SOME OTHER LOAN"
}

对于精确匹配,我们可以使用术语查询.让我们搜索SOME",我们应该得到文档 1.

For exact matching we can use term query. Lets search for "SOME" and we should get document 1.

GET test/_search
{
  "query": {
    "term": {
      "field1": "SOME"
    }
  }
}

我们得到的 O/P:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.6931472,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.6931472,
        "_source" : {
          "field1" : "SOME"
        }
      }
    ]
  }
}

所以关键是将字段类型设为keyword并使用term查询.

So the crux is make the field type as keyword and use term query.

这篇关于如何让 Elastic Engine 理解一个字段不进行精确匹配分析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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