Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@clintongormley
Created August 20, 2011 10:14
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save clintongormley/4193a7d63af534d3b0ef to your computer and use it in GitHub Desktop.
Save clintongormley/4193a7d63af534d3b0ef to your computer and use it in GitHub Desktop.
##########################################################################################
# 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
# }
@outsmartin
Copy link

good stuff, helped me a lot

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