SearchQuery vs CriteriaQuery in spring data elasticsearch

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

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.

ok Thank you.

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.

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!

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.

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