# Match query doesn't work well with Genre Expansion Synonyms

**URL:** <https://discuss.elastic.co/t/match-query-doesnt-work-well-with-genre-expansion-synonyms/197272>\
**Category:** Elasticsearch\
**Created:** [August 29, 2019, 7:45am UTC](https://discuss.elastic.co/t/match-query-doesnt-work-well-with-genre-expansion-synonyms/197272 "2019-08-29T07:45:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fong\_Ng](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fong_ng/32/21533_2.png) [@Fong\_Ng](https://discuss.elastic.co/u/Fong_Ng)\
**Post date:** [August 29, 2019, 7:45am UTC](https://discuss.elastic.co/t/match-query-doesnt-work-well-with-genre-expansion-synonyms/197272/1 "2019-08-29T07:45:46Z")

</div>

Elasticsearch 7.2, node.js

Synonym data:

`["khaki => khaki,green", "cat => cat,pet"]`

Index mapping:

PutMapping content:

```
{
    settings: {
        "analysis": {
            "char_filter": {
                "same_word": {
                    "type": "mapping",
                    "mappings": ["-=>", "&=>and"]
                },
            },
            "filter": {
                "my_stopwords": {
                    "type": "stop",
                    "stopwords": STOPWORD_FILE
                },
                "my_synonym": {
                    "type": "synonym",
                    "synonyms": ["khaki => khaki,green", "cat => cat,pet"],
                    "tokenizer": "whitespace"
                },
            },
            "analyzer": {
                "lowercaseWhiteSpaceAnalyzer": {
                    "type": "custom",
                    "char_filter": ["html_strip", "same_word"],
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase",
                        "my_stopwords",
                        "my_synonym",
                    ]
                },
            }
        }
    }
}

```

Material field:

```
"phone_case":{"type":"text","norms":false,"analyzer":"lowercaseWhiteSpaceAnalyzer"}

```

Example documents:

```auto
 [
  {
      id: "1",
      phone_case: "khaki,brushed and polished",
  },
  {
      id: "2",
      phone_case: "green,brushed",
  },
  {
      id: "3",
      phone_case: "black,matte"
  }
]

```

The "phone\_case" field is a text field.

When I search for **khaki** I want to find documents with just **khaki** results, excluding any results that contain **green**. On the other hand, when searching for **green** , I want to get documents with either **green** or **khaki**. That should be what **Genre Expansion** is supposed to do.

The term level query works fine for such purposes:

```auto
{
  "sort": [
    {
      "updated": {
        "order": "desc"
      }
    }
  ],
  "size": 10,
  "from": 0,
  "query": {
    "bool": {
      "filter": {
        "term": {
          "phone_case": "khaki"
        }
      }
    }
  }

```

It manages to return just documents with **khaki**.

But with **match\_phrase** , it returns documents with either **khaki** or **green**. That isn't what I expected. I want to get documents that contains **khaki** , not **green** :

```auto
{
  "sort": [
    {
      "updated": {
        "order": "desc"
      }
    }
  ],
  "size": 10,
  "from": 0,
  "query": {
    "match_phrase": {
      "phone_case": "khaki"
    }
  }
}

```

Could anyone tell me what's wrong with the **match** query not able to exclude results that contain "green"? I want to allow uses to look up the text field in exact order but **match** or **match\_phrase** doesn't work well with Genre Expansion Synonyms.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 29, 2019, 2:35pm UTC](https://discuss.elastic.co/t/match-query-doesnt-work-well-with-genre-expansion-synonyms/197272/2 "2019-08-29T14:35:24Z")

</div>

You have configured an `analyzer` for the `phone_case` field, but no explicit `search_analyzer`. As a result, Elasticsearch will apply the same analyzer to your query terms - as long as you are not using term queries.

So when you search for `khaki` with a `match` or `match_phrase` query you are actually going to search for both `khaki` as well as `green` because that's what your `khaki` search term is going to be expanded to. This doesn't happen when you use the `term` query, as it does not analyze the search term.

How to solve this? Add a `search_analyzer` to the mapping for `phone_case`:

```auto
      "phone_case": {
        "type": "text",
        "norms": false,
        "analyzer": "lowercaseWhiteSpaceAnalyzer",
        "search_analyzer": "standard"
      }

```

Now, your search terms will not get any synonyms applied and genre expansion should work as expected.

(Instead of the `standard` analyzer like in my example above you may want to use a custom analyzer similar to `lowercaseWhiteSpaceAnalyzer`, but without the synonym filter)

---

<div class="post-metadata">

**Author:** ![Fong\_Ng](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fong_ng/32/21533_2.png) [@Fong\_Ng](https://discuss.elastic.co/u/Fong_Ng)\
**Post date:** [August 29, 2019, 11:25pm UTC](https://discuss.elastic.co/t/match-query-doesnt-work-well-with-genre-expansion-synonyms/197272/3 "2019-08-29T23:25:21Z")

</div>

> [@abdon](#):
>
> genre expansion

Thank you for the explanation. I have to use this mapping for `phone_case` instead

```auto
phone_case: {
    type: "text",
    norms: false,
    index_analyzer: "standard",
    search_analyzer: "lowercaseWhiteSpaceAnalyzer"
}

```

And change the synonym format from

`["khaki => khaki,green", "cat => cat,pet"]`

to

`["green => khaki,green", "pet => cat,pet"]`

Now the genre expansion is working properly in either "term" and "match\_phrase" queries.

---

<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:** [September 27, 2019, 6:53am UTC](https://discuss.elastic.co/t/match-query-doesnt-work-well-with-genre-expansion-synonyms/197272/5 "2019-09-27T06:53:45Z")

</div>

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