Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

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

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

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).

But I am using

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.

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").

How can I do the same using NEST 7.6.0?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

by an example using elastic dsl.

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"
}

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 that we get:

{
  "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"
        }
      }
    ]
  }
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...