简体   繁体   中英

Elasticsearch match field=value (not contains)

I have problem while searching through elasticsearch. I have index product with fields title and gender

When I make query with default_field: title I need to get results only with gender=male (not female or others)

query: dress AND gender:male

Results contain both genders: male and female and male,female

It seems to me that gender:* search all which contains male , but not full match of value. How to do the query right?

I use it through Ruby on Rails

Product.search({
      query: {
        query_string: {
            query: query,
            default_field: "title"
          }
        },
        size: per_page,
        sort: [ _score: { order: relevance } ]
    })

Is gender a keyword data type? I suspect that you left/set the default mapping to the gender field (ie, text + keyword subfield). In this case, try the following query: dress AND gender.keyword:male

According with this I just need to put value in double quotes..

query = '(dress) AND (gender:"male")'

do not forget to escape them if needed "gender:\"male\""

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