Last active
January 8, 2021 13:20
-
-
Save mattweber/7374591 to your computer and use it in GitHub Desktop.
Multi-word query time synonyms in elasticsearch.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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"}}}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
andman
, such as: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