I have data in the following format
{
"mappings": {
"blog": {
"properties": {
"comments": {
"type": "nested",
"properties": {
"subComments": {
"type": "nested"
}
}
}
}
}
}
}
And i have multiple documents with data like
{
"blog_post_id": "blog1",
"comments": [
{
"id": "c1",
"user_id": "u1",
"timestamp": 1487781975676,
"value": "CVLA1",
"subComments": [
{
"value": "sub comment 1"
},
{
"value": "sub comment 2"
}
]
},
{
"id": "c2",
"user_id": "u1",
"timestamp": 1487781975686,
"value": "CVLA2",
"subComments": [
{
"value": "sub comment 3"
},
{
"value": "sub comment 4"
}
]
}
]
}
I'd like match the blog documents which have comment value CVLA1 and a suc comment which has value "sub comment 2".
I wrote a query like
{
"query": {
"nested": {
"path": "comments",
"query": {
"bool": {
"must": [
{
"match": {
"comments.value": "CVLA1"
}
},
{
"nested": {
"path": "comments.subComments",
"query": {
"match": {
"commnets.subComments.value": "sub comment 2"
}
}
}
}
]
}
}
}
}
}
But this one doesn't work as expected. Any help how to query at different levels of a multi level nested document.
You have a typo in your query around commnets.subComments.value. It should be comments.subComments.value. So the entire query would look like this:
{
"query": {
"nested": {
"path": "comments",
"query": {
"bool": {
"must": [
{
"match": {
"comments.value": "CVLA1"
}
},
{
"nested": {
"path": "comments.subComments",
"query": {
"match": {
"comments.subComments.value": "sub comment 2"
}
}
}
}
]
}
}
}
}
}
I double checked - it works fine for me.
Related
Hi i want to search array element from index using elastic search query
{
"name": "Karan",
"address": [
{
"city": "newyork",
"zip": 12345
},
{
"city": "mumbai",
"zip": 23456
}]
}}
when i am trying to search using match query it does not work
{
"query": {
"bool": {
"must": [
{
"match": {
"address.city": "newyork"
}
}
]
}
}
}
when i access simple feild like "name": "Karan" it works, there is only issue for array element.
Because nested objects are indexed as separate hidden documents, we can’t query them directly. Instead, we have to use the nested query to access them:
GET /my_index/blogpost/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "eggs"
}
},
{
"nested": {
"path": "comments",
"query": {
"bool": {
"must": [
{
"match": {
"comments.name": "john"
}
},
{
"match": {
"comments.age": 28
}
}
]
}
}
}
}
]
}}}
See the docs
The way i followed..
Mapping :
{
"mappings": {
"job": {
"properties": {
"name": {
"type": "text"
},
"skills": {
"type": "nested",
"properties": {
"value": {
"type": "text"
}
}
}
}
}
}
Records
[{"_index":"jobs","_type":"job","_id":"2","_score":1.0,"_source":{"name":"sr soft eng","skills":[{"value": "java"}, {"value": "oracle"}]}},{"_index":"jobs","_type":"job","_id":"1","_score":1.0,"_source":{"name":"sr soft eng","skills":[{"value": "java"}, {"value": "oracle"}, {"value": "javascript"}]}},
search Query
{
"query": {
"nested": {
"path": "skills",
"query": {
"bool": {
"must": [
{ "match": {"skills.value": "java"}}
]
}
}
}
}
}
Given this sample data:
"users": {
"user1": {
"first": "john",
"last": "bellamy"
},
"user2": {
.....
.....
}
}
How can I set up elasticsearch to query/search on child first and last? Ohter tutorials only shows one level child, not this 2 or more level child.
I tried looking for a solution, and I guess that it has something to do with mapping option?
I just started elasticsearch few days ago, already manage to set up and adding data.
This works for me
{
"query": {
"bool": {
"must": [{
"term": {
"users.user2.firstname": {
"value": "sumit"
}
}
}]
}
}
}
nested users approach
mappings
{
"mappings": {
"test_type": {
"properties": {
"users": {
"type": "nested",
"properties": {
"firstname": {
"type": "text"
},
"lastname": {
"type": "text"
}
}
}
}
}
}
}
query
{
"query": {
"bool": {
"must": [{
"nested": {
"inner_hits": {},
"path": "users",
"query": {
"bool": {
"must": [{
"term": {
"users.firstname": {
"value": "ajay"
}
}
}]
}
}
}
}]
}
}
}
Im trying to search two or more values on array and get only those ones that match with all words (AND CLAUSE)
Some example:
{ "name" : "Chevrolet",
"value" : [ "gasolina", "alcool", "diesel"]
}
{ "name" : "Fiat",
"value" : [ "eletrica", "alcool"]
}
{ "name" : "Honda",
"value" : [ "diesel", "gasolina"]
}
My mapping
{
"mappings": {
"cars": {
"properties": {
"name": {
"type": "string"
},
"GasType": {
"type": "nested",
"properties": {
"value": {
"type": "string"
}
}
}
}
}
}
}
Query:
{
"query": {
"nested": {
"path": "GasType",
"query": {
"bool": {
"must": [
{ "match": {"GasType.value": "gasolina"}},
{ "match": {"GasType.value": "diesel"}}
]
}
}
}
}
}
My return is always empty and if i change de query i have got all those that contains "Gasolina" or "diesel"
I need those that has "Gasolina" AND "diesel"
Your test data doesn't match the mapping of the index. In your test data I don't see the nested field name GasType. In any case, the following works for me just fine:
DELETE test
PUT test
{
"mappings": {
"cars": {
"properties": {
"name": {
"type": "string"
},
"GasType": {
"type": "nested",
"properties": {
"value": {
"type": "string"
}
}
}
}
}
}
}
POST test/cars/_bulk
{"index":{}}
{"name":"Chevrolet","GasType":{"value":["gasolina","alcool","diesel"]}}
{"index":{}}
{"name":"Fiat","GasType":{"value":["eletrica","alcool"]}}
{"index":{}}
{"name":"Honda","GasType":{"value":["diesel","gasolina"]}}
{"index":{}}
{"name":"Honda","GasType":{"value":["diesel"]}}
GET test/_search
{
"query": {
"nested": {
"path": "GasType",
"query": {
"bool": {
"must": [
{
"match": {
"GasType.value": "gasolina"
}
},
{
"match": {
"GasType.value": "diesel"
}
}
]
}
}
}
}
}
I am trying to write an elastic search query where I have an array of sub-documents with exactly same structure and I have to check two conditions within the same sub document.
My data looks like this :
{
"_id": "5672ba7c0d71c93d159c408e",
"productId": "1723913526",
"title": "Digitab Tablet",
"instock": true,
"specifications": [{
"category": "In the box",
"name": "Test1",
"value": "No"
}, {
"category": "Warranty",
"name": "Test2",
"value": "Yes"
}, {
"category": "General",
"name": "Test3",
"value": "Abcd"
}],
}
}
What I am trying is : I should get products where specifications.name is "Test1" and specifications.value of the same sub document is "Yes"
Here is the Query that I have written for the this:
{
"query": {
"bool": {
"must": [
{
"wildcard": {
"specifications.name": "Test1*"
}
},
{
"term": {
"instock": "true"
}
},
{
"term": {
"specifications.value": "yes"
}
}
],
"must_not": [],
"should": []
}
}
}
The issue is - It should not return the product with id 1723913526 because "value": "Yes" is true for "name": "Test2" and not for "name": "Test1".
I hope the question is clear, please comment if more information is needed.
Thanks in advance.
You need to modify your mapping to indicate that specifications is a nested type.
Mapping like:
"mappings": {
"product": {
"properties": {
...
"specifications": {
"type": "nested"
}
}
}
}
And then querying like:
"bool": {
"must": [{
"nested": {
"path": "specifications",
"query": {
"bool": {
"must": [
{ "wildcard": { "specifications.first": "Test1*" }},
{ "term": { "specifications.value": "yes" }}
]
}
}
},
{
"term": { "instock": "true" }
}
}]
}
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}}"
}
}
}
}
]
}
}
]
}
}
}
}
}
}