Full match of query value from beginning to end of field

Hello! I have problem with full match querying of field value. title and gender - fields of indexed docs

query: {
  query_string: {
    query: "box AND gender:\"women\"",
    default_field: "title"
  }
}

I use double quotes to match full query for gender. But if there is gender "men,women" with title 'box' it also will be in results. I know, that elasticsearch does not support regexp characters ^ and $ for beginning and end of the string, so I couldn't make /^women/.

What do I need to do if I want docs matching only 'women' gender, not 'men,women' ?

You probably need to change the analyzer. I assume you are using the default one here.
Maybe a keyword data type or a keyword analyzer would help.

Thank you. Keyword did the work, but with some addings.

Summary this is solution:

query: {
    bool: {
      must: {
        query_string: {
          query: "box",
          default_field: "title"
        }
      },
      filter: {
        bool: {
          should: [
            {term: {"gender.keyword": "women"}}
          ]
        }
      }
    }
    }

I need should as array for searching multiple genders if I will need it. For example, some docs have unisex gender, such as 'women,men'
Example with multiple genders:

query: {
    bool: {
      must: {
        query_string: {
          query: "box",
          default_field: "title"
        }
      },
      filter: {
        bool: {
          should: [
            {term: {"gender.keyword": "women"}},
            {term: {"gender.keyword": "kids"}} 
            #summary it may be gender 'girls'
          ]
        }
      }
    }
    }

Could be better may be to index gender as ["male", "female"] instead of "male,female".

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