How to do group by multiple fields and sort on a different field, in elasticsearch - elasticsearch

Lets say I have a query like
Select * from table
Group By field_1, field_2, field_3
Order By filed_7;
How to achieve the same functionality in elasticsearch.
I use Composite Aggregations to do Group by multiple fields. Is it possible to do the sorting part?

Related

What's the queivalent way to mysql's select as in Elasticsearch?

How to rename the query response field name just like mysql's select as query in Elasticsearch?
Note this is on the query not influence the index process which is different from this question

Elasticsearch: can you use the results of aggregates in the same search?

we have an Elasticsearch index with about 50000 "product" entries per user over which our app performs complex queries. Each of those entries has a corresponding "supplier" and "supervisor". The suppliers and supervisors are stored in their own indices, and there are only ~200 of each per user. They are big documents, so in the product index we store just their name and ID, which are the only things used in queries over products. However, on each product query we would also like to return aggregate information about suppliers and supervisors. Example: if the query returns 800 products and they have 10 different suppliers and 12 different supervisors, I want information on those. I know how to use bucket aggregates over their IDs, (or names treated as keyword). However these results return only their ID or name. Is there any way to retrieve all the information from the supplier and supervisor documents using these aggregate IDs on the same query? Or do I have to perform a second query?

elastic DSL query result from another query

can anyone help me to translate below SQL query to ELasticSearch DSL form for document selections?
SELECT * FROM table1 WHERE source_id IN (select source_id FROM table2)
This JOIN semantics does not translate to Elasticsearch. If your index had parent/child mapping - you might be able to achieve what you want to do. Another option is to do a two-pass query.
This blog post gives an idea of how relations are modeled in Elasticsearch - Managing Relations in Elasticsearch

mongodb-java-driver how to group multiple fields?

Does mongodb-java-driver group only support one field? e.g.
group("$customerId", sum("totalQuantity", "$quantity"), avg("averageQuantity", "$quantity"))
if group with multi fields, how to implement? e.g.
"_id":{platform:"$platform",screen_name:"$screen_name"}
I tried below manner, but it does not work
group("{platform:\"$platform\",screen_name:\"$screen_name\"}", sum("totalQuantity", "$quantity")
So how to support multiple group field in mongodb-java-driver?

Elasticsearch which is the better aggregation

I need to run a an aggregation which is to get the unique ids for a day where the ids in the specific set. So the SQL query will be like
SELECT count (DISTICT my_field) FROM my_table WHERE time BETWEEN '2015-02-13 00:00:00' AND '2015-02-13 23:59:59' AND my_field IN (value1,value2,value3....value n)
I am now using terms aggregation, and counting the number of buckets. But it is resulting in wrong counts and I am getting the bucket count as the number of values for "my_field" I specified in the query string. So is there any better method for doing this ?
By the way, I have over 4k values which need to be checked against for a day. I mean in the where condition.
You can use Cardinality Aggregation along with Filter Aggregation to get what you need. In the filter section of filter aggregation, encode your where clause and add the cardinality aggregation to get the count of distinct values of my_field field.

Resources