Query using array values against String value

version : "6.5.4"
I have mapping like :
{ "examples" : { "mappings" : { "example" : { "properties" : { // ... "petGender" : { "type" : "text" } } } } } }

and values for
GET /examples/_search : { "took" : 0, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "examples", "_type" : "example", "_id" : "5e9038d11543752", "_score" : 1.0, "_source" : { "gender" : "All", // ... } } ] } }

and i want to query like :
GET /examples/_search { "query": { "bool": { "must": [ { "bool": { "should": [ //... { "term": { "gender": ["All"] //["Male","Female"] } }, // ... ] } } ] } } }
and i get empty response

how do i perform query like this
using array values against String value

Welcome!

You should use a match query (which is analyzed) in that case instead of a term query (which is not analyzed).

thank you but i have to match wheather gender is in array like ["Male","Female","Other"] currently only working for all gender that is single string "All"

You want that the document matches one of the values or all the values?

yes one of the value in array

You can do:

GET /_search
{
    "query" : {
        "terms" : {
            "gender" : ["male", "female"]
        }
    }
}

Or if you have the mapping which uses either a keyword datatype or a keyword analyzer, you can run:

GET /_search
{
    "query" : {
        "terms" : {
            "gender" : ["Male", "Female"]
        }
    }
}

thank you so much @dadoonet camel case and small case giving diff. result,
value stored is "All" but need "all" in query i think i have to go through doc again.

thank you

if you have an issue could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

@dadoonet no i have solved by changing fields to keyword as you mentioned in previous solution. I just forgot to say it worked .

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.