Query not returning correct result


(Zelfapp) #1

My query below is returning an incorrect result. You can see that the "params" in the result do not perfectly match the terms params in the query. How do I craft my query so that the params must match perfectly?

The query:

GET _search
{
   "query": {
      "filtered": {
         "filter": {
            "bool": {
               "must": [
                  {
                     "term": {
                        "class": "ezr_ribbon_army"
                     }
                  },
                  {
                     "term": {
                        "award_sku": "401 ARACMR"
                     }
                  },
                  {
                    "terms": {
                      "params": [
                        "bronze-star|1",
                        "branch|army",
                        "type|standard_ribbons",
                        "spacing|flush",
                        "alignment|center",
                        "assembly|assembled",
                        "width|3"
                      ]
                    }
                  },
                  {
                    "term": {
                      "params_count": 7
                    }
                  }
               ]
            }
         }
      }
   }
}

The results:

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "awards.v1",
            "_type": "awards",
            "_id": "AU5pVpCi-IpoJe5I_BhB",
            "_score": 1,
            "_source": {
               "images": [
                  "120445907559bfe269242d0.48849945.png"
               ],
               "award_sku": "401 ARACMR",
               "class": "ezr_ribbon_army",
               "params": [
                  "bronze-star|1",
                  "silver-star|2",
                  "spacing|flush",
                  "alignment|center",
                  "assembly|assembled",
                  "branch|army",
                  "type|standard_ribbons"
               ],
               "device_skus": [
                  "937NP",
                  "914NP"
               ],
               "params_count": 7
            }
         }
      ]
   }
}

The mapping:

{
   "awards.v1": {
      "mappings": {
         "awards": {
            "_all": {
               "enabled": false
            },
            "properties": {
               "award_sku": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "class": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "device_skus": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "images": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "params": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "params_count": {
                  "type": "integer"
               },
               "skus": {
                  "type": "string",
                  "index": "not_analyzed"
               }
            }
         }
      }
   }
}

(Mark Harwood) #2

the "params" in the result do not perfectly match the terms params in the query.

Were you expecting the terms filter to be an AND? It's an OR - see https://www.elastic.co/search?q=terms+filter

"Filters documents that have fields that match any of the provided terms"


(Zelfapp) #3

Thanks for opening my eyes to that. Clearly misread. So to change the default OR behavior of terms if I'm reading the docs correctly (https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html) this is the correct way to tell terms to use AND. It seems to be working now. Thank you for the heads up.

"terms": {
    "params": [
        "bronze-star|1",
        "branch|army",
        "type|standard_ribbons",
        "spacing|flush",
        "alignment|center",
        "assembly|assembled",
        "width|3"
    ],
    "execution" : "and",
    "_cache": true
}

(system) #4