dao hodac
dao hodac

Reputation: 371

elasticsearch: term query fails

I have a mapping for some documents and queries agains terms does fail. I don't understand why:

"mappings":{
     "timeslot":{
            "properties":{
                 "FOB_IN":{
                        "type":"long"
                 },
                 "TRIGGER_CODE":{
                        "type":"long"
                 },
                 "FLIGHT_PHASE":{
                        "type":"long"
                 },
                 "REP16_TRIG":{
                        "type":"long"
                 },
                 "fwot":{
                        "type":"string"
                 },
                 "FOB_OUT":{
                        "type":"long"
                 },
                 "FP":{
                        "type":"long"
                 },
                 "FLTNB":{
                        "type":"string"
                 },
                 "Date":{
                        "format":"strict_date_optional_time||epoch_millis",
                        "type":"date"
                 }
            }
     }
}

I can make a term query against TRIGGER_CODE, for example, and it works fine

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 5,
      "max_score": 4.4446826,
      "hits": [
         {
            "_index": "merged-2016-04",
            "_type": "timeslot",
            "_id": "AVRS8VnirVLwfvMnwpXb",
            "_score": 4.4446826,
            "_source": {
               "Date": "2016-04-03T08:42:44+0000",
               "FLIGHT_PHASE": 20,
               "TRIGGER_CODE": 4000,
               "fwot": "A6-APA"
            }
         }
      ]
   }
}

now the same against fwot does fail. What's wrong?

GET merged-2016-04/_search?size=1
{
    "query" : {
        "term" : { "fwot": "A6-APA"}
    }
}

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

Upvotes: 2

Views: 1340

Answers (3)

RAHUL JAIN
RAHUL JAIN

Reputation: 11

We Can use keyword

GET merged-2016-04/_search?size=1
{
  "query": {
      "term": {
        "fwot.keyword": "A6-APA"  
      }
   }
}

Upvotes: 0

Cavaz
Cavaz

Reputation: 3119

See the doc page Query DLS term query, note "Why doesn’t the term query match my document" for a detailed explanation.

Upvotes: 0

Andrei Stefan
Andrei Stefan

Reputation: 52368

You need fwot to be "index": "not_analyzed" for that to work. And you need to reindex the data for the above change to work.

Here's the complete list of commands for the mapping change and some test data:

PUT /merged-2016-04
{
  "mappings": {
    "timeslot": {
      "properties": {
        "FOB_IN": {
          "type": "long"
        },
        "TRIGGER_CODE": {
          "type": "long"
        },
        "FLIGHT_PHASE": {
          "type": "long"
        },
        "REP16_TRIG": {
          "type": "long"
        },
        "fwot": {
          "type": "string",
          "index": "not_analyzed"
        },
        "FOB_OUT": {
          "type": "long"
        },
        "FP": {
          "type": "long"
        },
        "FLTNB": {
          "type": "string"
        },
        "Date": {
          "format": "strict_date_optional_time||epoch_millis",
          "type": "date"
        }
      }
    }
  }
}

POST /merged-2016-04/timeslot
{
  "Date": "2016-04-03T08:42:44+0000",
  "FLIGHT_PHASE": 20,
  "TRIGGER_CODE": 4000,
  "fwot": "A6-APA"
}

GET merged-2016-04/_search?size=1
{
  "query": {
    "term": {
      "fwot": "A6-APA"
    }
  }
}

Upvotes: 4

Related Questions