S
S
SergeyTSA2016-03-24 21:26:04
Web development
SergeyTSA, 2016-03-24 21:26:04

How to correctly form a search query in Elasticsearch?

Hello!
Please help me to correctly form a search query in elastic.
There is an index for products
localhost:9200/test_index/product
Here is the mapping:

{
  "test_index" : {
    "mappings" : {
      "product" : {
        "properties" : {
          "category" : {
            "type" : "integer"
          },
          "id" : {
            "type" : "integer"
          },
          "name" : {
            "type" : "string",
            "index" : "not_analyzed"
          },
          "property_stock" : {
            "type" : "integer"
          },
          "skus" : {
            "type" : "string",
            "index" : "not_analyzed"
          },
          "tags" : {
            "properties" : {
              "group_id" : {
                "type" : "integer"
              },
              "tag_id" : {
                "type" : "integer"
              }
            }
          },
          "visible" : {
            "type" : "boolean"
          }
        }
      }
    }
  }
}

How to make a search request if, for example, you need a product to be included in at least one of the categories (for example, [111, 222, 333, 444]) and it had tags { "group_id": 10, "tag_id": 20} and { "group_id": 50, "tag_id": 100} at the same time.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SergeyTSA, 2016-03-25
@SergeyTSA

I myself will answer my own question :-)
I managed to do it in this way:

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "bool": {
                "must": [
                  {"term": {"tags.group_id": 818}},
                  {"term": {"tags.tag_id": 223}}
                ]
              }
            },
            {
              "bool": {
                "should": [
                  {"term": {"categories": 9110}},
                  {"term": {"categories": 9111}},
                  {"term": {"categories": 9114}},
                  {"term": {"categories": 9109}}
                ]
              }
            }
          ]
        }
      }
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question