Home > Net >  ElasticSearch - Multiple query on one call (with sub limit)
ElasticSearch - Multiple query on one call (with sub limit)

Time:11-22

I have a problem with ElasticSearch, I need you :)

Today I have an index in which I have my documents. These documents represent either Products or Categories.

The structure is this:

{
    "_index": "documents-XXXX",
    "_type": "_doc",
    "_id": "cat-31",
    "_score": 1.0,
    "_source": {
        "title": "Category A",
        "type": "category",
        "uniqId": "cat-31",
        [...]
    }
},
{
    "_index": "documents-XXXX",
    "_type": "_doc",
    "_id": "prod-1",
    "_score": 1.0,
    "_source": {
        "title": "Product 1",
        "type": "product",
        "uniqId": "prod-1",
        [...]
    }
},

What I'd like to do, in one call, is:

  • Have 5 documents whose type is "Product" and 2 documents whose type is "Category". Do you think it's possible?
    That is, two queries in a single call with query-level limits.

Also, isn't it better to make two different indexes, one for the products, the other for the categories? If so, I have the same question, how, in a single call, do both queries?

Thanks in advance

CodePudding user response:

If product and category are different contexts I would try to separate them into different indices. Is this type used in all your queries to filter results? Ex: I want to search for the term xpto in docs with type product or do you search without applying any filter?

About your other question, you can apply two queries in a request. The Multi search API can help with this.

You would have two answers one for each query.

GET my-index-000001/_msearch
{ }
{"query": { "term": { "type": { "value": "product" } }}}
{"index": "my-index-000001"}
{"query": { "term": { "type": { "value": "category" } }}}
  • Related