O
O
Oleg-Ukraine2017-09-12 14:21:28
elasticsearch
Oleg-Ukraine, 2017-09-12 14:21:28

How to correctly compose a query in ElasticSearch?

My products are stored in this format in Elastica

{
            "_index": "market",
            "_type": "products",
            "_id": "4",
            "_score": 0.5619608,
            "_source": {
               "id": 4,
               "title": "Zora",
               "category_id": 3,
               "price": "35000.00",
               "characteristicValue": [
                  {
                     "id": 1,
                     "name": "Красный",
                     "characteristic": {
                        "id": 2,
                        "name": "color"
                     }
                  }
               ]
            }
         }

I need to filter by product properties.
In particular, I consider one filter
"query": {
        "bool": {
            "must": [
               {"match": {
                  "characteristicValue.characteristic.name": "color"
               }}
            ],
            "should": [
               {"terms": {
                  "characteristicValue.name": [
                     "красный",
                     "синий"
                  ]
               }}
            ]
            
        }
    }

In this query, everyone who has the color property falls out, but they do not have colors (red or green). How to fix a request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2017-09-12
@Oleg-Ukraine

what should be moved to must
https://www.elastic.co/guide/en/elasticsearch/ref...
If the bool query is in a query context and has a must or filter clause then a document will match the bool query even if none of the should queries match . In this case these clauses are only used to influence the score .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question