简体   繁体   中英

Elasticsearch query to select all documents where one field's value is contained multi-valued field

Is there a way to query elastic search for all documents that contain the value of one property in the multivalued field; ie:

I have a list of property values in field COLORS: Red, Blue, Black, Green another property has a single value in field PREFERENCE : Red

is there a way to select all documents that contain value found in field PREFERENCE within the multi valued field COLORS?

SQL equivalent would be something of this sort:

SELECT * FROM index WHERE COLORS LIKE CONCAT('%', PREFERENCE, '%')

You could use a script filter. Something like this

GET /test/stack/_search
{
    "query": {
        "match_all": {}
    },
    "filter": {
        "script":{ 
           "script":"if(doc['colors'].values.indexOf(doc['preference'].value) > -1) true; else false;" ,
           "params": {}

        },"lang":"js"
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM