ElasticSearch: Mapping & querying multi-layer nested objects


(nirmala ranganathan) #1

This question might have been answered but it is hard to find it in this
forum. Thanks for your help.

I have multi-level nested object, say for example -
In this case eData is a nested object that contains bData and contact.

{
"eId" : "123",
"eData" : {
"bData" : {
"reviewList" : [
{
"id" : "1950158",
"author" : "fancylaw",
"text" : "I love this Beauty salon. The staff is friendly and professional.
",
"rating" : "5",
"date" : "2007-07-23",
},
{
"id" : "1950159",
"author" : "mmeylor",
"text" : "I called & asked the price of a haircut & was told $45. When I
got there I found out that if I wanted it blow dryed it would actually be
$80! ",
"rating" : "1",
"date" : "2009-03-20",
},
],
"categoryList" : [
"Beauty & Spas",
"Hair Care & Salons",
"Wigs Toupees & Hairpieces"
]
},
"contact" : {
"streetAddress" : "124 1/2 N Larchmont Blvd",
"phone" : "3234612979",
"website" : "http://www.jessica.com",
"country" : "USA",
"addressLocality" : "Los Angeles",
"postalCode" : "90004",
"email" : "sshssh@gmail.com",
"addressRegion" : "CA",
"googlePlusUrl" : "http://googleplus",
"name" : "Jessica's Beauty",
"twitterUrl" : "http://twitter",
}
}
}

Then I do the mapping.

Question: Do I need to map every single field in here or just a high
level field (of type = nested) is good enough? In other words, in this
mapping below, I have set bData and contact type as nested but didn't list
all the fields that it holds. I thought this is good enough to be able to
query.

curl -XPOST 'http://localhost:9200/myindex/' http://localhost:9200/mongoentityindex/' -d '
{
"mappings":{
"eType":{
"properties":{
"eData":{
"type": "nested",
"include_in_parent": true,
"include_in_root": true,
"properties": {
"bData": {
"type": "nested"
},
"contact": {
"type": "nested"
}
}
}
}
}
}
}'

Next to sync the data from my mongo database. It works fine. I can see that
all the data gets loaded as expected.

curl -XPUT 'http://localhost:9200/_river/mbservices/_meta' -d '{ "type":
"mongodb", "mongodb": { "db": "mydb", "collection": "entity" }, "index": {
"name": "myindex", "type": "eType" } }'

Now I would like to query for text "Beauty" within the nested field path
"eData".
You can see from the sample record above the word "Beauty" exists in 3
fields "eData.bData.reviewList[0].text", "eData.bData.categoryList" and
"eData.bData.contact".
I expect the output fields to contain eId and only those that had the
matched text. I would like to highlight the part of the matched text to the
user.
The following search returns nothing.

curl -XGET 'localhost:9200/myindex/_search?pretty=true' -d '
{
"query": {
"nested": {
"path": "eData",
"score_mode": "max",
"query": {
"query_string": {
"fields": [
"eData.contact", "eData.bData"
],
"query": "Beauty"
}
}
}
}
}'

When I search for the text "Beauty" within "eData.contact.name" in path
"eData.contact", it works.
But in case I don't even know where the text will be present and if present
in any fields, I need to display that record to the user.

curl -XGET 'localhost:9200/mongoentityindex/_search?pretty=true' -d '
{
"fields" : ["entityData.contact.name"],
"query": {
"nested": {
"path": "entityData.contact",
"score_mode": "max",
"query": {
"query_string": {
"fields": [
"eData.contact.name"
],
"query": "Beauty"
}
}
}
}
}'

If you need additional information, please let me know. Thanks again for
your help.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(system) #2