# Unexpected Behavior of OR Match Query With Synonym Graph

**URL:** <https://discuss.elastic.co/t/unexpected-behavior-of-or-match-query-with-synonym-graph/344320>\
**Category:** Elasticsearch\
**Created:** [October 3, 2023, 2:17pm UTC](https://discuss.elastic.co/t/unexpected-behavior-of-or-match-query-with-synonym-graph/344320 "2023-10-03T14:17:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MilanGatyas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/milangatyas/32/81894_2.png) [@MilanGatyas](https://discuss.elastic.co/u/MilanGatyas)\
**Post date:** [October 3, 2023, 2:17pm UTC](https://discuss.elastic.co/t/unexpected-behavior-of-or-match-query-with-synonym-graph/344320/1 "2023-10-03T14:17:55Z")

</div>

I don't know if the following behavior is intended or not. See the following example of index definition and documents:

```auto
PUT /test
{
  "settings": {
    "analysis": {
      "filter": {
          "syn": {
            "synonyms": ["ysl, yves saint laurent"],
            "type": "synonym_graph"
          }
      },
      "analyzer": {
        "index": {
          "type": "custom", 
          "tokenizer": "standard",
          "filter": ["lowercase", "asciifolding", "trim"]
        },
        "query": {
          "type": "custom", 
          "tokenizer": "standard",
          "filter": ["lowercase", "asciifolding", "trim", "syn"]
        }
      }
    }
  }, 
  "mappings": {
    "properties": {
      "field1": { "type": "text", "analyzer": "index", "search_analyzer": "query" }
    }
  }
}

POST test/_doc/1
{
  "field1": "saint nicolas"
}

POST test/_doc/2
{
  "field1": "new ysl bag"
}

POST test/_doc/3
{
  "field1": "new yves saint laurent shoes"
}

```

When I run query

```auto
GET test/_search
{
  "query": {
    "match": {
      "field1": {
        "query": "yves saint laurent",
        "operator": "or"
      }
    }
  }
}

```

I get back documents 2 and 3. Not 1. Why not 1? Since I specified operator **OR** , should not presence of the token saint be enough to return document 1?

If I run query

```auto
GET test/_search
{
  "query": {
    "match": {
      "field1": {
        "query": "yves saint",
        "operator": "or"
      }
    }
  }
}

```

I do get back documents 1 and 3, which is expected.

Just for info, when I run

```auto
GET test/_analyze
{
  "analyzer": "query",
  "text": "yves saint laurent"
}

```

I get back

```auto
{
  "tokens" : [
    {
      "token" : "ysl",
      "start_offset" : 0,
      "end_offset" : 18,
      "type" : "SYNONYM",
      "position" : 0,
      "positionLength" : 3
    },
    {
      "token" : "yves",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "saint",
      "start_offset" : 5,
      "end_offset" : 10,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "laurent",
      "start_offset" : 11,
      "end_offset" : 18,
      "type" : "<ALPHANUM>",
      "position" : 2
    }
  ]
}

```

So I can see there token saint being present, that makes me unsure whether behavior described above is expected or not.

I am using Elasticsearch 7.10

---

<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:** [October 3, 2023, 3:52pm UTC](https://discuss.elastic.co/t/unexpected-behavior-of-or-match-query-with-synonym-graph/344320/2 "2023-10-03T15:52:17Z")

</div>

I reproduced the behavior with 8.10.2, which you should use BTW as it has a brand new API for synonyms management 😉

It looks like a bug to me, but let me check that a bit more... I'll get back to you.

Thanks for the detailed report!

---

<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:** [October 3, 2023, 4:09pm UTC](https://discuss.elastic.co/t/unexpected-behavior-of-or-match-query-with-synonym-graph/344320/3 "2023-10-03T16:09:55Z")

</div>

I appears that this is giving a clue:

```auto
GET test/_validate/query?rewrite=true
{
  "query": {
    "match": {
      "field1": {
        "query": "yves saint laurent"
      }
    }
  }
}

```

Which gives how the query is actually rewritten:

```auto
{
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "valid": true,
  "explanations": [
    {
      "index": "test",
      "valid": true,
      "explanation": "field1:ysl field1:\"yves saint laurent\""
    }
  ]
}

```

As `yves saint laurent` is exactly a synonym, the query is rewritten as a `phrase` query.  
If you try with `saint laurent`, it will work as you were expecting.

If you change the query to this:

```auto
GET test/_validate/query?rewrite=true
{
  "query": {
    "match": {
      "field1": {
        "query": "yves saint laurent saint"
      }
    }
  }
}

```

This will produce:

```auto
{
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "valid": true,
  "explanations": [
    {
      "index": "test",
      "valid": true,
      "explanation": "field1:ysl field1:\"yves saint laurent\" field1:saint"
    }
  ]
}

```

I'd say that's a "side effect" of the the way the `synonym_graph` is working behind the scene. But I'm not sure there's anything to do to "fix" that at it seems to be expected...

---

<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:** [October 4, 2023, 9:45am UTC](https://discuss.elastic.co/t/unexpected-behavior-of-or-match-query-with-synonym-graph/344320/4 "2023-10-04T09:45:03Z")

</div>

And my colleague @jpountz just told me about `auto_generate_phrase_queries`.  
Could you try with this option set to `false`?

> **[Match query | Elasticsearch Guide \[8.10\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#match-field-params)**

---

<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:** [November 1, 2023, 9:45am UTC](https://discuss.elastic.co/t/unexpected-behavior-of-or-match-query-with-synonym-graph/344320/5 "2023-11-01T09:45:44Z")

</div>

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