Skip to content

Instantly share code, notes, and snippets.

@babadofar
Created June 25, 2014 21:48
Show Gist options
  • Save babadofar/264cf1340044498d270b to your computer and use it in GitHub Desktop.
Save babadofar/264cf1340044498d270b to your computer and use it in GitHub Desktop.
Testing search in nested documents
text: This text will be used if nothing else is specified.
analyzer:
myAnalyzer:
type: custom
tokenizer: whitespace
filter:
- lowercase
- reverse
metaphoneAnalyzer:
# Custom text here, since this makes more sense for this analyzer.
text:
- John Smith
- Jon Schmidth
type: custom
tokenizer: standard
filter:
- double_metaphone # Defined below
filter:
double_metaphone:
type: phonetic
encoder: doublemetaphone
# These are sample documents.
_index: play
_type: type
# If you don't specify an _index or _type, they default to "play" and "type" respectively.
foo: bar
---
# Specify multiple documents with the document separator, i.e. "---"
_index: other-index
_type: person
_id: 1
name:
first: John
last: Smith
nickname: Smithy
---
_index: other-index
_type: message
_parent: 1
message: This is a child of the previous document
# Customize mappings. Note: All indexes get the same types.
message:
_parent:
type: person
# ^ Put your cursor in this column to get auto-completion on things for the "message"-type.
person:
properties:
nickname:
type: string
# ^ Place your cursor within the nickname object in the analysis view to see how the
# sample values of that field get tokenized.
# Dynamic templates and multi fields are supported.
dynamic_templates:
- name:
match: "name.*"
mapping:
type: multi_field
fields:
double_metaphone:
type: string
analyzer: double_metaphone
exact:
type: string
index: not_analyzed
#!/bin/bash
export ELASTICSEARCH_ENDPOINT="http://localhost:9200"
# Create indexes
curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{
"settings": {
"analysis": {
"text": "This text will be used if nothing else is specified.",
"analyzer": {
"myAnalyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"lowercase",
"reverse"
]
},
"metaphoneAnalyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"double_metaphone"
]
}
},
"filter": {
"double_metaphone": {
"type": "phonetic",
"encoder": "doublemetaphone"
}
}
}
},
"mappings": {
"message": {
"_parent": {
"type": "person"
}
},
"person": {
"properties": {
"nickname": {
"type": "string"
}
},
"dynamic_templates": [
{
"name": {
"match": "name.*",
"mapping": {
"type": "multi_field",
"fields": {
"double_metaphone": {
"type": "string",
"analyzer": "double_metaphone"
},
"exact": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
]
}
}
}'
curl -XPUT "$ELASTICSEARCH_ENDPOINT/other-index" -d '{
"settings": {
"analysis": {
"text": "This text will be used if nothing else is specified.",
"analyzer": {
"myAnalyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"lowercase",
"reverse"
]
},
"metaphoneAnalyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"double_metaphone"
]
}
},
"filter": {
"double_metaphone": {
"type": "phonetic",
"encoder": "doublemetaphone"
}
}
}
},
"mappings": {
"message": {
"_parent": {
"type": "person"
}
},
"person": {
"properties": {
"nickname": {
"type": "string"
}
},
"dynamic_templates": [
{
"name": {
"match": "name.*",
"mapping": {
"type": "multi_field",
"fields": {
"double_metaphone": {
"type": "string",
"analyzer": "double_metaphone"
},
"exact": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
]
}
}
}'
# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"type"}}
{"foo":"bar"}
{"index":{"_index":"other-index","_type":"person","_id":1}}
{"name":{"first":"John","last":"Smith"},"nickname":"Smithy"}
{"index":{"_index":"other-index","_type":"message","_parent":1}}
{"message":"This is a child of the previous document"}
'
# Do searches
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"query": {
"match": {
"foo": "bar"
}
},
"size": 2,
"explain": true
}
'
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"filter": {
"term": {
"foo": "bar"
}
}
}
'
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"query": {
"match_all": {}
},
"facets": {
"foo": {
"terms": {
"field": "foo"
}
}
}
}
'
# Auto generated by Found's Play-tool at 2014-06-25T23:48:30+02:00
version: 0
title: NestedDocumentsTest
description: Testing search in nested documents
# Sample searches. Press Ctrl-Enter to run.
# Press Shift-Enter to go to focused mode for the selected editor.
# Click "Help" for more shortcuts.
query:
match:
foo: bar
# You can specify other search options as well.
size: 2
explain: true
---
# ^-- The YAML-document separator separates searches.
# This is search #2.
filter: # <- There should be a warning for filter, since this should go in a filtered-query.
term:
foo: bar
---
# This is search #3.
query:
match_all: {}
facets:
foo:
terms:
field: foo
# ^ Placing your cursor within the facet definition will scroll to and highlight the
# facet in the results pane.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment