Query for missing fields in nested documents


(aash dhariya) #1

I have a user document which contains many tags
Here is the mapping:
{
"user" : {
"properties" : {
"tags" : {
"type" : "nested",
"properties" : {
"id" : {
"type" : "string",
"index" : "not_analyzed",
"store" : "yes"
},
"current" : {
"type" : "boolean"
},
"type" : {
"type" : "string"
},
"value" : {
"type" : "multi_field",
"fields" : {
"value" : {
"type" : "string",
"analyzer" : "name_analyzer"
},
"value_untouched" : {
"type" : "string",
"index" : "not_analyzed",
"include_in_all" : false
}
}
}
}
}
}
}
}
Here are the sample user documents
User 1:

{
"created_at": 1317484762000,
"updated_at": 1367040856000,
"tags": [
{
"type": "college",
"value": "Dhirubhai Ambani Institute of Information and
Communication Technology",
"id": "a6f51ef8b34eb8f24d1c5be5e4ff509e2a361829"
},
{
"type": "company",
"value": "alma connect",
"id": "58ad4afcc8415216ea451339aaecf311ed40e132"
},
{
"type": "company",
"value": "Google",
"id": "93bc8199c5fe7adfd181d59e7182c73fec74eab5",
"current": true
},
{
"type": "discipline",
"value": "B.Tech.",
"id": "a7706af7f1477cbb1ac0ceb0e8531de8da4ef1eb",
"institute_id": "4fb424a5addf32296f00013a"
},
]
}

User 2:

{
"created_at": 1318513355000,
"updated_at": 1364888695000,
"tags": [
{
"type": "college",
"value": "Dhirubhai Ambani Institute of Information and
Communication Technology",
"id": "a6f51ef8b34eb8f24d1c5be5e4ff509e2a361829"
},
{
"type": "college",
"value": "Bharatiya Vidya Bhavan's Public School, Jubilee hills,
Hyderabad",
"id": "d20730345465a974dc61f2132eb72b04e2f5330c"
},
{
"type": "company",
"value": "Alma Connect",
"id": "93bc8199c5fe7adfd181d59e7182c73fec74eab5"
},
{
"type": "sector",
"value": "Website and Software Development",
"id": "dc387d78fc99ab43e6ae2b83562c85cf3503a8a4"
}
]
}

User 3

{
"created_at": 1318513355001,
"updated_at": 1364888695010,
"tags": [
{
"type": "college",
"value": "Dhirubhai Ambani Institute of Information and
Communication Technology",
"id": "a6f51ef8b34eb8f24d1c5be5e4ff509e2a361821"
},
{
"type": "sector",
"value": "Website and Software Development",
"id": "dc387d78fc99ab43e6ae2b83562c85cf3503a8a1"
}
]
}

Using the above ES documents for search, I want to construct a query where
I need to fetch users who have company tags in nested tag documents or the
users who do not have any company tags. What will be my search query?

For example in above case, if search for google tag, then the returned
documents should be 'user 1' and 'user 3' (as user 1 has company tag google
and user 3 has no company tag). User 2 is not returned as it has a company
tag other than google too.

--
Thanks,
Aash

--
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.


#2

I have the same issue.

I need to remove empty topics field and i can't find any documentation or solution to query them.
Scheme:

"doc": {
"_parent": {
"type": "source"
},
"_timestamp": {
"enabled": true
},
"_all": {
"enabled": true
},
"properties": {
"topics":{
"type": "nested",
"properties":{
"label":{"type":"string","index":"not_analyzed"},
"score":{"type":"float"}
}
}
}
}

Data sample:

{
"_index" : "watch",
"_type" : "doc",
"_id" : "539f6cdafd667041e356c8740707b87f",
"_score" : 0.5058483,
"_routing" : "cbf1d10571c4da9d101c1b4fab3d3d93",
"_timestamp" : 1476395901292,
"_parent" : "cbf1d10571c4da9d101c1b4fab3d3d93",
"_source" : {
"topics" : [ ]
}
}

Query:

{
"size":1,
"query":{
"nested": {
"path": "topics",
"query": {
"missing": {
"field":"topics"
}
}
}
}
}

Result:

{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}

Thank you for your help.


(system) #3