I am trying to fetch documents where is missing field "topic.description" and match term "fundedUnder.programme": "ABC".
Mapping:
...
"fundedUnder": {
"properties": {
"programme": {
"type": "string"
},
"subprogramme": {
"type": "string"
}
}
},
"topics": {
"type": "nested",
"include_in_parent": true,
"properties": {
"code": {
"type": "string",
"analyzer": "analyzer_keyword"
},
"description": {
"type": "string",
"analyzer": "analyzer_keyword"
},
"title": {
"type": "string",
"analyzer": "analyzer_keyword"
}
}
},
...
My Query looks like:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"missing": {
"field": "topics.description"
}
},
{
"term": {
"fundedUnder.programme" : "ABC"
}
}
]
}
}
}
}
}
This query found nothing and that is wrong, because I have in indexes a lot of documents with fundedUnder.programme == "ABC" and with missing field topics.description.
Thanks in advance.
ElasticSearch version 1.7.5
I believe this should work:
EDIT: updated to use version 1.7 Query DSL
{
"query": {
"filtered": {
"query": {
"match": { "fundedUnder.programme" : "ABC" }
},
"filter": {
"missing": { "field": "topics.description" }
}
}
}
}
Related
I have an ES index with this mapping:
{
"_doc": {
"dynamic": "false",
"properties": {
"original": {
"properties":{
"id": {
"type": "keyword"
},
"purchaseStatus": {
"type": "keyword"
},
"marketCode": {
"type": "keyword"
},
"salesProfiles": {
"type": "nested",
"properties": {
"marketCode": {
"type": "keyword"
},
"purchaseStatus": {
"type": "keyword"
}
}
}
}
},
"recommended": {
"properties":{
"id": {
"type": "keyword"
},
"purchaseStatus": {
"type": "keyword"
},
"marketCode": {
"type": "keyword"
},
"salesProfiles": {
"type": "nested",
"properties": {
"marketCode": {
"type": "keyword"
},
"purchaseStatus": {
"type": "keyword"
}
}
}
}
},
"distance": {
"type": "double"
},
"rank": {
"type": "double"
},
"source": {
"properties": {
"application": {
"type": "keyword"
},
"platform": {
"type": "keyword"
}
}
},
"timestamp": {
"properties": {
"createdAt": {
"type": "date"
},
"updatedAt": {
"type": "date"
}
}
}
}
},
"_default_": {
"dynamic": "false"
}
}
and I need to obtain the recommended docs with salesProfiles.marketCode equal to original.marketCode but my query doesn't return any buckets:
GET index/_search
{
"aggs": {
"similarities": {
"filter": {
"bool": {
"must": [
{
"term": {
"original.storefrontId": "12345"
}
},
{
"nested": {
"path": "recommended.salesProfiles",
"query": {
"bool": {
"must": [
{
"match": {
"recommended.salesProfiles.purchaseStatus": "PAID"
}
}
]
}
}
}
}
]
}
},
"aggs": {
"markets": {
"nested": {
"path": "recommended.salesProfiles"
},
"aggs": {
"recommendedMarket": {
"terms": {
"field": "recommended.salesProfiles.marketCode",
"size": 100
}
}
}
}
}
}
},
"explain": false
}
Any suggestion would be really appreciated. Thanks in advance!
Its hard to debug this without any example docs, but I think this might work
{
"size": 0,
"query": {
"bool": {
"must": [
{
"term": {
"original.storefrontId": "12345"
}
},
{
"nested": {
"path": "recommended.salesProfiles",
"query": {
"bool": {
"must": [
{
"match": {
"recommended.salesProfiles.purchaseStatus": "PAID"
}
}
]
}
}
}
}
]
}
},
"aggs": {
"Profiles": {
"nested": {
"path": "recommended.salesProfiles"
},
"aggs": {
"by_term": {
"terms": {
"field": "recommended.salesProfiles.marketCode",
"size": 100
}
}
}
}
}
}
I don't think you can use "nested" under the filter agg without being under a nested aggregation, so I believe that's why you didn't get any docs.
I basically moved all the filtering to the query and just aggregated the terms later
I use ElasticSearch version 1.7.5 and I am trying to fetch all documents where missing some fields.
My mapping:
...
"participant": {
"properties": {
"id": {
"type": "string"
},
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"name": {
"type": "string"
}
},
"coordinator": {
"properties": {
"id": {
"type": "string"
},
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"name": {
"type": "string"
}
}
...
I want to query all documents that don't have assigned coordinator.id or participant.id yet.
My query looks like:
"query": {
"nested": {
"path": "coordinator, participant",
"query": {
"constant_score": {
"filter": {
"or": [
{
"missing": {
"field": "coordinator.id"
}
},
{
"missing": {
"field": "participant.id"
}
},
]
}
}
}
}
}
You do OR queries via the bool query:
https://www.elastic.co/guide/en/elasticsearch/reference/1.7/query-dsl-bool-filter.html
So this query would work:
{
"query": {
"bool": {
"should": [
{
"constant_score": {
"filter": {
"missing": {
"field": "participant.id"
}
}
}
},
{
"constant_score": {
"filter": {
"missing": {
"field": "coordinator.id"
}
}
}
}
]
}
}
}
I noticed that you were using a nested query though the mapping does not state that coordinator and participant are nested field types so that will not work:
https://www.elastic.co/guide/en/elasticsearch/reference/1.7/mapping-nested-type.html
Setting something as a nested type is only useful when you need to group search terms together so I don't think it is necessary for you.
My elasticsearch coding as below:
"query": {
"constant_score" : {
"filter" : {
"bool": {
"must": [
{
"terms": {"stkno" : ["Net Asset","asset combine"]}},
{ "range": { "CDate": { "gte": "2015-01-01" , "lte": "2017-06-03T23:59:59" } } } ]
}}}}
this is mapping:
{
"news": {
"mappings": {
"bursa": {
"_timestamp": {},
"properties": {
"CDate": {
"type": "date",
"format": "epoch_millis||dateOptionalTime"
},
"RDate": {
"type": "date",
"format": "epoch_millis||dateOptionalTime"
},
"category": {
"type": "string"
},
"content": {
"type": "string"
},
"exDate": {
"type": "string"
},
"exgcode": {
"type": "string"
},
"htext": {
"type": "string"
},
"paymentDate": {
"type": "string"
},
"stat": {
"type": "string"
},
"stkno": {
"type": "string"
},
"tag": {
"type": "string"
}
}
}
}
}
}
Please kindly advice if my word is : Net Asset and Asset combine, use in Must condition,
if "Asset combine" not in the elasticsearch and "New Asset" in the elasticsearch, using Must condition so all indices can't search?
how to do i want find the word if ones of them inside elasticsearch?
Cause it can't find "Net Asset" exactly word when i use should condition.
Thanks
Since stkno is an analyzed field and you are applying terms query which looks for exact match, you are not able fetch results. Try this instead.
"bool": {
"should": [
{
"match": {
"stkno": "Net Asset",
"operator": "and"
}
},
{
"match": {
"stkno": "asset combine",
"operator": "and"
}
}
],
"must": [
{
"range": {
"CDate": {
"gte": "2015-01-01",
"lte": "2017-06-03T23:59:59"
}
}
}
]
}
Hope it helps!!
I have the following mapping:
PUT /test
{
"mappings": {
"test": {
"properties": {
"parent": {
"type": "nested",
"properties": {
"#id": {
"type": "string",
"index": "not_analyzed"
},
"#type": {
"type": "string"
},
"child": {
"type": "nested",
"properties": {
"#id": {
"type": "string",
"index": "not_analyzed"
},
"subchild": {
"type": "nested",
"properties": {
"#id": {
"type": "string",
"index": "not_analyzed"
},
"hasA": {
"type": "nested",
"properties": {
"#value": {
"type": "string"
}
}
},
"hasB": {
"type": "nested",
"properties": {
"#id": {
"type": "string",
"index": "not_analyzed"
}
}
},
"hasC": {
"type": "nested",
"properties": {
"#id": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}
}
}
}
}
}
And the following document:
POST /test/test/1
{
"parent": {
"#id": "12345",
"#type": "test",
"child": [
{
"#id": "1",
"subchild": [
{
"#id": "1.1",
"hasA": {
"#value": "hasA value"
},
"hasB": {
"#id": "hasB_1"
},
"hasC": {
"#id": "hasC_1"
}
}
]
},
{
"#id": "2",
"subchild": [
{
"#id": "2.1",
"hasA": {
"#value": "hasA value"
},
"hasB": {
"#id": "hasB_2"
},
"hasC": {
"#id": "hasC_2"
}
}
]
}
]
}
}
And the following query:
POST test/test/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "parent.child.subchild.hasB",
"filter": {
"bool": {
"must": [
{
"term": {
"parent.child.subchild.hasB.#id": "hasB_2"
}
}
]
}
},
"_cache": false
}
}
}
}
}
I'm unable to set the path to just parent.child.subchild so that I can match on both hasB and hasC, it seems I can only select one nested item at a time. This is what I would like to be able to do:
POST test/test/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "parent.child.subchild",
"filter": {
"bool": {
"must": [
{
"term": {
"parent.child.subchild.hasB.#id": "hasB_2"
}
},
{
"term": {
"parent.child.subchild.hasC.#id": "hasC_2"
}
}
]
}
},
"_cache": false
}
}
}
}
}
Are you looking for something like this?
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "parent.child.subchild",
"filter": {
"bool": {
"must": [
{
"nested": {
"path": "parent.child.subchild.hasB",
"query": {
"term": {
"parent.child.subchild.hasB.#id": "hasB_2"
}
}
}
},
{
"nested": {
"path": "parent.child.subchild.hasC",
"query": {
"term": {
"parent.child.subchild.hasC.#id": "hasC_2"
}
}
}
}
]
}
},
"_cache": false
}
}
}
}
}
Query for correct syntax for multi-level nested docs can be found here. Look at #martijnvg comments inside it.
ES Docs has not done a good job on explaining multi-level nested query.
Basically you need to nest subchild inside child inside parent and specify individual path. You will need three nested queries.
P.S - I have not tested this myself. Please let me know if it does not work.
Is it possible to use nested object mappings in a has_parent query or filter? Whenever I try I always get:
[customers_and_messages] [nested] nested object under path [accounts]
is not of nested type]
Mapping
{
"customer": {
"properties": {
"name": {
"analyzer": "name_edge_ngram_analyzer",
"type": "string"
},
"phone_number": {
"analyzer": "phone_number_edge_ngram_analyzer",
"type": "string"
},
"location_id": {
"index": "not_analyzed",
"type": "string"
},
"accounts": {
"type": "nested",
"properties": {
"id": {
"index": "not_analyzed",
"type": "string"
},
"tags": {
"type": "string"
},
"notes": {
"type": "string"
}
}
}
}
}
}
Query
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"has_parent": {
"type": "customer",
"filter": {
"nested": {
"path": "accounts",
"filter": {
"term": {
"accounts.id": "8392f356-d6ec-11e4-8737-b74c1bacfb15"
}
}
}
}
}
}
],
"should": [
{
"term": {
"kind": "task"
}
},
{
"term": {
"kind": "alert"
}
}
]
}
}
}
}
}