This is my original query dsl, and total of hits was 8,981.
GET /{index}/{document}/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": {
"query": "blue shoes",
"boost": 2
}
}
},
{
"match": {
"description": {
"query": "blue shoes",
"operator": "and",
"boost": 1
}
}
}
]
}
}
}
I want to add filter to this query.
GET /{index}/{document}/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": {
"query": "blue shoes",
"boost": 2
}
}
},
{
"match": {
"description": {
"query": "blue shoes",
"operator": "and",
"boost": 1
}
}
}
],
"filter": {
"terms": {
"store.id": [ "store_a.com", "store_b.com" ]
}
}
}
}
}
Now its total of hits is 15,989(increased).
And I sort the result by score in asc(I don't know why it's asc not desc), there are documents which is scored 0.
I think there is no more filtering by query because it is already filtered.
Can I remove 0 scored documents from the result?
To add a filter, use a must clause in your bool query to add a mandatory value. Try :
GET /{index}/{document}/_search
{
"query": {
"bool": {
"must": [
"terms": {
"store.id": [ "store_a.com", "store_b.com" ]
}
],
"should": [
{
"match": {
"title": {
"query": "blue shoes",
"boost": 2
}
}
},
{
"match": {
"description": {
"query": "blue shoes",
"operator": "and",
"boost": 1
}
}
}
]
}
}
}
Related
I have an indexed job description field. I am trying to rank or order the results by number of matches.
Example, I am searching for:
friendly
honest personality
excellent communication skills
Records with most number of matches will be ranked highest.
I tried the suggested here https://stackoverflow.com/a/45319822/2445717 but did not work as what I expected.
Below is my current query:
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"match": {
"job_description": {
"query": "friendly",
"operator": "and"
}
}
},
{
"match": {
"job_description": {
"query": "honest personality",
"operator": "and"
}
}
},
{
"match": {
"job_description": {
"query": "excellent communication skills",
"operator": "and"
}
}
}
]
}
}
]
}
}
}
match_query & minimum_should_match did the trick.
posting example code in case someone will need it.
{
"query": {
"bool": {
"should": [
{
"match_query": {
"job_description": {
"query": "friendly",
"boost": 1
}
}
},
{
"match_query": {
"job_description": {
"query": "honest personality",
"boost": 1
}
}
},
{
"match_query": {
"job_description": {
"query": "excellent communication skills",
"boost": 1
}
}
}
],
"minimum_should_match": 1
}
}
}
Hello elastic experts!
I am new to elasticsearch. I am trying to build a search query with multiple or matching. I am boosting the query for different matching conditions. But here I need a bit more information. I need to know which conditions contributed to the search result. Is there any way to know which match conditions were hit by the query string?
{
"query": {
"bool": {
"should": [
{
"term": {
"title.keyword": {
"value": "Ski trip",
"boost": 1
}
}
},
{
"match_phrase_prefix": {
"title": {
"query": "Ski trip",
"boost": 0.8
}
}
},
{
"match": {
"title": {
"query": "Ski trip",
"operator": "and",
"boost": 0.6
}
}
},
{
"match": {
"description": {
"query": "Ski trip",
"boost": 0.3
}
}
}
]
}
}
}
I have some PageDocuments which I would like to search based on the title, excluding PageDocuments with a path starting with some particular text. This field is analyzed. I would like some fuzziness to help users with spelling mistakes. I need to be able to do partial matches so some would match some text and this is some text.
If I use the following query I don't get an exact match back as the first result because of tf-idf
{
"size": 20,
"query": {
"bool": {
"must": [
{
"match": {
"title": {
"query": "myterm",
"fuzziness": 1
}
}
}
],
"must_not": [
{
"wildcard": {
"path": {
"value": "/test/*"
}
}
}
]
}
}
}
So then I added a not_analyzed version of the title field at title.not_analyzed and tried adding a function score to increase the weighting of an exact match using term.
{
"query": {
"function_score": {
"functions": [
{
"weight": 2,
"filter": {
"fquery": {
"query": {
"term": {
"title.not_analyzed": {
"value": "myterm"
}
}
}
}
}
}
],
"query": {
"bool": {
"must": [
{
"match": {
"title": {
"query": "myterm",
"fuzziness": 1
}
}
}
],
"must_not": [
{
"wildcard": {
"path": {
"value": "/path/*"
}
}
}
]
}
},
"boost_mode": "multiply"
}
}
}
But this gives me the same results. How can I get the exact matches returned first?
We found a solution to this by adding a combination of should and boost.
{
"size": 20,
"query": {
"bool": {
"must": [
{
"match": {
"title": {
"query": "myterm",
"fuzziness": 1
}
}
}
],
"must_not": [
{
"wildcard": {
"path": {
"value": "/path/*"
}
}
}
],
"should": [
{
"term": {
"title": {
"value": "myterm",
"boost": 10
}
}
}
]
}
}
}
Imagine I have a document, which looks like this:
{
"Title": "Smartphones in United Kingdom",
"Text": "A huge text about the topic",
"CategoryTags": [
{
"CategoryID": 1,
"CategoryName": "Smartphone"
},
{
"CategoryID": 2,
"CategoryName": "Apple"
},
{
"CategoryID": 3,
"CategoryName": "Samsung"
}
],
"GeographyTags": [
{
"GeographyID": 1,
"GeographyName": "Western Europe"
},
{
"GeographyID": 2,
"GeographyName": "United Kingdom"
}
]
}
CategoryTags and GeographyTags are stored as nested subdocuments.
I'd be looking for "apple united kingdom" in my search bar. How'd I make a query that would boost this document if it has both matching category and geography at the same time?
I was thinking of multi_match query, but I didn't figure out how would I deal with nested documents here...
I was thinking of nesting must into should statement. Would that make any sense?
POST /_search
{
"template": {
"size": "50",
"_source": {
"include": "Title"
},
"query": {
"filtered": {
"query": {
"bool": {
"minimum_number_should_match": "2<50%",
"must": [
{
"match": {
"Text": {
"query": "{{SearchPhrase}}"
}
}
}
],
"should": [
{
"match": {
"Title": {
"query": "{{SearchPhrase}}",
"type": "phrase",
"boost": "20"
}
}
},
{
"bool": {
"must": [
{
"nested": {
"path": "CategoryTags",
"query": {
"match": {
"CategoryTags.CategoryName": "{{SearchPhrase}}"
}
}
}
},
{
"nested": {
"path": "GeographyTags",
"query": {
"match": {
"GeographyTags.GeographyName": "{{SearchPhrase}}"
}
}
}
}
]
}
}
]
}
}
}
}
}
}
I'm trying to create queries similar to kibana queries in elasticsearch lucene queries. What I'm basically trying to do is matching some phrases. For example; my kibana query looks like this:(+"anna smith") AND ( (+"university"), (+"chairman"), (+"women rights")) It searches "anna smith" as must and one of the other phrases as should(there should be at least one of them exist in the text). I wrote a query to do this but it gives "elasticsearch parse exception:expected field name but got start_object". How can I solve this. Here is my query;
{
"query": {
"bool": {
"must": {
"match": {
"text": {
"query": "anna smith",
"operator": "and"
}
}
}
},
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"match": {
"text": {
"query": "university",
"boost": 2
}
}
},
{
"match": {
"text": {
"query": "chairman",
"boost": 2
}
}
}
]
}
}]
}}}}
Your second query at the bottom cannot be there, it needs to be inside the first bool/must like this
{
"query": {
"bool": {
"must": [
{
"match": {
"text": {
"query": "anna smith",
"operator": "and"
}
}
},
{
"bool": {
"should": [
{
"match": {
"text": {
"query": "university",
"boost": 2
}
}
},
{
"match": {
"text": {
"query": "chairman",
"boost": 2
}
}
}
]
}
}
]
}
}
}