Skip to content

Instantly share code, notes, and snippets.

@mattweber
Last active January 8, 2021 13:20
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mattweber/7374591 to your computer and use it in GitHub Desktop.
Save mattweber/7374591 to your computer and use it in GitHub Desktop.
Multi-word query time synonyms in elasticsearch.
# delete old index if exists
curl -XDELETE 'http://localhost:9200/syns?pretty'
# create index with synonym analyzer and mapping
curl -XPUT 'http://localhost:9200/syns?pretty' -d '{
"settings" : {
"number_of_replicas": 0,
"number_of_shards": 1,
"index": {
"analysis": {
"analyzer": {
"synonym": {
"tokenizer": "standard",
"filter": ["standard", "lowercase", "stop", "synonym"]
}
},
"filter": {
"synonym": {
"type": "synonym",
"synonyms": [
"spider man => spiderman"
]
}
}
}
}
},
"mappings": {
"test": {
"properties": {
"text": {"type": "string", "index_analyzer": "standard", "search_analyzer": "synonym"}
}
}
}
}'
# index the test documents
curl -XPUT 'http://localhost:9200/syns/test/1?pretty' -d '{"text": "the adventures of spiderman"}'
curl -XPUT 'http://localhost:9200/syns/test/2?pretty' -d '{"text": "what hath man wrought?"}'
curl -XPUT 'http://localhost:9200/syns/test/3?pretty&refresh=true' -d '{"text": "spiders eat insects"}'
# working query, finds correct document #1
curl -XPOST 'http://localhost:9200/syns/test/_search?pretty' -d '{"query": {"match": {"text": "spider man"}}}'
@mattweber
Copy link
Author

Does elasticsearch have the same query-time synonym problem as Solr? Nope!

http://www.opensourceconnections.com/2013/10/27/why-is-multi-term-synonyms-so-hard-in-solr/

@jprante
Copy link

jprante commented Nov 8, 2013

BTW the trick is changing to synonym analyzer only at search time.

It also works with tokenizer "whitespace".

To be fair, this solution should also work with Solr, because Solr can also set up different index and query analyzer. But ES "match" query is by far more convenient :)

@mattweber
Copy link
Author

@jprante Actually it doesn't work in Solr because of the dismax query parser that always splits on whitespace during query parsing before analyzing it. We have the same issue in elasticsearch using the QueryString Query because it also splits on whitespace. This works since the Match Query sends directly to the analyzer and does not attempt any query parsing.

@JnBrymn
Copy link

JnBrymn commented Nov 9, 2013

Wow. Interesting. Solr's problem lies on line 301 of o.a.l.queryparse.classic.QueryParser. This is a Lucene class. So apparently ES doesn't use lucene's query parser. While I continue to be impressed with ES, I'm still not certain this is an apple-apple comparison. Once I recover from my vacation maybe I'll have a chance to look into this more. Cool stuff, man.

@ianribas
Copy link

ianribas commented Apr 2, 2015

I know it's been a while, but for others that end up here searching: this only works because @mattweber used contraction on his synonym.

If a document were to contain the words spider and man, such as:

curl -XPUT 'http://localhost:9200/multiwordsyns/test/3?pretty' -d '{
    "text": "that spider is the size of a man"
}'

It would not be found by the query "spider man".

I've created another gist, based on this one, that expands on this issue a little: https://gist.github.com/ianribas/f76d20c21bb9f5c0df2f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment