There is a backend for Node.js + Elasticsearch, and I want to find and get only the unique values ​​of the two fields, as it is easier to do, there is a manufacturer and a device model, since there are many duplicates; you need to return only unique pairs of model / manufacturer, or ideally, of course, arrays, for example {producer1: {model1, model2}, producer2: {model1, model2}} you can of course do this on the server or even on the client, but I would like to wrap everything up on Elasticsearch.

2 answers 2

You can do this with the help of aggregation functions. For your case it will look like this:

'aggregations' => [ 'manufacturer_list' => [ 'terms' => [ 'field' => 'manufacturer_filed', ], 'aggregations' => [ 'model_list' => [ 'terms' => [ 'field' => 'model_field' ] ] ] ] ] 
  • I understood what I was doing wrong, but there is one nuance, I have gaps in the names and the aggregation goes by the words, that is, if there is "Xerox 5222" then in "aggregations" there will be two values ​​"Xerox" and "5222", force to aggregate considering full value match - pnp2000
  • for the field in the index, specify index: not_analyzed PUT mapping API similar problems ru.stackoverflow.com/questions/485619 and ru.stackoverflow.com/questions/456072 - Yuriy
  • I already read a little, I even found an interesting article qbox.io/blog/elasticsearch-aggregation-custom-analyzer , but I was interrupted and I haven’t figured out the mapping yet, that is, I understand correctly that this is a type of field setting in the database, not search property? - pnp2000
  • Yes, this is the way to specify the server so that it does not use a text analyzer for this field, which parses into words. - Yuriy
  • In general, I tried, it does not work, I do a mapping on all fields and it produces all the "Model" and "1" instead of "Model-1". Already xs what to do with it, and through Sense and through Curl I tried to score a mapping, all to no purpose - pnp2000

For 'terms' => ['field' => 'manufacturer_filed'] and 'terms' => ['field' => 'model_field'] , size: 0 must be specified, otherwise the aggregation will take place only in 10 items.