# Full match of query value from beginning to end of field

**URL:** <https://discuss.elastic.co/t/full-match-of-query-value-from-beginning-to-end-of-field/212979>\
**Category:** Elasticsearch\
**Created:** [December 24, 2019, 7:21pm UTC](https://discuss.elastic.co/t/full-match-of-query-value-from-beginning-to-end-of-field/212979 "2019-12-24T19:21:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unavailabl3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/unavailabl3/32/59938_2.png) [@unavailabl3](https://discuss.elastic.co/u/unavailabl3)\
**Post date:** [December 24, 2019, 7:21pm UTC](https://discuss.elastic.co/t/full-match-of-query-value-from-beginning-to-end-of-field/212979/1 "2019-12-24T19:21:18Z")

</div>

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' ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 24, 2019, 9:19pm UTC](https://discuss.elastic.co/t/full-match-of-query-value-from-beginning-to-end-of-field/212979/2 "2019-12-24T21:19:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![unavailabl3](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/unavailabl3/32/59938_2.png) [@unavailabl3](https://discuss.elastic.co/u/unavailabl3)\
**Post date:** [December 25, 2019, 7:55am UTC](https://discuss.elastic.co/t/full-match-of-query-value-from-beginning-to-end-of-field/212979/3 "2019-12-25T07:55:11Z")

</div>

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'
          ]
        }
      }
    }
    }
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 25, 2019, 1:03pm UTC](https://discuss.elastic.co/t/full-match-of-query-value-from-beginning-to-end-of-field/212979/4 "2019-12-25T13:03:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 22, 2020, 1:03pm UTC](https://discuss.elastic.co/t/full-match-of-query-value-from-beginning-to-end-of-field/212979/5 "2020-01-22T13:03:36Z")

</div>

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