Skip to content

Instantly share code, notes, and snippets.

@clintongormley
Created May 7, 2012 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clintongormley/2626785 to your computer and use it in GitHub Desktop.
Save clintongormley/2626785 to your computer and use it in GitHub Desktop.
# Create your mapping
# -------------------
curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1' -d '
{
"mappings" : {
"test" : {
"properties" : {
"lang" : {
"fields" : {
"base" : {
"omit_term_freq_and_positions" : 1,
"omit_norms" : 1,
"type" : "string",
"analyzer" : "base_lang"
},
"lang" : {
"index" : "not_analyzed",
"omit_term_freq_and_positions" : 1,
"omit_norms" : 1,
"type" : "string"
}
},
"type" : "multi_field"
}
}
}
},
"settings" : {
"analysis" : {
"analyzer" : {
"base_lang" : {
"tokenizer" : "base_lang"
}
},
"tokenizer" : {
"base_lang" : {
"group" : 0,
"pattern" : "^(\\w\\w)",
"type" : "pattern"
}
}
}
}
}
'
curl -XPUT 'http://127.0.0.1:9200/test/test/1' -d '{ "lang" : "en" }'
curl -XPUT 'http://127.0.0.1:9200/test/test/2' -d '{ "lang" : "en_GB" }'
curl -XPUT 'http://127.0.0.1:9200/test/test/3' -d '{ "lang" : "en_US" }'
curl -XPUT 'http://127.0.0.1:9200/test/test/4' -d '{ "lang" : "es" }'
curl -XPUT 'http://127.0.0.1:9200/test/test/5' -d '{ "lang" : "es_ES" }'
# Check what terms have been indexed in lang and lang.base
# ---------------------------------------------------------
curl -XGET 'http://127.0.0.1:9200/test/_search?pretty=1' -d '
{
"facets" : {
"base" : {
"terms" : {
"field" : "lang.base"
}
},
"lang" : {
"terms" : {
"field" : "lang"
}
}
},
"size" : 0
}
'
# {
# "hits" : {
# "hits" : [],
# "max_score" : 1,
# "total" : 5
# },
# "timed_out" : false,
# "_shards" : {
# "failed" : 0,
# "successful" : 5,
# "total" : 5
# },
# "facets" : {
# "base" : {
# "other" : 0,
# "terms" : [
# {
# "count" : 3,
# "term" : "en"
# },
# {
# "count" : 2,
# "term" : "es"
# }
# ],
# "missing" : 0,
# "_type" : "terms",
# "total" : 5
# },
# "lang" : {
# "other" : 0,
# "terms" : [
# {
# "count" : 1,
# "term" : "es_ES"
# },
# {
# "count" : 1,
# "term" : "es"
# },
# {
# "count" : 1,
# "term" : "en_US"
# },
# {
# "count" : 1,
# "term" : "en_GB"
# },
# {
# "count" : 1,
# "term" : "en"
# }
# ],
# "missing" : 0,
# "_type" : "terms",
# "total" : 5
# }
# },
# "took" : 4
# }
# Run a search, with priority en_GB > en > *
# ------------------------------------------
curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1' -d '
{
"query" : {
"custom_filters_score" : {
"query" : {
"match_all" : {}
},
"filters" : [
{
"boost" : "3",
"filter" : {
"term" : {
"lang" : "en_GB"
}
}
},
{
"boost" : "2",
"filter" : {
"term" : {
"lang.base" : "en"
}
}
}
]
}
}
}
'
# {
# "hits" : {
# "hits" : [
# {
# "_source" : {
# "lang" : "en_GB"
# },
# "_score" : 3,
# "_index" : "test",
# "_id" : "2",
# "_type" : "test"
# },
# {
# "_source" : {
# "lang" : "en"
# },
# "_score" : 2,
# "_index" : "test",
# "_id" : "1",
# "_type" : "test"
# },
# {
# "_source" : {
# "lang" : "en_US"
# },
# "_score" : 2,
# "_index" : "test",
# "_id" : "3",
# "_type" : "test"
# },
# {
# "_source" : {
# "lang" : "es"
# },
# "_score" : 1,
# "_index" : "test",
# "_id" : "4",
# "_type" : "test"
# },
# {
# "_source" : {
# "lang" : "es_ES"
# },
# "_score" : 1,
# "_index" : "test",
# "_id" : "5",
# "_type" : "test"
# }
# ],
# "max_score" : 3,
# "total" : 5
# },
# "timed_out" : false,
# "_shards" : {
# "failed" : 0,
# "successful" : 5,
# "total" : 5
# },
# "took" : 5
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment