Single bool must queries in Elasticsearch - elasticsearch

Does wrapping single Elasticsearch queries in bool must queries change search results, or are the following two queries identical (both in terms of how elasticsearch processes them and what the outcome is)?
single query_string query (no bool query as wrapper):
POST _search
{
"query": {
"query_string" : { "query" : "My query string" }
}}
bool query that wrapps a single query_string query:
POST _search
{
"query": {
"bool" : {
"must" : {
"query_string" : { "query" : "My query string" }
}}}}

Both are exactly semantically the same and will produce the same results.
It's worth noting, though, that a bool query only makes sense if there are more than one clause, otherwise it's useless to specify it.

Related

What is the need for "bool" in queries with single must/should/must_not?

In Elasticsearch I can use boolean combination with Single must/should, is there any meaning to that?
This example with only one must inside bool works
GET /logstash-2021.02.25/_search
{
"query":
{
"bool":
{
"must":
[{
"match":
{
"level": "Error"
}
}]
}
}
}
In this other example without the "bool" (since there is only one must) it doesn't work
GET /logstash-2021.02.25/_search
{
"query":
{
"must":
[{
"match":
{
"level": "Error"
}
}]
}
}
ES DSL Query with bool means, Query that matches documents matching boolean combinations of other queries. The bool query maps to the underlying Elasticsearch layer of Lucene's Boolean query which makes the ES more powerful and delightful. It is built using one or more boolean clauses, each clause with a typed occurrence.
Elastic search bool query is a widely used query when it comes to a complex combination of filters. furthermore, ElasticSearch is able to run all the complex queries together in real-time and locate the most suitable results and return them to the user in a very short amount of time using the bool query

Combining results of two queries

I'm using Kibana v6.1.1 and trying to get within one GET request two different queries in order to use the "must" or "should" terms more than once.
When I run this query under "Dev Tools" in the Kibana, it works.
When I want to apply this "double query" (without the GET line of course) under "Discover"->"Add a filter"->"Edit filter"->"Edit Query DSL", it doesn't accept the syntax {} in order to create an 'OR' between the queries.
It is necessary that these two "must" terms will be separated but stay in the same filter.
GET _my_index/_search
{
"query" : {
"bool" : {
"must" : [{
...
}]
}
}
}
{}
{
"query" : {
"bool" : {
"must" : [{
...
}]
}
}
}
P.S.
Using the simple_query_string doesn't seem to solve the problem and so far, I couldn't find the way to combine these two queries.
I'm not sure what you actually want to achieve. Use the following if at least one of the shoulds has to match (there is an implicit minimum_should_match if there are no other conditions, but you can also set an explicit value for that):
{
"query" : {
"bool" : {
"should" : [
{
...
},
{
...
}
]
}
}
}
If you want to run independent queries, use a multi search.

difference between match and query_string

What is the difference between match query and query string query? Say, I have the following queries, do they have the same functionality?
GET /_search
{
"query": {
"match" : {
"_all" : "this is a test"
}
}
}
and:
GET /_search
{
"query": {
"query_string" : {
"query" : "this is a test",
}
}
}
Considering the fact that using query_string and not indicating any specific field, automatically _all field will be used
From elasticsearch documentation
Comparison match query to query_string / field
The match family of queries does
not go through a "query parsing" process. It does not support field
name prefixes, wildcard characters, or other "advanced" features. For
this reason, chances of it failing are very small / non existent, and
it provides an excellent behavior when it comes to just analyze and
run that text as a query behavior (which is usually what a text search
box does). Also, the phrase_prefix type can provide a great "as you
type" behavior to automatically load search results.

fuzzy searching with query_string Elasticsearch

i have a record saved in Elasticsearch which contains a string exactly equals to Clash of clans
now i want to search this string with Elasticsearch and i using this
{
"query_string" : {
"query" : "clash"
}
}
its working perfectly but now if i write
"query" : "class"
it dont give me back any record so i realize i should use Fuzzy searching so i come to know that i can use fuzziness parameter with query_string so i did
{
"query_string" : {
"query" : "clas"
"fuzziness":1
}
}
but still elasticsearch is not returning anything!
kindly help and i cant use Fuzzy query i just can use query_string.
Thanks
You need to use the ~ operator to have fuzzy searching in query_string:
{
"query": {
"query_string": {
"query": "class~"
}
}
}

Elastic Search boost query corresponding to first search term

I am using PyElasticsearch (elasticsearch python client library). I am searching strings like Arvind Kejriwal India Today Economic Times and that gives me reasonable results. I was hoping I could increase weight of the first words more in the search query. How can I do that?
res = es.search(index="article-index", fields="url", body={
"query": {
"query_string": {
"query": "keywordstr",
"fields": [
"text",
"title",
"tags",
"domain"
]
}
}
})
I am using the above command to search right now.
split given query into multiple terms. In your example it will be Arvind, Kejriwal... Now form query string queries(or field query or any other which fits into the need) for each of the given terms. A query string query will look like this
http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/query-dsl-query-string-query.html
{
"query_string" : {
"default_field" : "content",
"query" : "<one of the given term>",
"boost": <any number>
}
}
Now you have got multiple queries like above with different boost values(depending upon which have higher weight). Combine all of those queries into one query using BOOL query. http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
If you want all of the terms to be present in the result, query will be like this.
{
"bool" : {
"must" : [q1, q2, q3 ...]
}
}
you can use different options of bool query. for example you want any of 3 terms to present in result then query will be like
{
"bool" : {
"should" : [q1, q2,q3 ...]
},
"minimum_should_match" : 3,
}
theoretically:
split into terms using api
query against terms with different boosting
Lucene Query Syntax does the trick. Thanks
http://lucene.apache.org/core/2_9_4/queryparsersyntax.html#Boosting%20a%20Term

Resources