# PerFieldSimilarity for synonym expansion at query time

**URL:** https://discuss.elastic.co/t/perfieldsimilarity-for-synonym-expansion-at-query-time/94037
**Category:** Elasticsearch
**Created:** [July 21, 2017, 12:34am UTC](https://discuss.elastic.co/t/perfieldsimilarity-for-synonym-expansion-at-query-time/94037 "2017-07-21T00:34:13Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![guilherme\_maranhao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guilherme_maranhao/32/101483_2.png) [@guilherme\_maranhao](https://discuss.elastic.co/u/guilherme_maranhao)
#### Post date: [July 21, 2017, 12:34am UTC](https://discuss.elastic.co/t/perfieldsimilarity-for-synonym-expansion-at-query-time/94037/1 "2017-07-21T00:34:13Z")

</div>

Hi everybody,

It's said here ([https://www.elastic.co/guide/en/elasticsearch/guide/current/synonyms-expand-or-contract.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/synonyms-expand-or-contract.html)) that using simple synonym expansion at query time is an advantage for relevance. Please, consider my scenario:

My simple expansion synonym is:

`shirt, blouse`

The index structure is

```
  {
	"my_index": {
		"aliases": {},
		"mappings": {
			"my_type": {
				"properties": {
					"name": {
						"type": "text",
						"fields": {
							"keyword": {
								"type": "keyword",
								"ignore_above": 256
							}
						}
					}
				}
			}
		},
		"settings": {
			"index": {
				"analysis": {
					"filter": {
						"brazilian_stop": {
							"type": "stop",
							"stopwords": "_brazilian_"
						},
						"synonym_filter": {
							"type": "synonym",
							"synonyms_path": "sinonimos.txt"
						}
					},
					"analyzer": {
						"synonym_brazilian_analyzer": {
							"filter": [
								"lowercase",
								"asciifolding",
								"synonym_filter",
								"brazilian_stop"
							],
						"tokenizer": "standard"
						}
					}
				}
			}
		}
	}
}

```

Note that I'm not applying the synonym analyzer at index time.

And only 3 documents:

```
_id = 1
{
    name: "shirt xyz" 
}

_id = 2
{
   name: "blouse xyz" 
}

_id = 3
{
    name: "blouse wvc"
}

```

My query is

```
  {
	"query":
    {
    	"query_string":
        {
        	"fields":["name"], 
        	"query":"shirt", 
        	"analyzer":"synonym_brazilian_analyzer"
        }
    }
}

```

If I search for "shirt", applying the synonym analyzer just at query time, shouldn't the \_id=1 document have higher score than the \_id=2 one?

I'm asking that because, according to the explain clause, both have exactly the same score.  
My point is: what exactly is the query time advantage for relevance considering simple expansion?  
What about the PerFieldSimilarity calculation?  
Shouldn't the "shirt xyz" text have more relevance than the "blouse xyz", at query time?

Thanks a lot,

Guilherme

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [July 21, 2017, 2:01pm UTC](https://discuss.elastic.co/t/perfieldsimilarity-for-synonym-expansion-at-query-time/94037/2 "2017-07-21T14:01:19Z")

</div>

I get the same score for all documents when I run the following:

```auto
DELETE index

PUT index 
{
  "settings": {
    "number_of_shards": 1, 
    "analysis": {
      "filter": {
        "my_syns": {
          "type": "synonym",
          "synonyms" : [
            "shirt,blouse"
          ]
        }
      },
      "analyzer": {
        "my_index_analyzer": {
          "type": "custom",
          "tokenizer": "whitespace"
        },
        "my_search_analyzer": {
          "type": "custom",
          "tokenizer": "whitespace",
          "filter": ["my_syns"]
        }
      }
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "field": {
          "type": "text",
          "analyzer": "my_index_analyzer",
          "search_analyzer": "my_search_analyzer"
        }
      }
    }
  }
}

PUT index/doc/1
{
  "field": "shirt xyz"
}

PUT index/doc/2
{
  "field": "blouse xyz"
}

GET index/_search
{
  "query": {
    "match": {
      "field": "shirt"
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![guilherme\_maranhao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guilherme_maranhao/32/101483_2.png) [@guilherme\_maranhao](https://discuss.elastic.co/u/guilherme_maranhao)
#### Post date: [July 21, 2017, 3:33pm UTC](https://discuss.elastic.co/t/perfieldsimilarity-for-synonym-expansion-at-query-time/94037/3 "2017-07-21T15:33:29Z")

</div>

But, is that the expected?

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [July 21, 2017, 3:35pm UTC](https://discuss.elastic.co/t/perfieldsimilarity-for-synonym-expansion-at-query-time/94037/4 "2017-07-21T15:35:11Z")

</div>

To me it is. We do this on purpose on the Lucene side by using a `SynonymQuery` which merges statistics in order to make sure which synonym is used does not matter.

---

<div class="post-metadata">

### Author: ![guilherme\_maranhao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guilherme_maranhao/32/101483_2.png) [@guilherme\_maranhao](https://discuss.elastic.co/u/guilherme_maranhao)
#### Post date: [July 21, 2017, 3:37pm UTC](https://discuss.elastic.co/t/perfieldsimilarity-for-synonym-expansion-at-query-time/94037/5 "2017-07-21T15:37:16Z")

</div>

Ok, but what about the advantage for analyzing it at query time mentioned here [Expand or contract | Elasticsearch: The Definitive Guide [2.x] | Elastic](https://www.elastic.co/guide/en/elasticsearch/guide/current/synonyms-expand-or-contract.html) ?

> The IDF for each synonym will be correct.

Thank you!

---

<div class="post-metadata">

### Author: ![guilherme\_maranhao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guilherme_maranhao/32/101483_2.png) [@guilherme\_maranhao](https://discuss.elastic.co/u/guilherme_maranhao)
#### Post date: [July 21, 2017, 8:08pm UTC](https://discuss.elastic.co/t/perfieldsimilarity-for-synonym-expansion-at-query-time/94037/6 "2017-07-21T20:08:16Z")

</div>

Hi,

I got it! The IDF at query time considers the relevance of each of the synonyms in the whole index not only the specific term that is being searched.  
So, the documents which contain the most relevant synonym word will have the highest scores.

Thank you

---

<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: [August 18, 2017, 8:08pm UTC](https://discuss.elastic.co/t/perfieldsimilarity-for-synonym-expansion-at-query-time/94037/7 "2017-08-18T20:08:22Z")

</div>

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