# SearchQuery vs CriteriaQuery in spring data elasticsearch

**URL:** <https://discuss.elastic.co/t/searchquery-vs-criteriaquery-in-spring-data-elasticsearch/46792>\
**Category:** Elasticsearch\
**Created:** [April 8, 2016, 10:11am UTC](https://discuss.elastic.co/t/searchquery-vs-criteriaquery-in-spring-data-elasticsearch/46792 "2016-04-08T10:11:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Visva\_Ram](https://avatars.discourse-cdn.com/v4/letter/v/c68b51/32.png) [@Visva\_Ram](https://discuss.elastic.co/u/Visva_Ram)\
**Post date:** [April 8, 2016, 10:11am UTC](https://discuss.elastic.co/t/searchquery-vs-criteriaquery-in-spring-data-elasticsearch/46792/1 "2016-04-08T10:11:38Z")

</div>

Hi, I totally confused in elasticsearch query method. Actually Here i hearing the words Query,Filtered and Search while i tried to learn through terminal and also i confused in spring data elastic search with SearchQuery and CriteriaQuery object. Any one can you explain the difference of those terms in the elasticsearch.

1. Query vs Filtered vs Bool vs Search ?
2. CriteriaQuery vs SearchQuery object in spring data elasticsearch? which one is best for communcation?

Note: I have learned mongodb, so I just tried to understand elasticsearch from that only

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [April 8, 2016, 11:46am UTC](https://discuss.elastic.co/t/searchquery-vs-criteriaquery-in-spring-data-elasticsearch/46792/2 "2016-04-08T11:46:31Z")

</div>

I can do the elasticsearch half.

A query is a thing that selects documents. They can all be described with  
json. The first place you will see one is in the "query" field of the  
"\_search" API.

A filter is just a query that doesn't attempt to rank documents based on  
how well the document matched. In 1.x filters were entirely separate from  
queries. In 2.x a filter is just a query used in a place where the score  
isn't important. The simplest way to make that happen is to wrap a query in  
a "constant score" query.

Bool is a specific kind of query for doing boolean logic. It supports  
arrays of queries in three fields: must, should, and must\_not. Should is  
funky because another field, minimum\_should\_match controls how many queries  
in should must match for the document to match the whole bool query. The  
reason we use this funky bool thing rather than and/or/not is that it gives  
you more power to influence the score of the documents and it is the way  
the search is actually performed. It is "closer to the metal".

Search is just an API in elasticsearch that executes queries and returns  
documents or aggregations. It is a big, complicated API that makes multiple  
round trips to various nodes in the cluster, but it is just an API from a  
user perspective.

---

<div class="post-metadata">

**Author:** ![Visva\_Ram](https://avatars.discourse-cdn.com/v4/letter/v/c68b51/32.png) [@Visva\_Ram](https://discuss.elastic.co/u/Visva_Ram)\
**Post date:** [April 11, 2016, 4:20am UTC](https://discuss.elastic.co/t/searchquery-vs-criteriaquery-in-spring-data-elasticsearch/46792/3 "2016-04-11T04:20:50Z")

</div>

ok Thank you.

---

<div class="post-metadata">

**Author:** ![Visva\_Ram](https://avatars.discourse-cdn.com/v4/letter/v/c68b51/32.png) [@Visva\_Ram](https://discuss.elastic.co/u/Visva_Ram)\
**Post date:** [April 13, 2016, 7:15am UTC](https://discuss.elastic.co/t/searchquery-vs-criteriaquery-in-spring-data-elasticsearch/46792/4 "2016-04-13T07:15:50Z")

</div>

I want to know difference of following queries and what it exactly doing.  
**Query 1:**

> '{  
> "query":{  
> "filtered" : {  
> "query" : {  
> "match\_all" : { }  
> },  
> "filter" : {  
> "and" : {  
> "filters" : [ {  
> "term" : {  
> "message" : "visva"  
> }  
> } ]  
> }  
> }  
> }  
> }  
> }'

\*\* Query 2:\*\*

> {  
> "query":{  
> "filtered" : {  
> "query" : {  
> "term":{"message":"visva"}  
> },  
> "filter" : {  
> "and" : {  
> "filters" : [ {  
> "term" : {  
> "message" : "visva"  
> }  
> } ]  
> }  
> }  
> }  
> }  
> }

**Query 3:**

> {  
> "query":{  
> "filtered" : {  
> "query" : {  
> "term":{"message":"kannan"}  
> },  
> "filter" : {  
> "and" : {  
> "filters" : [ {  
> "term" : {  
> "message" : "visva"  
> }  
> } ]  
> }  
> }  
> }  
> }  
> }

**Query 4:**  
use previous example - remove only filter part

\*\*Query 5:  
use previous example - remove only query part

what will happen for previous. This will help me to understand the query & Filter.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [April 13, 2016, 12:09pm UTC](https://discuss.elastic.co/t/searchquery-vs-criteriaquery-in-spring-data-elasticsearch/46792/5 "2016-04-13T12:09:09Z")

</div>

Next time wrap your code block in ``` so they are easier to read please.

In all the examples above your use of `and` isn't required and just makes it harder to read. Also, `and` is deprecated in favor of `bool` because `bool` is closer to the way Lucene is implemented.

In Query 1 you'll get all messages containing the term `visva` in the message field. Terms come from analyzing the text, breaking it into words and transforming the words. If you don't specify anything in the mapping you'll get an analyzer that basically breaks on spaces. It is more complicated than that because it has rules for breaking around each character in languages that don't break on spaces, but if you think of it breaking on spaces you are generally ok.

In Query 2 you'll get all messages containing the term `visva` in the message field sorted by "how good" the term matches. The filter in this query is pretty redundant.

In Query 3 you'll get all messages containing the terms `visva` and `kannan` in the message but sorted only based on the term `kannan`.

Query 4 is the same idea as query 2. Query 5 is the same idea as query 1. Play with it and see!

---

<div class="post-metadata">

**Author:** ![Visva\_Ram](https://avatars.discourse-cdn.com/v4/letter/v/c68b51/32.png) [@Visva\_Ram](https://discuss.elastic.co/u/Visva_Ram)\
**Post date:** [April 13, 2016, 12:54pm UTC](https://discuss.elastic.co/t/searchquery-vs-criteriaquery-in-spring-data-elasticsearch/46792/6 "2016-04-13T12:54:12Z")

</div>

Oh thank you. Now i am getting following doubts.

Is Query and Filter doing the same Job?  
If It is doing the same job whether can I use any one for my devleopment?  
If It is not doing the same job, where do i can use query and where do i can use filter?  
if it is not doing the same job, What is the difference?

from your answser, you saying sorting will happening only for quering field. what you mean by sorting here.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [April 13, 2016, 1:21pm UTC](https://discuss.elastic.co/t/searchquery-vs-criteriaquery-in-spring-data-elasticsearch/46792/7 "2016-04-13T13:21:18Z")

</div>

> [@Visva\_Ram](#):
>
> Is Query and Filter doing the same Job?

Yes. In 2.x and onwards they are merged and there are only queries. If you use a query in a context where the score doesn't matter (like `filtered`) then it won't calculate the score.

The default sorting that Elasticsearch does is by score - this describes how score is calculated but it is a bit out of date (written for 1.x): [Theory Behind Relevance Scoring | Elasticsearch: The Definitive Guide [2.x] | Elastic](https://www.elastic.co/guide/en/elasticsearch/guide/current/scoring-theory.html)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:59pm UTC](https://discuss.elastic.co/t/searchquery-vs-criteriaquery-in-spring-data-elasticsearch/46792/8 "2017-07-05T22:59:37Z")

</div>


