How to search for a value in any nested field? - elasticsearch

Basically I have to find a matching value in all the fields in my document.
I tried using query string but unfortunately, it only searches in first-level fields. The nested field values are not fetched using query string or may be the way I am using it is wrong.
I tried
GET events/_search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "*",
"query": "match"
}
}
]
}
}
}
how will I query such a way that it checks all the fields including the nested fields.
Thanks in advance

You can always have multiple clauses, one for the first level fields, and another for nested fields combined in an OR condition:
GET events/_search
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"query_string": {
"default_field": "*",
"query": "match",
"lenient": true
}
},
{
"nested": {
"path": "nested_field",
"query": {
"query_string": {
"default_field": "nested_field.*",
"query": "match",
"lenient": true
}
}
}
}
]
}
}
}

Related

How to use multi-match query to get half search terms?

I want to find names that start or have the phrase man in them. But I also want to query on different indexes.
So it should return all fields that have the term man in them such as hoffman last name, or anything that has the term in it.
How can I achieve this?
"query": {
"multi_match": {
"query": "man",
"fields": [
"name",
"last_name",
"email"
]
}
}
}
{
"query": {
"multi_match": {
"query": "man",
"fields": [
"billing.name",
"billing.last_name",
"billing.email"
]
}
}
}
If you want to search across multiple indexes, then you can use _index field, and for searching across multiple fields you can use query_String instead of a multi-match query.
GET /_search
{
"query": {
"bool": {
"must": [
{
"terms": {
"_index": [
"index-1",
"index-2"
]
}
},
{
"query_string": {
"query": "*man*"
}
}
]
}
}
}

Many must with multi_match

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.

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 ?

Elastic Search nested query string?

I want to search 2 values in 2 different columns using wildcard but its not running as expected with 2 values but runs fine on single query_string
This works with single column
{
"query": {
"query_string" : {
"default_field" : "Phone",
"query" : "*568072*"
}
}
}
I tried to expand it to use it with 2 columns with 2 different values.
{
"query":
{
"bool":
{
"should": [
{
"query_string":
{
"query": "*Chicago*",
"fields": ["Sources"]
},
"query_string":
{
"query": "*493*",
"fields": ["Phone"]
}
}
]
}
}
}
Where am I wrong ?
You have a mistake in your query. You have one big object inside should array, that has two same keys that does not make sense. Instead it should be array of objects like this:
{
"query": {
"bool": {
"should": [
{ "query_string": { "query": "*Chicago*", "fields": ["Sources"] } },
{ "query_string": { "query": "*493*", "fields": ["Phone"] } }
]
}
}
}

ElasticSearch Multi Match with Multiple Query Parameters

We have the following multi match query in Elastic Search
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "90803",
"type": "cross_fields",
"fields": [
"POSTAL_CODE^5",
"ADDRESS",
"CITY"
],
"operator": "and"
}
}
}
}}
How can we pass multiple query parameters. For e.g. we want to pass multiple ID in the query to match against the field Postal Code.
First, is POSTAL_CODE an analyzed field? If it's not the case you could use a Terms Query:
{
"query": {
"terms" : {
"POSTAL_CODE" : ["90803", "90809"]
}
}
}
If you want to use Match for some reason, there is not a Match Query that matches several values, you have to use a Bool Query with should or must depending on your use case.
Example with must:
{
"query": {
"bool": {
"must": [{
"match": { "POSTAL_CODE": "90803" }
}, {
"match": { "POSTAL_CODE": "90809" }
}]
}
}
}

Resources