Query specific term value with a specific numeric value


(Daniel C S Yeh) #1

my mappiing:

{
"20150520": {
"mappings": {
"01": {
"properties": {
"rekognition_tags": {
"properties": {
"confidence": {
"type": "double"
},
"tag": {
"type": "string"
}
}
},
"url": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}

my query
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "sky"
}
},
{
"range": {
"confidence": {
"gt": "0.5"
}
}
}
]
}
}
}

result (2 hits)
"_source": {
"rekognition_tags": [
{
"tag": "vessel",
"confidence": 0.7106
},
{
"tag": "boat",
"confidence": 0.7106
},
{
"tag": "outdoors",
"confidence": 0.6975
},
{
"tag": "sky",
"confidence": 0.6957
},
{
"tag": "cloud",
"confidence": 0.6172
}
],
"url": "http://zencirclemedia.blob.core.windows.net/media/5c233265cd
bef5b10929ddb8e12c2bfd.jpg"
}

	  "_source": {
        "rekognition_tags": [
          {
            "tag": "collection",
            "confidence": 0.876
          },
          {
            "tag": "collage",
            "confidence": 0.876
          },
          {
            "tag": "outdoors",
            "confidence": 0.0908
          },
          {
            "tag": "sky",
            "confidence": 0.0898
          },
          {
            "tag": "sunset",
            "confidence": 0.0898
          }
        ],
        "url": "http://zencirclemedia.blob.core.windows.net/media/7dc9a884c3

4231987ad15ec3c54b036d.jpg"
}

the second hit is wrong since sky's confidence is under 0.5, why elasticsearch still returns it?

Thanks


(Mark Harwood) #2

Your query is asking for docs containing any tag "sky" and (separately) any tag with confidence value >0.5.
Both of these conditions are true.
To test confidence and tag values of the same object in an array of objects like your rekognition_tags you need to be more explicit in both the mapping and the query. Take a look at the nested support: https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-nested-type.html

Cheers
Mark


(Camilo Sierra) #3

hello @Daniel_C_S_Yeh hear you have a great post to explain the difference between array and nested https://www.elastic.co/blog/managing-relations-inside-elasticsearch


(Daniel C S Yeh) #4

it is because my mapping is wrong or query is wrong?

is there any query example that I can reference?

thanks


(Camilo Sierra) #5

hello @Daniel_C_S_Yeh , for use nested you need to change your mapping and your query ! there is an example in the link that i send you, and is important to read this post to understand the difference between inner obj and nested and how defined a nested . https://www.elastic.co/blog/managing-relations-inside-elasticsearch


(system) #6