Elastic Search Query to get results for multiple keywords(i.e Country name) - elasticsearch

Key String will be like
"india,singapore" without quotes.
How to split and search the keyword
Expected result will be match the country with india or singapore.
So far i tried..
{
"_source": "country_name",
"query": {
"bool": {
"must": [
{
"term": {
"country_name.keyword": "india,singapore"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10,
"sort": [],
"aggs": {}
}
But it will showing only those content have match the exact key string "india,singapore"

you can use terms query in place of term query like below:
{
"_source": "country_name",
"query": {
"bool": {
"must": [
{
"terms": {
"country_name.keyword": ["india","singapore"]
}
}
]
}
},
"from": 0,
"size": 10
}

Related

Filter with querystring in elasticsearch

I send this query and it works fine. It returns filtered data:
{
"query": {
"bool": {
"filter": [
{
"match": {
"lang": "en"
}
}
]
}
},
"size": 10,
"from": 0,
"sort": []
}
If I want to search with searchstring then it workst fine too:
{
"query": {
"query_string": {
"query": "big size"
}
},
"size": 10,
"from": 0,
"sort": []
}
But I can't get data from elastic by filter and searchstring together:
{
"query": {
"query_string": {
"query": "big size"
},
"bool": {
"filter": [
{
"match": {
"lang": "en"
}
}
]
}
},
"size": 10,
"from": 0,
"sort": []
}
I receive next error:
Error 400.
{"error":{"root_cause":[{"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":76}],"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":76},"status":400}
Your query needs to be restructured as shown below.
Query:
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "big size"
}
}
],
"filter": [
{
"match": {
"lang": "en"
}
}
]
}
},
"size": 10,
"from": 0,
"sort": []
}

Elasticsearch wildcard search: when I add space to a query everything falls apart

I have a English dictionary Index, I search fields with the following JSON
GET /words/_search
{
"from": 0,
"size": 10,
"query": {
"bool": {
"filter": [
{
"bool": {
"should": [
{
"query_string": {
"default_field": "text",
"query": "*orhan*"
}
}
]
}
}
]
}
},
"track_total_hits": true
}
My query purpose is to get all records if they include orhan name, and after running the query I get the results as expected;
_id
_idex
_score
_type
text
B6F1eoQBu3ncIuw4CyKL
words
0.0
_doc
orhan
cKN5eoQBu3ncIuw4JgxK
words
0.0
_doc
vorhand
drDzzYQBu3ncIuw4vn10
words
0.0
_doc
orhan second word
I modify my query and I try the search orhan s but everything falls apart, the whole 54665 records were shown to me.
###
"bool": {
"should": [
{
"query_string": {
"default_field": "text",
"query": "*orhan s*" // <- modified
}
###
I can't add the whole response :) But I can provide,
Response of total value:
"total": {
"value": 54665,
"relation": "eq"
},
My response shouldn't include the whole record just related records shown to me
Query: "query": "*orhan s*"
Response:
_id
_idex
_score
_type
text
drDzzYQBu3ncIuw4vn10
words
0.0
_doc
orhan second word
That's because the default operator is an OR, so you are catching all the words finishing with orhan OR starting with s.
You can change the operator:
GET /words/_search
{
"from": 0,
"size": 10,
"query": {
"bool": {
"filter": [
{
"bool": {
"should": [
{
"query_string": {
"default_field": "text",
"query": "*orhan s*",
"default_operator": "AND"
}
}
]
}
}
]
}
},
"track_total_hits": true
}
Or add the operator to the query directly:
GET /test_words/_search
{
"from": 0,
"size": 10,
"query": {
"bool": {
"filter": [
{
"bool": {
"should": [
{
"query_string": {
"default_field": "text",
"query": "*orhan AND s*"
}
}
]
}
}
]
}
},
"track_total_hits": true
}

Elasticsearch multiple fields wildcard bool query

Currently using bool query which searches for a combination of both input words or either one of input word on field "Name". How to search on multiple fields using wild cards?
POST inventory_dev/_search
{"from":0,"query":{"bool":{"must":[{"bool":{"should":[{"term":{"Name":{"value":"dove"}}},{"term":{"Name":{"value":"3.75oz"}}},{"bool":{"must":[{"wildcard":{"Name":{"value":"*dove*"}}},{"wildcard":{"Name":{"value":"*3.75oz*"}}}]}}]}}]}},"size":10,"sort":[{"_score":{"order":"desc"}}]}
You can use query_string in place of wildcard query, to search on multiple fields
{
"from": 0,
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"term": {
"Name": {
"value": "dove"
}
}
},
{
"term": {
"Name": {
"value": "3.75oz"
}
}
},
{
"bool": {
"must": [
{
"query_string": {
"query": "*dove*",
"fields": [
"field1",
"Name"
]
}
},
{
"query_string": {
"query": "*3.75oz*",
"fields": [
"field1",
"Name"
]
}
}
]
}
}
]
}
}
]
}
},
"size": 10,
"sort": [
{
"_score": {
"order": "desc"
}
}
]
}

