elasticsearch query to search 2 different queries on same fields and get results which have the combination of both queries - elasticsearch

I have 2 parts of query
(a) query
(b) context,
I want to make a query to elasticsearch which give me results on (a), this is mandatory, and the results which have (b) should be boosted.
for example
if the query/(a) is "Inflation rates" and context/(b) is "United States" -- the results should give results only on "Inflation rates" and the results with "United States" should be boosted.
I have tried various concepts of elasticsearch like - bool/should/filter/ but the results are not as exepected.
below I am giving the query template that I am using currently. It gives me results in which query and context are not combined, its just gives docs which either match query or context.
I am using ES 2.4.0
{
"template":{
"from":"0",
"size":"10",
"_source": ["*"],
"query":{
"function_score": {
"query":{
"bool":{
"should":[{
"match":{
"display":{
"query": "inflation rate",
"boost": 2
}
}
},
{
"match":{
"attributes.definition": {
"query": "inflation rate",
"boost": 5
}
}
},
{
"match":{
"attributes.topics.name": {
"query": "inflation rate",
"boost": 5
}
}
},
{
"match":{
"attributes.titles": {
"query": "inflation rate",
"boost": 7
}
}
},
{
"match":{
"attributes.definition": {
"query": "United States",
"boost": 4
}
}
},
{
"match":{
"attributes.topics.name": {
"query": "United States",
"boost": 4
}
}
},
{
"match":{
"attributes.titles": {
"query": "United States",
"boost": 4
}
}
}],
"must": [
{
"match":{
"type":"news"
}
}]
}
},
"functions": [{
"gauss": {
"attributes.published_ts": {
"scale": "10d"
}
},
"weight" : 0.1
}]
}
}
}
}

Should be something like this:
e.g.
{
"query": {
"function_score": {
"filter": {
"bool": {
"must": [{ //here can be should, must etc ... This is your first condition.
"match": {
"attributes.definition": {
"query": "inflation rate",
"boost": 5
}
}
}, {
"match": {
"attributes.topics.name": {
"query": "inflation rate",
"boost": 7
}
}
}]
}
},
"functions": [{ //This is your second condition. Just to give more relevance to the topics with United States.
"filter": {
"term": {
"attributes.topics.name": "United States"
}
},
"weight": 3
}, {
"field_value_factor": {
"field": "views",
"modifier": "log1p"
}
}],
"score_mode": "sum"
}
}
}
Is just an example, adapt it to your own requirements.

Related

Why does Elasticsearch score these documents the way it does?

I have a query where I'm trying pull documents out of my index and sort them by a date. Additionally, if the document's ID matches a provided one then I boost that result.
When I run my query I'm noticing that some of the documents with a more recent sort date are not at the top of the results because Elasticsearch is giving them a different score than other documents. As a result my result order is incorrect. I don't see anything in my query that could be affecting the score. Anyone have any idea what's happening?
Here's the query I'm using:
{
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"match": {
"language.keyword": {
"query": "english",
"operator": "OR",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"functions": [
{
"filter": {
"match": {
"id": {
"query": "ID1",
"operator": "OR",
"boost": 1
}
}
},
"weight": 10
}
],
"score_mode": "multiply",
"boost_mode": "multiply",
"boost": 1
}
},
"sort": [
{
"_score": {
"order": "desc"
}
},
{
"sortDate": {
"order": "desc"
}
}
]
}

Source filtering an array of objects in ElasticSearch

Here is a document in ElasticSearch
"CompanyId": 5733,
"PartNumber": "W8S038",
"Name_en": "#8 Washer, M4 Compatible, Stainless Steel, Pack of 100",
"ProductId": 90023,
"CompanyName": "Washers Ltd",
"Prices": [
{
"BuyerId": 308,
"Price": 2.42
}
,
{
"BuyerId": 406,
"Price": 2.22
}
]
}
Obviously we can't let on to buyer 308 that buyer 406 is getting a better price. Therefore when buyer 308 is searching I need to remove all of the prices for other buyers.
I'd like to do this by using source filtering. But how?!
(I could exclude Prices and add back in the required price by using a script_field. However, that means that the price is not part of the source document and therefore ReactiveSearch can't see it and therefore can't sort on it.)
Update: here is the query generated by ReactiveSearch to which I need to append the limit on prices:
"query":{
"bool":{
"must":[
{
"bool":{
"must":[
{
"bool":{
"must":[
{
"bool":{
"should":[
{
"multi_match":{
"query":"m4 washer",
"fields":[
"Name_en"
],
"type":"cross_fields",
"operator":"and"
}
},
{
"multi_match":{
"query":"m4 washer",
"fields":[
"Name_en"
],
"type":"phrase_prefix",
"operator":"and"
}
}
],
"minimum_should_match":"1"
}
}
]
}
}
]
}
}
],
"filter": [
{
"nested": {
"path": "Prices",
"query": {
"term": {
"Prices.CompanyId": 1474
}
},
"inner_hits": {}
}
}
]
}
},
"size":10,
"aggs":{
"CompanyName.raw":{
"terms":{
"field":"CompanyName.raw",
"size":1000,
"order":{
"_count":"desc"
}
}
}
},
"_source":{
"excludes":[
"PurchasingViews",
"ContractFilters",
"SearchField*",
"Keywords*",
"Menus*"
]
},
"from":0,
"sort":[
{
"Name_en.raw":{
"order":"asc"
}
}
],
"script_fields":{
"price":{
"script":{
"lang":"painless",
"inline":"if(params['_source']['Prices'] != null){for(p in params['_source']['Prices']){ if(p.CompanyId == 1474) return p.Price; }} return null;"
}
}
}
}
(That bool, must, bool, must, bool, must, bool, should seems rather stupid?)
You need to use the nested inner_hits feature like below.
{
"_source": [
"CompanyId", "PartNumber", "Name_en", "ProductId", "CompanyName"
],
"query": {
"bool": {
"filter": [
{
"nested": {
"path": "Prices",
"query": {
"term": {
"Prices.BuyerId": 308
}
},
"inner_hits": {}
}
}
]
}
}
}
In the output you'll get exactly what you expect, namely all the root-level fields and the matching prices for the given buyer.
UPDATE:
Here is how I would rewrite your query:
{
"query": {
"bool": {
"minimum_should_match": "1",
"should": [
{
"multi_match": {
"query": "m4 washer",
"fields": [
"Name_en"
],
"type": "cross_fields",
"operator": "and"
}
},
{
"multi_match": {
"query": "m4 washer",
"fields": [
"Name_en"
],
"type": "phrase_prefix",
"operator": "and"
}
}
],
"filter": [
{
"nested": {
"path": "Prices",
"query": {
"term": {
"Prices.CompanyId": 1474
}
},
"inner_hits": {}
}
}
]
}
},
"size": 10,
"aggs": {
"CompanyName.raw": {
"terms": {
"field": "CompanyName.raw",
"size": 1000,
"order": {
"_count": "desc"
}
}
}
},
"_source": {
"excludes": [
"PurchasingViews",
"ContractFilters",
"SearchField*",
"Keywords*",
"Menus*",
"Prices"
]
},
"from": 0,
"sort": [
{
"Name_en.raw": {
"order": "asc"
}
}
],
"script_fields": {
"price": {
"script": {
"lang": "painless",
"inline": "if(params['_source']['Prices'] != null){for(p in params['_source']['Prices']){ if(p.CompanyId == 1474) return p.Price; }} return null;"
}
}
}
}

Elasticsearch - how to know if a particular match condition was hit

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

Elasticsearch query + filter

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

Elasticsearch additional boost if multiple conditions are met

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

Resources