0

I performed below elasticsearch query.

GET amasyn/_search
{
    "query": {
        "bool" : {
            "filter" : {
                "term": {"ordernumber": "112-9550919-9141020"}
            }
        }
    }
}

But it does not get any hits

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

But I have a document having this ordernumber in the index. ordernumber is a text field.

When I change the above query by replacing term with match, I get total number of hits as the no of hits for the given query. Please explain what's happening here and how to solve this.

Pramodya Mendis
  • 686
  • 8
  • 24

1 Answers1

2

This is because since you used ordernumber field with type as text, so it is getting analyzed. Please refer difference between text and keyword through this answer Difference between keyword and text in ElasticSearch.

In this way you can define both text and keyword for your ordernumber field.

Mapping

{
    "mappings": {
        "properties": {
            "ordernumber": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }
        }
    }
}

and then you can use term query as below :

{
    "query": {
        "bool" : {
            "filter" : {
                "term": {"ordernumber.keyword": "112-9550919-9141020"}
            }
        }
    }
}

Please see, how text and keyword fields are tokenized for your text.

Standard analyzer

This analyzer is used when you were defining your field as text.

{
    "analyzer": "standard",
    "text" : "112-9550919-9141020"
}

result :

 {
        "tokens": [
            {
                "token": "112",
                "start_offset": 0,
                "end_offset": 3,
                "type": "<NUM>",
                "position": 0
            },
            {
                "token": "9550919",
                "start_offset": 4,
                "end_offset": 11,
                "type": "<NUM>",
                "position": 1
            },
            {
                "token": "9141020",
                "start_offset": 12,
                "end_offset": 19,
                "type": "<NUM>",
                "position": 2
            }
        ]
    }

Keyword Analyzer

This analyzer is used when you are defining your field as keyword.

{
    "analyzer": "keyword",
    "text" : "112-9550919-9141020"
}

Result

 {
        "tokens": [
            {
                "token": "112-9550919-9141020",
                "start_offset": 0,
                "end_offset": 19,
                "type": "word",
                "position": 0
            }
        ]
    }