Skip to content

Instantly share code, notes, and snippets.

@jsbonline2006
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsbonline2006/8817443 to your computer and use it in GitHub Desktop.
Save jsbonline2006/8817443 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, spider man"
]
}
}
}
}
},
"mappings": {
"test": {
"properties": {
"text": {
"type": "string",
"index_analyzer": "standard",
"search_analyzer": "synonym",
"expand":"true"
}
}
}
}
}'
# 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": "spider man eat insects"}'
# working query, finds correct document #1
curl -XPOST 'http://localhost:9200/syns/test/_search?pretty' -d '{"query": {"match": {"text": "spider man"}}}'
curl -XPOST 'http://localhost:9200/syns/test/_search?pretty' -d '{"query": {"match": {"text": "spiderman"}}}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment