本文介绍了如何使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

以使用弹性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理解将不分析字段以进行精确匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:11