Skip to content

Instantly share code, notes, and snippets.

@tomchuk
Last active January 14, 2016 19:44
Show Gist options
  • Save tomchuk/aeab3e39f72b4f2943ab to your computer and use it in GitHub Desktop.
Save tomchuk/aeab3e39f72b4f2943ab to your computer and use it in GitHub Desktop.
Querying nested document multi_fields in elasticsearch
index:
analysis:
analyzer:
ngram_index:
tokenizer: standard
filter: ["standard", "lowercase", "ngram_filter"]
ngram_search:
tokenizer: standard
filter: ["standard", "lowercase"]
filter:
ngram_filter:
type: nGram
min_gram: 2
max_gram: 8
#!/bin/sh
echo "\nDELETING INDEX: "
curl -XDELETE localhost:9200/test
echo "\nPUTTING INDEX: "
curl -XPUT localhost:9200/test
echo "\nPUTTING MAPPING: "
curl -XPUT localhost:9200/test/outer/_mapping -d '{
"outer": {
"properties": {
"name": {
"type": "multi_field",
"fields": {
"name": {
"analyzer": "snowball",
"index": "analyzed",
"term_vector": "with_positions_offsets",
"type": "string"
},
"name_n": {
"index": "analyzed",
"index_analyzer": "ngram_index",
"search_analyzer": "ngram_search",
"type": "string"
}
}
},
"mid": {
"type": "nested",
"properties": {
"name": {
"type": "multi_field",
"fields": {
"name": {
"analyzer": "snowball",
"index": "analyzed",
"term_vector": "with_positions_offsets",
"type": "string"
},
"name_n": {
"index": "analyzed",
"index_analyzer": "ngram_index",
"search_analyzer": "ngram_search",
"type": "string"
}
}
},
"inner": {
"type": "nested",
"properties": {
"name": {
"type": "multi_field",
"fields": {
"name": {
"analyzer": "snowball",
"index": "analyzed",
"term_vector": "with_positions_offsets",
"type": "string"
},
"name_n": {
"index": "analyzed",
"index_analyzer": "ngram_index",
"search_analyzer": "ngram_search",
"type": "string"
}
}
}
}
}
}
}
}
}
}'
echo "\nPUTTING DOC: "
curl -XPUT localhost:9200/test/outer/1 -d '
{
"name": "Robert Smith",
"mid": {
"name": "Jason Jones",
"inner": [
{"name": "Sarah Summer"},
{"name": "Wendy Winter"}
]
}
}'
echo "\nREFRESHING INDEX: "
curl -XPOST localhost:9200/test/_refresh
echo "\nSEARCHING outer snowball 'smith': "
curl -XPOST localhost:9200/test/outer/_search -d '{
"query": {
"match": {
"name": {
"query": "smith",
"operator": "and"
}
}
}
}'
echo "\nSEARCHING mid snowball 'jones': "
curl -XPOST localhost:9200/test/outer/_search -d '{
"query": {
"nested": {
"path": "mid",
"query": {
"match": {
"mid.name": {
"query": "jones",
"operator": "and"
}
}
}
}
}
}'
echo "\nSEARCHING inner snowball 'summer': "
curl -XPOST localhost:9200/test/outer/_search -d '{
"query": {
"nested": {
"path": "mid.inner",
"query": {
"match": {
"mid.inner.name": {
"query": "summer",
"operator": "and"
}
}
}
}
}
}'
echo "\nSEARCHING outer ngram 'mit': "
curl -XPOST localhost:9200/test/outer/_search -d '{
"query": {
"match": {
"name_n": {
"query": "mit",
"operator": "and"
}
}
}
}'
echo "\nSEARCHING mid ngram 'one': "
curl -XPOST localhost:9200/test/outer/_search -d '{
"query": {
"nested": {
"path": "mid",
"query": {
"match": {
"mid.name.name_n": {
"query": "one",
"operator": "and"
}
}
}
}
}
}'
echo "\nSEARCHING inner ngram 'mme': "
curl -XPOST localhost:9200/test/outer/_search -d '{
"query": {
"nested": {
"path": "mid.inner",
"query": {
"match": {
"mid.inner.name.name_n": {
"query": "mme",
"operator": "and"
}
}
}
}
}
}'
echo "\nSEARCHING mid ngram 'one' (the wrong way): "
curl -XPOST localhost:9200/test/outer/_search -d '{
"query": {
"nested": {
"path": "mid",
"query": {
"match": {
"mid.name_n": {
"query": "one",
"operator": "and"
}
}
}
}
}
}'
echo "\nSEARCHING inner ngram 'mme' (the wrong way): "
curl -XPOST localhost:9200/test/outer/_search -d '{
"query": {
"nested": {
"path": "mid.inner",
"query": {
"match": {
"mid.inner.name_n": {
"query": "mme",
"operator": "and"
}
}
}
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment