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

后端 未结 1 1170
眼角桃花
眼角桃花 2020-12-22 10:04

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 s

相关标签:
1条回答
  • 2020-12-22 10:30

    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.

    0 讨论(0)
提交回复
热议问题