Elasticsearch with grouped query_string - elasticsearch

{
"query":
{
"query_string" :
{
"query" : "((name:the_search_phrase) OR (keywords:the_search_phrase)) AND (city:Sydney, Australia)"
}
}
}
New to elasticsearch. Building the JSON as per the documentation here: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
The query runs, however, results with city other that Sydney, Australia are returned too. Why the AND part is not working?
I want the search phrase to match against either or both name, keywords but the city should be strictly Sydney.

What you are doing is a full text query. city:Sydney, Australia seems to be a filter query. Like a WHERE clause in a SQL. You are better off using a filter query for that.
Look at the boolean query for examples,
Something like this perhaps,
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "the_search_term",
"fields": [
"name",
"keywords"
]
}
}
],
"filter": [
{
"match": {
"city": "Sydney, Australia"
}
}
]
}
}
}

Related

Elasic search: find doc by id and highlight words based on query string

I like to find an document in elastic search an highlight terms based on an query string.
Is this possible?
I tried to run an query-string elastic search and filter the result based on ID. But those sounds not very efficient, because elastic first generates an huge list of all document matched the querystring (which could by millions) an pic only one document based on the filter.
Is there a way or query-contstruct to combine querystring and "search for term in _id field" in one boolean search?
Something like this (which is not working):
"query": {
"bool": {
"must": {
"query_string": {
"query": "red*",
"fields": [
"text",
"title"
]
},
"term": {
"_id":"fda72434fa172"
}
}
}
},
"highlight": {
"fields": {
[...]
I made a small example that can be a starting point.
Use filter to perform your query and retrieve the doc by id.
Then I used match and highlight to highlight the term I want.
POST test/_doc/fda72434fa172
{
"text": "I like to find an document in elastic search an highlight terms based on an query string. Is this possible?"
}
GET test/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"_id": "fda72434fa172"
}
}
],
"must": [
{
"match": {
"text": {
"query": "elastic search"
}
}
}
]
}
},
"highlight": {
"fields": {
"text": {}
}
}
}

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

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

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.

Search in Elastic Search with AND operator

I have the following documents in my ES
name type
Thomas Robert King xyz
Peter Jacob Junior xyz
Robert Einstein Jr abc
Thomas Jupyin DR xyz
I want to get all the documents where it matches for Robert and Thomas both in a sentence and are of type xyz.
The current ES query that i have created is :
http://localhost/index/type/_search/?size=100&pretty=1/
{
"query": {
"bool": {
"must": [
{
"match":
{ "name": "Thomas" }
},
{
"match":
{ "name": "Robert" }
}
],
"must_not": [
{
{
"match":
{ "type": "abc" }
}
]
}
}
}
Is it the right way to create a AND scenario in elastic search or is there a better query to handle the same output .
I am using ES 2.1
Thanks
Given your current goal: get all the documents where it matches for Robert and Thomas both in a sentence and are of type xyz, you should not use a must_not query on abc. If there is another type ( ubv for example), your query will also get the documents which match this type.
You can do that query:
{
"query": {
"bool": {
"must": [
{
"match":
{
"name": {
"query": "Thomas Robert",
"operator": "and"
}
}
},
{
"match":
{
"type": "xyz"
}
}
]
}
}
}
By the way, the clauses must are scoring query in a bool query. If you want just want to filter on type xyz you should use:
{
"query": {
"bool": {
"must": [
{
"match":
{
"name": {
"query": "Thomas Robert",
"operator": "and"
}
}
}],
"filter": [
{
"match":
{
"type": "xyz"
}
}
]
}
}
}
Moreover please look at term level query versus full-text query, to see what you really need. If your "type" field is not analyzed, you should use term query instead of match to avoid analysis in match query.
https://www.elastic.co/guide/en/elasticsearch/reference/2.3/term-level-queries.html

Resources