@Joshua_Rich, thanks and my confusion is I just need to filter the document to match the exact price value, why and how match_all works here? Match_all makes me think returning all document -- seems not related here to find/filter for an exact value.
Remember, you are using the search API here, your filter simply acts in the context of the search. You want to filter on price, but you still need to find some documents to apply this filter to. In your case, you don't care about specific documents, like documents containing a certain string etc. So you just search against all documents (a match_all query) and then filter those for price. Later on, you might want to just filter on documents with a certain feature/quality, like those containing a certain name or piece of text. That's when you'd actually specify some kind of query, then your filter just acts on those documents.
@Joshua_Rich, thanks a lot. For your comments, "you are using the search API here", do you mean I can use other types API for the same purposes (find document whose price is 20)? Thanks.
Not really, just pointing out the fact that Elasticsearch is at its core, a way to search through your data. You ask it to find a bunch of documents, then you filter that result set and then maybe you might do some statistical analysis of it (i.e. an aggregation). This is the way it works, you can't really use it the way you want without searching.
@Joshua_Rich, thanks a lot. If I just want to match some documents based on some conditions (no need for aggregation or other kinds of statistics), e.g. price equal some values, what is the most efficient way to query? Not sure whether what I post in original post in the best (query performance perspective) solution?
What you are doing is the best way. You don't care about relevancy here, just absolutes, so using a filtered_query as you've done is the optimal way already
@Joshua_Rich, thanks for the confirmation. In the past, I may have a wrong understanding for the two queries,
If I explicitly write match_all then filter, then I think ElasticSearch will fetch all documents to client side (maybe less performance efficient dues to transfer all documents from network), then do a filter;
If I do not explicitly write match_all, then ElasticSearch is smart enough to fetch all matched documents at backend in a smarter way without transfer all documents.
Please feel free to correct me if it is not the case.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.