Many must with multi_match - elasticsearch

I have this query:
{
"query": {
"bool": {
"must": [
{
"match": {
"egyik": {
"query": "piros alma"
}
}
},
{
"match": {
"masik": {
"query": "piros alma"
}
}
}
]
}
}
}
It's not too beautiful, because the query parameter occured twice, therefore I tried to rewrite it with the multi_match syntax:
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "piros alma",
"fields": [
"egyik",
"masik"
]
}
}
}
}
}
But it returns more hits than the first. I tried operator, minimum_should_match modifiers, but not helps. How do I solve the same result with multi_match?

As far as I know, all types of multi-match queries return a hit when the provided query matches any of the listed fields (see Elastic docs). Therefore, the reason why you have more hists with multi_match is that you can't enforce the same boolean condition you have with your first query. That said, I don't see anything wrong with repeating the same query parameter twice. If you want to generalise it a bit, you might want to consider using Search Templates

By default operator OR is used, which means query term can be present in any field, if you want query term to be present in all the fields then you can explicitly define operator field with AND value.
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "piros alma",
"fields": [
"egyik",
"masik"
],
"operator":"and"
}
}
}
}
}
To know more you can go through this

Meanwhile I found the solution:
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "piros alma",
"fields": [
"egyik",
"masik"
],
"type": "cross_fields",
"operator": "and"
}
}
}
}
}
Need the type and operator together.

Related

Combining filter and must in elasticsearch

What is the difference between adding a query filter inside a must and having a query filter and a must separately?
I need to apply a filter query to a search for but either of these two queries works the same for me. I would like to know if there are any differences.
Case 1:
"query": {
"bool": {
"must": [
{
"term": {
"field": {
"value": "VALUE"
}
}
},
{
"bool": {
"filter": [
{
"script": {
"script": {
"source": """
return true;
"""
}
}
}
]
}
}
]
}
}
Case 2:
"query": {
"bool": {
"must": [
{
"term": {
"field": {
"value": "VALUE"
}
}
}
],
"filter": [
{
"script": {
"script": {
"source": """
return true;
"""
}
}
}
]
}
}
In my opinion they do not differ, but I need references. Greetings.
Both the query will work exactly the same
Refer to documentation on the boolean query to know more about your structure
must: The clause (query) must appear in matching documents and will
contribute to the score.
filter: The clause (query) must appear in matching documents. However
unlike must the score of the query will be ignored. Filter clauses are
executed in filter context, meaning that scoring is ignored and
clauses are considered for caching.
Structure of your first query where multiple bool queries are combined:
{
"query": {
"bool": {
"must": [
{
"term": {},
"bool": {
"filter": {
"script": {}
}
}
}
]
}
}
}
Structure of your second query that includes single bool query:
{
"query": {
"bool": {
"must": [
{
"term": {}
}
],
"filter": [
{
"script": {}
}
]
}
}
}
As you can see, in both the search queries the document will match only when both the term query and script query condition is satisfied
They both will work exactly the same, second one would be preferred syntax because it's not as nested as first one and easier to read.

ElasticSeach combine multi_match and match_phrase

I use ES 7, I want to search over multi fields, but on this field (title) must be shown firstly if it matches exactly. For now I tried :
{
"query": {
"bool": {
"must": {
"bool": {
"should": [
{
"match_phrase": {
"titre": {
"query": "test",
"boost": "20"
}
}
},
{
"multi_match": {
"fields": ["titre", "description^4", "subtitle^3"],
"query": "test",
"type": "most_fields"
}
}
]
}
}
}
}
}
It works, but I would like to order the match_phrase before other results.
The idea is the user type the exact phrase of a title, this result will appear before other based on multi_match.
Is it possible ?

Relevance by type on same field in elasticsearch

Is there any way to boost search results on same field depending on type?
My basic boosting is something like:
GET _search
{
"query": {
"simple_query_string": {
"query": "mangan",
"fields":["_all", "title^6"]
}
}
}
But for some other documents I want title to be less important, so I tried to prefix it with type:
GET _search
{
"query": {
"simple_query_string": {
"query": "mangan",
"fields":[
"_all",
"DocumentationPage.title^6",
"DocumentationPage.title^6"]
}
}
}
But then it does not boost at all. As a last resort I could use Funcsion/Script Score bu would like to avoid it.
For sake of example, assume that document contains just title field.
A simple way to achieve this is re-writing the query in the OP as a dis-max query.
Example for elasticsearch 5.x:
{
"query": {
"dis_max": {
"queries": [
{
"simple_query_string": {
"fields": [
"_all"
],
"query": "mangan"
}
},
{
"bool": {
"filter": {
"type": {
"value": "DocumentationPage"
}
},
"must": [
{
"simple_query_string": {
"fields": [
"title^6"
],
"query": "mangan"
}
}
]
}
}
]
}
}
}

NOT condition in elasticsearch

I am trying to implement NOT condition in elasticsearch query.
Can I Implement filter inside bool or I need to write separate
filter as below. Any optimum solution is there?
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "fashion"
}
},
{
"term": {
"post_status": "publish"
}
}
]
}
},
"filter": {
"not": {
"filter": {
"term": {
"post_type": "page"
}
}
}
}
}
You can use a must_not clause:
{
"query": {
"bool": {
"must": [
{
"match": {
"_all": "fashion"
}
},
{
"term": {
"post_status": "publish"
}
}
],
"must_not": {
"term": {
"post_type": "page"
}
}
}
}
}
Also, I'd recommend using a match filter instead of query_string, as query_string requires the much more strict Lucene syntax (and is therefor more error prone), whereas match works more like a search box: it will automatically transform a human readable query to a Lucene query.

Give different Boost to queries under an OR condition filter

Is there a way to give a different boost to queries in an OR condition filter??? I will want to see the results that match with the first query over the ones that match the second query.
This is part of my query:
{
"or": [
{
"query": {
"query_string": {
"query": "(field_state:ALL OR field_state:'WA') AND type:incentive"
}
}
},
{
"query": {
"query_string": {
"query": "* AND NOT (type:adcampaign OR type:incentive OR type:page_simple OR type:page OR type:glossary_item OR type:product OR type:lighting_option)"
}
}
}
]
}
Based on the ES docs here (http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query/), yes you can. boost is a listed option for the query_string query type.
While untested, I imagine something like this will do the trick:
{
"or": [
{
"query": {
"query_string": {
"query": "(field_state:ALL OR field_state:'WA') AND type:incentive",
"boost": 10.5
}
}
},
{
"query": {
"query_string": {
"query": "* AND NOT (type:adcampaign OR type:incentive OR type:page_simple OR type:page OR type:glossary_item OR type:product OR type:lighting_option)"
}
}
}
]
}

Resources