Combine 2 elastic search queries - database

I've 2 function_score queries.
The first one, that I run on attractions type
{
"query": {
"function_score": {
"query": {
"nested": {
"path": "translations",
"query": {
"multi_match": {
"query": "Text to search",
"type": "best_fields",
"fields": [
"title^3",
"description"
]
}
}
}
},
"functions": [
{
"filter": {
"term": {
"cityId": 3
}
},
"weight": 100
}
],
"score_mode": "multiply"
}
}
}
and the second one, that I run on pizzeria type
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "Text to search",
"type": "best_fields",
"fields": [
"name^3",
"description"
]
}
},
"functions": [
{
"filter": {
"term": {
"cityId": 1
}
},
"weight": 100
}
],
"score_mode": "multiply"
}
}
}
They both works well. I know want to search on both types with a single query but I don't know how to "merge" these 2 queries.
I want to do this because I want to have the elements from the 2 type sorted by the same score.
Is this possible ? How Can I do this ?

You can use the Multi Search API to combine queries into one single query:
Multi Search API

Related

Elasticsearch query in array conditions on doc fields

I have a document like this:
{
"_index": "listings",
"_type": "listing",
"_id": "234",
"_source": {
"category_id": "43608",
"categories": [
43608,
43596
]
}
}
I wanna query to array search category_id in categories. some thing like that
{
"query": {
"bool": {
"must": [
{
"terms": {
"category_id": "doc.categories"
}
}
]
}
}
}
What I supposed to do?
As, category_id is a string type, better to use SHOULD query instead of MUST and Simply, Itrate through the array categories and make separate term level query for each element in array.
{
"query": {
"bool": {
"should": [
{
"term": {
"category_id": "doc.categories[0]"
}
},
{
"term": {
"category_id": "doc.categories[1]"
}
},
...
]
}
}
}
It will return you all which match any of categories array.
You have to user script for find a field value in another field value.
{
"script": {
"script": {
"source": "doc.containsKey('categories') && doc['categories'].values.contains(doc['category_id'].value)",
"lang": "painless"
}
}
}

Elasticsearch query identical arrays

I have documents like these:
Doc1
{
"id": ...,
...
"articles": [
{
"id": "5cdd17c7e24f6e05d487b2c2#142936",
...
},
{
"id": "5cdd17c7e24f6e05d487b2c2#226536",
...
}
...
}
Doc2
{
"id": ...,
...
"articles": [
{
"id": "5cdd17c7e24f6e05d487b2c2#142936",
...
},
{
"id": "5cdd17c7e24f6e05d487b2c2#226536",
...
},
{
"id": "5cdd17c7e24f6e05d487b2c2#142965",
...
}
...
}
Doc3
{
"id": ...,
...
"articles": [
{
"id": "5cdd17c7e24f6e05d487b2c2#142936",
...
}
...
}
And I want the document exactly has the array of articles I need. For example, if my Array of article Ids is ['5cdd17c7e24f6e05d487b2c2#142936', '5cdd17c7e24f6e05d487b2c2#226536'] I only want to get the Doc1.
Now I have this query:
GET my_index/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "articles",
"query": {
"query_string": {
"default_field": "articles.id",
"query": "5cdd17c7e24f6e05d487b2c2#142936 AND 5cdd17c7e24f6e05d487b2c2#226536"
}
}
}
}
]
}
}
}
But with this, I get Doc1 & Doc2...
Assuming articles.id is of type keyword, I think this should work for you (not sure it's the most efficient way to write the query):
GET my_index/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "articles",
"query": {
"term": {
"articles.id": "5cdd17c7e24f6e05d487b2c2#142936"
}
}
}
},
{
"nested": {
"path": "articles",
"query": {
"term": {
"articles.id": "5cdd17c7e24f6e05d487b2c2#226536"
}
}
}
}
],
"must_not": {
"nested": {
"path": "articles",
"query": {
"query_string": {
"default_field": "articles.id",
"query": "NOT 5cdd17c7e24f6e05d487b2c2#142936 AND NOT 5cdd17c7e24f6e05d487b2c2#226536"
}
}
}
}
}
}
}

ElasticSearch-Kibana : filter array by key

