Created
November 22, 2013 08:24
-
-
Save alexbrasetvik/7596633 to your computer and use it in GitHub Desktop.
Simple example of nested mappings, with queries that require a key to match as well.
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
# Here you could have various analyzers for different string-types. | |
# "text_value" and "name_value" should probably have different ones. |
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
_id: 12345678 | |
fields: | |
- key: book_title | |
text_value: "Harry Potter" | |
- key: price | |
float_value: 34.50 | |
--- | |
_id: 123456790 | |
fields: | |
- key: blog_title | |
text_value: "My awesome blog" | |
- key: author | |
name_value: "John Smith" | |
# Just showing that both key and value must match above. | |
- key: not_book_title | |
text_value: "This blog is not about Harry Potter" | |
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
type: | |
properties: | |
fields: | |
type: nested | |
properties: | |
key: | |
type: string | |
index: not_analyzed | |
text_value: | |
type: string | |
analyzer: simple | |
name_value: | |
type: string | |
# Should probably want a better analyzer. See https://www.found.no/play/gist/68ca8a4a50025560f822 | |
# for an example on metaphone-based fuzzy name searching | |
analyzer: simple |
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
#!/bin/bash | |
export ELASTICSEARCH_ENDPOINT="http://localhost:9200" | |
# Create indexes | |
curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{ | |
"settings": { | |
"analysis": {} | |
}, | |
"mappings": { | |
"type": { | |
"properties": { | |
"fields": { | |
"type": "nested", | |
"properties": { | |
"key": { | |
"type": "string", | |
"index": "not_analyzed" | |
}, | |
"text_value": { | |
"type": "string", | |
"analyzer": "simple" | |
}, | |
"name_value": { | |
"type": "string", | |
"analyzer": "simple" | |
} | |
} | |
} | |
} | |
} | |
} | |
}' | |
# Index documents | |
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d ' | |
{"index":{"_index":"play","_type":"type","_id":12345678}} | |
{"fields":[{"key":"book_title","text_value":"Harry Potter"},{"key":"price","float_value":34.5}]} | |
{"index":{"_index":"play","_type":"type","_id":123456790}} | |
{"fields":[{"key":"blog_title","text_value":"My awesome blog"},{"key":"author","name_value":"John Smith"},{"key":"not_book_title","text_value":"This blog is not about Harry Potter"}]} | |
' | |
# Do searches | |
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d ' | |
{ | |
"query": { | |
"nested": { | |
"path": "fields", | |
"query": { | |
"filtered": { | |
"filter": { | |
"term": { | |
"key": "book_title" | |
} | |
}, | |
"query": { | |
"match": { | |
"text_value": "potter" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
' | |
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d ' | |
{ | |
"query": { | |
"nested": { | |
"path": "fields", | |
"query": { | |
"filtered": { | |
"filter": { | |
"bool": { | |
"must": [ | |
{ | |
"term": { | |
"key": "price" | |
} | |
}, | |
{ | |
"range": { | |
"float_value": { | |
"gt": 34 | |
} | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
' | |
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
# Auto generated by Found's Play-tool at 2013-11-22T09:24:06+01:00 | |
version: 0 | |
title: "Nested key/value-mapping" | |
description: "Simple example of nested mappings, with queries that require a key to match as well." |
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
# In reply to http://stackoverflow.com/questions/20138641/dynamic-type-with-mappings | |
query: | |
nested: | |
path: fields | |
query: | |
filtered: | |
filter: | |
term: | |
key: book_title | |
query: | |
# This should not match the second document | |
match: | |
text_value: potter | |
--- | |
query: | |
nested: | |
path: fields | |
query: | |
filtered: | |
filter: | |
bool: | |
must: | |
- term: | |
key: price | |
- range: | |
float_value: | |
gt: 34 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment