N
N
Nokyta2014-04-21 23:12:54
elasticsearch
Nokyta, 2014-04-21 23:12:54

Is there an analogue of IN() in ElasticSearch for nested?

There is a mapping for products (a product can have several categories):

'categories' => array(
  'properties' => array(
    'category_id' => array(
      'type' => 'string',
      'index' => 'not_analyzed'
    ),
    'position' => array(
      'type' => 'integer',
      'index' => 'not_analyzed'
    )
  ),
  'type' => 'nested',
),

Those. we have standard nested. We also have an array of category IDs from which we need products. The question is how to build such a query? In fact, you need an analogue of IN () in MySql.
I have my own ideas:
- for each element of the array (category ID) make a new nested filter and put it in or, it scares, but what if there are 100 IDs?
- run script filter. But for some reason, this option is not very much liked, will such a search slow down?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MintTea, 2015-01-15
@MintTea

curl -XGET 'http://localhost:9200/index_name/product/_search?pretty' -d '{
    "filter": {
        "nested": {
            "path": "categories",
            "filter": {
                "terms": {
                    "category_id": ["category_one", "category_two"]
                }
            }
        }
    }
}'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question