سؤال

I'm using elasticsearch to serve people when they are using dropdown. So far, so good. Right now I'm trying to figure out is it possible to search from different types with different sorts/filters/limits.

So far I've found my way out with different filters per type and kinda with sorts. I've achieved different filters with boolean query, using shoulds and sorting is depending on scores. My goal is to show total 10 rows, where 8 of then should be products, 2 rows should be categories. For categories this limit should be max, if there is no categories to display, 10 products should be displayed. I've read the docs and I can see that size can be set to query body like sorting. But sorting has more flexibility. So yes, question is about the limit - is there a way to achieve it?

هل كانت مفيدة؟

المحلول

What you're asking for seems to be field collapsing or grouping.

There's a long-outstanding ticket for this feature on the Elasticsearch issue tracker: https://github.com/elasticsearch/elasticsearch/issues/256, with the latest update being (from http://www.elasticsearch.com/blog/from-amsterdam-with-love-elasticsearchs-second-company-all-hands/) that while progress is being made, it won't come before after 1.0 has been released:

We again fleshed out what is needed in order to properly support field collapsing in a distributed environment execution, as well as the ability to get inner hits (for nested / parent child cases). We have a good idea on the type of refactoring we need in our search execution infrastructure, and hope to tackle it post 1.0.

Until this feature is added, your best bet is to perform multiple searches and combine their results in the way you want, using the multi search api as suggested by Damien. This means constructing one search request per required group (in your case, setting the index/type for each search) and parsing the sort-of JSON returned.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top