Skip to content

Instantly share code, notes, and snippets.

@alexbrasetvik
Created November 22, 2013 08:24
Show Gist options
  • Save alexbrasetvik/7596633 to your computer and use it in GitHub Desktop.
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.
# Here you could have various analyzers for different string-types.
# "text_value" and "name_value" should probably have different ones.
_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"
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
#!/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
}
}
}
]
}
}
}
}
}
}
}
'
# 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."
# 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