# Urgent Help! Synonym search is not working as expected

**URL:** <https://discuss.elastic.co/t/urgent-help-synonym-search-is-not-working-as-expected/22306>\
**Category:** Elasticsearch\
**Created:** [February 21, 2015, 10:00am UTC](https://discuss.elastic.co/t/urgent-help-synonym-search-is-not-working-as-expected/22306 "2015-02-21T10:00:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Santosh\_Kumar](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@Santosh\_Kumar](https://discuss.elastic.co/u/Santosh_Kumar)\
**Post date:** [February 21, 2015, 10:00am UTC](https://discuss.elastic.co/t/urgent-help-synonym-search-is-not-working-as-expected/22306/1 "2015-02-21T10:00:49Z")

</div>

We wanted to use the synonym concept of elasticsearch for our text search.  
Problem illustration:  
We are creating synonym analyzer during the index creation.  
String synonymsPath = "./word\_synonyms.txt";  
Settings settings =  
ImmutableSettings.settingsBuilder().loadFromSource(XContentFactory.jsonBuilder()  
.startObject()  
.startObject("analysis")  
.startObject("filter")  
.startObject("word\_filter\_synonyms")  
.field("type","synonym")  
.field("synonyms\_path",synonymsPath)  
.field("ignore\_case",true)  
.field("expand",true)  
.endObject()  
.endObject()  
.startObject("analyzer")  
.startObject("word\_synonym")  
.field("tokenizer","whitespace")  
.field("filter", new String[] {"lowercase", "word\_filter\_synonyms"})  
.endObject()  
.endObject()  
.endObject()  
.endObject().string()).build();  
CreateIndexRequestBuilder createIndexReqBuilder =  
client.admin().indices().prepareCreate(indexName).setSettings(settings);  
createIndexReqBuilder.execute().actionGet();

```
                    //Adding index items
                    getEsClient().prepareIndex(indexName, 

```

".percolator", word)  
.setSource(XContentFactory.jsonBuilder()  
.startObject()  
.field("query",  
QueryBuilders.matchPhraseQuery("content",  
word).analyzer("word\_synonym")) //  
Registering the query  
.endObject())  
.setRefresh(true)  
.execute().actionGet();

With above code snippet, we have indexed a word "webservice"

Now, we have used highlighter during search as well, which highlights all  
the word and return the actual synonym.  
DocBuilder docBuilder =  
PercolateSourceBuilder.docBuilder().setDoc(XContentFactory.jsonBuilder()  
.startObject().field("content", text).endObject());  
PercolateResponse response = client.preparePercolate()  
.setIndices(indexName).setDocumentType("type")  
.setSize(100)  
.setPercolateDoc(docBuilder)  
.setHighlightBuilder(new HighlightBuilder().field("content",0,0))  
.setScore(true)  
.execute().actionGet();

List matchedKeyords = new ArrayList();  
for(PercolateResponse.Match match : response) {  
Iterator iter = match.getHighlightFields().entrySet().iterator();  
}

Our synonym text file formats we tried are

_webservice webservices web-service_

_webservice, webservices, web-service_

\*Now when you search for text like "Synonym testing webservices web-service  
and web service". results are not correct and not consistent. \*  
_It randomly highlights web-service and web service. But not webservices.  
Sometimes it done only with webservices..._  
\*We are very much confused on the behavior. What we have felt is that it  
might work okey when there are no spaces in your synonym and index items. \*

\*Please suggest on how to proceed. \*

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/32a0cc64-2b72-49c1-b13f-9060804e0879%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/32a0cc64-2b72-49c1-b13f-9060804e0879%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Santosh\_Kumar](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@Santosh\_Kumar](https://discuss.elastic.co/u/Santosh_Kumar)\
**Post date:** [February 25, 2015, 6:53am UTC](https://discuss.elastic.co/t/urgent-help-synonym-search-is-not-working-as-expected/22306/2 "2015-02-25T06:53:32Z")

</div>

Hello???? Any suggestion would be greatly appreciated....

On Saturday, February 21, 2015 at 3:30:49 PM UTC+5:30, Santosh Kumar wrote:

> We wanted to use the synonym concept of elasticsearch for our text search.  
> Problem illustration:  
> We are creating synonym analyzer during the index creation.  
> String synonymsPath = "./word\_synonyms.txt";  
> Settings settings =  
> ImmutableSettings.settingsBuilder().loadFromSource(XContentFactory.jsonBuilder()  
> .startObject()  
> .startObject("analysis")  
> .startObject("filter")  
> .startObject("word\_filter\_synonyms")  
> .field("type","synonym")  
> .field("synonyms\_path",synonymsPath)  
> .field("ignore\_case",true)  
> .field("expand",true)  
> .endObject()  
> .endObject()  
> .startObject("analyzer")  
> .startObject("word\_synonym")  
> .field("tokenizer","whitespace")  
> .field("filter", new String {"lowercase", "word\_filter\_synonyms"})  
> .endObject()  
> .endObject()  
> .endObject()  
> .endObject().string()).build();  
> CreateIndexRequestBuilder createIndexReqBuilder =  
> client.admin().indices().prepareCreate(indexName).setSettings(settings);  
> createIndexReqBuilder.execute().actionGet();
> 
> ```
> //Adding index items
> getEsClient().prepareIndex(indexName, 
> 
> ```
> 
> ".percolator", word)  
> .setSource(XContentFactory.jsonBuilder()  
> .startObject()  
> .field("query",  
> QueryBuilders.matchPhraseQuery("content",  
> word).analyzer("word\_synonym")) //  
> Registering the query  
> .endObject())  
> .setRefresh(true)  
> .execute().actionGet();
> 
> With above code snippet, we have indexed a word "webservice"
> 
> Now, we have used highlighter during search as well, which highlights all  
> the word and return the actual synonym.  
> DocBuilder docBuilder =  
> PercolateSourceBuilder.docBuilder().setDoc(XContentFactory.jsonBuilder()  
> .startObject().field("content", text).endObject());  
> PercolateResponse response = client.preparePercolate()  
> .setIndices(indexName).setDocumentType("type")  
> .setSize(100)  
> .setPercolateDoc(docBuilder)  
> .setHighlightBuilder(new HighlightBuilder().field("content",0,0))  
> .setScore(true)  
> .execute().actionGet();
> 
> List matchedKeyords = new ArrayList();  
> for(PercolateResponse.Match match : response) {  
> Iterator iter = match.getHighlightFields().entrySet().iterator();  
> }
> 
> Our synonym text file formats we tried are
> 
> _webservice webservices web-service_
> 
> _webservice, webservices, web-service_
> 
> \*Now when you search for text like "Synonym testing webservices  
> web-service and web service". results are not correct and not consistent. \*  
> _It randomly highlights web-service and web service. But not webservices.  
> Sometimes it done only with webservices..._  
> \*We are very much confused on the behavior. What we have felt is that it  
> might work okey when there are no spaces in your synonym and index items. \*
> 
> \*Please suggest on how to proceed. \*

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/880b09d8-395c-4036-bcad-be2be5e4dab6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/880b09d8-395c-4036-bcad-be2be5e4dab6%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 12:30am UTC](https://discuss.elastic.co/t/urgent-help-synonym-search-is-not-working-as-expected/22306/3 "2017-07-06T00:30:19Z")

</div>


