-
-
Save clintongormley/4193a7d63af534d3b0ef to your computer and use it in GitHub Desktop.
This file contains hidden or 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
########################################################################################## | |
# First, create the index, with the analyzer settings, and apply that analyzer to a field: | |
########################################################################################## | |
curl -XPUT 'http://127.0.0.1:9200/foo/?pretty=1' -d ' | |
{ | |
"mappings" : { | |
"bar" : { | |
"properties" : { | |
"test_field" : { | |
"type" : "string", | |
"analyzer" : "syns" | |
} | |
} | |
} | |
}, | |
"settings" : { | |
"analysis" : { | |
"filter" : { | |
"syns_filter" : { | |
"type" : "synonym", | |
"synonyms_path" : "/opt/elasticsearch/synonym.txt" | |
} | |
}, | |
"analyzer" : { | |
"syns" : { | |
"filter" : [ | |
"standard", | |
"lowercase", | |
"syns_filter" | |
], | |
"type" : "custom", | |
"tokenizer" : "standard" | |
} | |
} | |
} | |
} | |
} | |
' | |
####################################################################################### | |
# Check that the mapping looks correct: | |
####################################################################################### | |
curl -XGET 'http://127.0.0.1:9200/foo/_mapping?pretty=1' | |
# { | |
# "foo" : { | |
# "bar" : { | |
# "properties" : { | |
# "test_field" : { | |
# "type" : "string", | |
# "analyzer" : "syns" | |
# } | |
# } | |
# } | |
# } | |
# } | |
####################################################################################### | |
# Use the analyze API to test out the analyzer: | |
####################################################################################### | |
curl -XGET 'http://127.0.0.1:9200/foo/_analyze?pretty=1&text=mono&analyzer=syns' | |
# { | |
# "tokens" : [ | |
# { | |
# "end_offset" : 4, | |
# "position" : 1, | |
# "start_offset" : 0, | |
# "type" : "<ALPHANUM>", | |
# "token" : "mono" | |
# }, | |
# { | |
# "end_offset" : 4, | |
# "position" : 1, | |
# "start_offset" : 0, | |
# "type" : "<ALPHANUM>", | |
# "token" : "one-channel" | |
# }, | |
# { | |
# "end_offset" : 4, | |
# "position" : 1, | |
# "start_offset" : 0, | |
# "type" : "<ALPHANUM>", | |
# "token" : "one" | |
# }, | |
# { | |
# "end_offset" : 4, | |
# "position" : 1, | |
# "start_offset" : 0, | |
# "type" : "<ALPHANUM>", | |
# "token" : "1-channel" | |
# }, | |
# { | |
# "end_offset" : 4, | |
# "position" : 1, | |
# "start_offset" : 0, | |
# "type" : "<ALPHANUM>", | |
# "token" : "1" | |
# }, | |
# { | |
# "end_offset" : 4, | |
# "position" : 1, | |
# "start_offset" : 0, | |
# "type" : "<ALPHANUM>", | |
# "token" : "1" | |
# }, | |
# { | |
# "end_offset" : 4, | |
# "position" : 2, | |
# "start_offset" : 0, | |
# "type" : "<ALPHANUM>", | |
# "token" : "channel" | |
# }, | |
# { | |
# "end_offset" : 4, | |
# "position" : 2, | |
# "start_offset" : 0, | |
# "type" : "<ALPHANUM>", | |
# "token" : "channel" | |
# } | |
# ] | |
# } | |
####################################################################################### | |
# Index some data: | |
####################################################################################### | |
curl -XPOST 'http://127.0.0.1:9200/foo/bar?pretty=1' -d ' | |
{ | |
"test_field" : "UPPER FIELD FIRST" | |
} | |
' | |
# { | |
# "ok" : true, | |
# "_index" : "foo", | |
# "_id" : "Symcx_ysSMKgVZFZLbKW4g", | |
# "_type" : "bar", | |
# "_version" : 1 | |
# } | |
####################################################################################### | |
# Search for the existence of a synonym in the same field | |
####################################################################################### | |
curl -XGET 'http://127.0.0.1:9200/foo/bar/_search?pretty=1' -d ' | |
{ | |
"query" : { | |
"text" : { | |
"test_field" : "uff" | |
} | |
} | |
} | |
' | |
# { | |
# "hits" : { | |
# "hits" : [ | |
# { | |
# "_source" : { | |
# "test_field" : "UPPER FIELD FIRST" | |
# }, | |
# "_score" : 0.34307188, | |
# "_index" : "foo", | |
# "_id" : "Symcx_ysSMKgVZFZLbKW4g", | |
# "_type" : "bar" | |
# } | |
# ], | |
# "max_score" : 0.34307188, | |
# "total" : 1 | |
# }, | |
# "timed_out" : false, | |
# "_shards" : { | |
# "failed" : 0, | |
# "successful" : 5, | |
# "total" : 5 | |
# }, | |
# "took" : 3 | |
# } | |
####################################################################################### | |
# Search for the synonym in the _all field -> not found, because different analyzer | |
####################################################################################### | |
curl -XGET 'http://127.0.0.1:9200/foo/bar/_search?pretty=1' -d ' | |
{ | |
"query" : { | |
"text" : { | |
"_all" : "uff" | |
} | |
} | |
} | |
' | |
# { | |
# "hits" : { | |
# "hits" : [], | |
# "max_score" : null, | |
# "total" : 0 | |
# }, | |
# "timed_out" : false, | |
# "_shards" : { | |
# "failed" : 0, | |
# "successful" : 5, | |
# "total" : 5 | |
# }, | |
# "took" : 1 | |
# } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good stuff, helped me a lot