I have data with one parameter which is an array. I know that objects in array are not well supported in Kibana, however I would like to know if there is a way to filter that array with only one value for the key. I mean :
This is a json for exemple :
{
"_index": "index",
"_type": "data",
"_id": "8",
"_version": 2,
"_score": 1,
"_source": {
"envelope": {
"version": "0.0.1",
"submitter": "VF12RBU1D53087510",
"MetaData": {
"SpecificMetaData": [
{
"key": "key1",
"value": "94"
},
{
"key": "key2",
"value": "0"
}
]
}
}
}
}
And I would like to only have the data which contains key1 in my SpecificMetaData array in order to plot them. For now, when I plot SpecificMetaData.value it takes all the values of the array (value of key1 and key2) and doesn't propose SpecificMetaData.value1 and SpecificMetaData.value2.
If you need more information, tell me. Thank you.
you may need to map your data to mappings so as SpecificMetaData should act as nested_type and inner_hits of nested filter can supply you with objects which have key1.
PUT envelope_index
{
"mappings": {
"document_type": {
"properties": {
"envelope": {
"type": "object",
"properties": {
"version": {
"type": "text"
},
"submitter": {
"type": "text"
},
"MetaData": {
"type": "object",
"properties": {
"SpecificMetaData": {
"type": "nested"
}
}
}
}
}
}
}
}
}
POST envelope_index/document_type
{
"envelope": {
"version": "0.0.1",
"submitter": "VF12RBU1D53087510",
"MetaData": {
"SpecificMetaData": [{
"key": "key1",
"value": "94"
},
{
"key": "key2",
"value": "0"
}
]
}
}
}
POST envelope_index/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"inner_hits": {},
"path": "envelope.MetaData.SpecificMetaData",
"query": {
"bool": {
"must": [
{
"term": {
"envelope.MetaData.SpecificMetaData.key": {
"value": "key1"
}
}
}
]
}
}
}
}
]
}
}
}

Sort by nested field

I've a type in elastic with documents with this structure
{
"name": "Foo Bar",
"myTags": [
{
"id": 3,
"name": "My tag 1"
},
{
"id": 5,
"name": "My Tag 5"
},
{
"id": 7,
"name": "My Tag 7"
}
]
}
Now, given 3 tags I would like to get ALL documents sorted by the number of matching tags. So first the documents that match all 3 tags than those that match 2 then one and finally none.
How can I do this ?
You can do it with function_score:
{
"query": {
"function_score": {
"query": {
"match_all": {}
},
"functions": [
{
"filter": {
"nested": {
"path": "myTags",
"query": {
"term": {
"myTags.name": "My Tag 1"
}
}
}
},
"weight": 1
},
{
"filter": {
"nested": {
"path": "myTags",
"query": {
"term": {
"myTags.name": "My Tag 5"
}
}
}
},
"weight": 1
},
{
"filter": {
"nested": {
"path": "myTags",
"query": {
"term": {
"myTags.name": "My Tag 7"
}
}
}
},
"weight": 1
}
],
"boost_mode": "sum",
"score_mode": "sum"
}
}
}

Filtering results in mongodb

maybe a simple question for experimented developers with MongoDB, but I'm not getting a solution:
My json with "Stations" collection:
{
"code": "XX",
"variables": [
{
"code": 1,
"items": [
{
"value": 81
},
{
"value": 77
}
]
},
{
"code":2,
"items": [
{
"value": 33
}
]
}
]
}
....
I want to filter "Station" collection to only get variable with code 1 and item with value 81, obtaining something similar to this:
{
"code": "XX",
"variables": [
{
"code": 1,
"items": [
{
"value": 81
}
]
}
]
}
Due json contains arrays in different levels, my approach (mongo shell) was:
db.stations.find(
{"code": "XX"},
{
"variables":
{ $elemMatch:
{
"code": 1,
"items":
{ $elemMatch:
{
"value": 81
}
}
}
}
}
)
But that's getting all items of the same level of 'value: 81', not only this.
Any idea? I also tried to do something with "aggregate" operator and $redact, but no result...thanks!
As per mongo $elemMatch documentation
The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.
hence using $elemMatch it match in items.value:81 and return whole matching items array like below query
db.stations.find({
"code": "XX"
}, {
"variables": {
"$elemMatch": {
"code": 1
}
},
"variables": {
"$elemMatch": {
"items": {
"$elemMatch": {
"value": 81
}
}
}
}
}).pretty()
This return items.value:81 and items.value:77 because of elemMatch match one elements in array. Or same if used in project as below it shows same result like above query
db.stations.find({
"code": "XX",
"variables": {
"$elemMatch": {
"code": 1
}
},
"variables": {
"$elemMatch": {
"items": {
"$elemMatch": {
"value": 81
}
}
}
}
}, {
"code": 1,
"variables.code.$": 1
}).pretty()
So If you find your expected output then you should use mongo aggregation as below :
db.stations.aggregate({
"$match": {
"code": "XX",
"variables.code": 1
}
}, {
"$unwind": "$variables"
}, {
"$unwind": "$variables.items"
}, {
"$match": {
"variables.items.value": 81
}
}, {
"$group": {
"_id": "$code",
"data": {
"$push": "$variables"
}
}
}, {
"$project": {
"code": "$_id",
"variables": "$data",
"_id": 0
}
}).pretty()

Resources