Elasticsearch Ranking on aggregation based on AND and OR

I have a query with multiple keywords with an aggregation on author ID.
I want the ranking to be based on combining must and should.
For example for query 'X', 'Y' the authors containing both 'X' and 'Y' in the document field should be ranked higher, followed by authors who have either 'X' or 'Y'.
Doing each of them (AND/OR) is easy, I need the idea/direction how to achieve both in one ES query.
The current query I have for both X and Y is:
GET /docs/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "X",
"fields": [
"fulltext"
],
"default_operator": "AND"
}
},
{
"query_string": {
"query": "Y",
"fields": [
"fulltext"
],
"default_operator": "AND"
}
}
]
}
},
"aggs": {
"search-users": {
"terms": {
"field": "author.id.keyword",
"size": 200
},
"aggs": {
"top-docs": {
"top_hits": {
"size": 100
}
}
}
}
}
}
Changing must to should change it to OR but I want the combination of both ranking authors with must a higher ranking in aggregation.
The usual way of boosting results is by adding a should clause looking for both terms, like this.
GET /docs/_search
{
"size": 10,
"query": {
"bool": {
"should": [
{
"match": {
"fulltext": "X Y",
"operator": "AND"
}
}
],
"must": [
{
"match": {
"fulltext": "X Y",
"operator": "OR"
}
}
]
}
},
"aggs": {
"search-users": {
"terms": {
"field": "author.id.keyword",
"size": 200
},
"aggs": {
"top-docs": {
"top_hits": {
"size": 100
}
}
}
}
}
}

Query on multiple range of document

What I want to search is to extract documents among certain range of documents, not the whole documents. I know ids of documents. For example, I want to query matching some sentences with query field - 'pLabel' among the documents ids of which I know via different process. My trial is as below but I got bunch of documents which is different with my expectation.
For example, in such documents as eid1, eid2...etc groups, I want to query filtering out the matching documents out of the groups (eid1, eid2, eid3, ...). Query is shown as below.
How I fix query statement to get the right search result?
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "pLabel" ,
"query": "search words here"
}
}
] ,
"must_not": [] ,
"should": [
{
"term": {
"eid": "eid1"
}
} ,
{
"term": {
"eid": "eid2"
}
}
]
}
} ,
"size": 0 ,
"_source": [
"eid"
] ,
"aggs": {
"eids": {
"terms": {
"field": "eid" ,
"size": 1000
}
}
}
}
You need to move the should clause of the Doc IDs inside the must clause.
Right now the query can return any document that matches the query_string clause, it'll only prefer docs that matches the Doc IDs.
Also, you should use terms query
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "pLabel",
"query": "search words here"
}
},
{
"terms": {
"user": ["eid1", "eid2"]
}
}
]
}
},
"size": 0,
"_source": [
"eid"
],
"aggs": {
"eids": {
"terms": {
"field": "eid",
"size": 1000
}
}
}
}

Resources