ElasticSearch Multi Match with Multiple Query Parameters - elasticsearch

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" }
}]
}
}
}

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*"
}
}
]
}
}
}

How to search for a value in any nested field?

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
}
}
}
}
]
}
}
}

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 ?

Elasticsearch filter with multi_match

I'm trying to write a query in ElasticSearch where I combine multi_match with filter for an id or a number og ids.
This is what i have so far:
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "Kasper",
"fields": ["name", "first_name", "last_name"]
}
},
"filter": {
"term": {
"user_id": "ea7528f0-1b8a-11e8-a492-13e39bbd17cb"
}
}
}
}
}
The "must" part of the query works perfectly, and when I run it alone, I get two results.
When I pick out the "user_id" from one of the two results and adds the "filter" part of the query with that id, I get nothing.
What I really want to do is have something like in SQL where user_id in ('id1', 'id2'), so the filtering would be something like:
...,
"filter": {
"terms": {
"user_id": ["ea7528f0-1b8a-11e8-a492-13e39bbd17cb"]
}
}
Did I misunderstand something here?
I'm guessing that this is because user_id field is treated as a text and is analyzed. You should use keyword type in this situation (you need just change the mapping of user_id field.
Another way (if you are on Elasticsearch 5+) you can search in keyword subfield. Just try use below query:
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "Kasper",
"fields": ["name", "first_name", "last_name"]
}
},
"filter": {
"term": {
"user_id.keyword": "ea7528f0-1b8a-11e8-a492-13e39bbd17cb"
}
}
}
}
}
I only changed "user_id" to "user_id.keyword" in your query.

Elastic search query according to MySQL Group by clause

I am using Elasticsearch I want to write a query for getting unique record on the basis of query and group:
SELECT * from users where name='%john%', age='21', location='New York' group by name
Could you please let me know how to write the query in elasticsearch with query.
It would go something like this. You need a filtered query with a query_string in the query part to match *john* and then two filters in the filter part to match the age and the location. Finally, the grouping is achieved using a terms aggregation.
{
"query": {
"query": {
"query_string": {
"query": "*john*",
"default_field": "name"
}
},
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"age": 21
}
},
{
"term": {
"location": "New York"
}
}
]
}
}
}
},
"aggs": {
"group_by_name": {
"terms": {
"field": "name"
}
}
}
}

Resources