Skip to content

Instantly share code, notes, and snippets.

@jasongilman
Created December 28, 2011 17:59
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 jasongilman/1528898 to your computer and use it in GitHub Desktop.
Save jasongilman/1528898 to your computer and use it in GitHub Desktop.
Elastic Search Nested Query Problem
#!/bin/sh
# Create main index
curl -XPUT "http://localhost:9200/main/" -d '
{ "settings" : { "index" : { "number_of_shards" : 3 } } }
'
# Set alpha mapping
curl -XPUT "http://localhost:9200/main/alpha/_mapping" -d '
{"alpha": {
"dynamic" : "strict",
"_all" : {"enabled" : false},
"_source" : {"compress" : true},
"properties":
{
"alpha_id":{"type":"string","index":"not_analyzed"},
"charlies": {
"type" : "nested",
"dynamic" : "strict",
"properties": {
"charlie_id":{"type":"string","index":"not_analyzed"}
}
}
}
}
}
'
# Set bravo mapping
curl -XPUT "http://localhost:9200/main/bravo/_mapping" -d '
{"bravo":
{
"dynamic" : "strict",
"_all" : {"enabled" : false},
"_source" : {"compress" : true},
"properties":
{
"bravo_id":{"type":"string","index":"not_analyzed"},
"charlies": {
"type" : "nested",
"dynamic" : "strict",
"properties": {
"charlie_id":{"type":"string","index":"not_analyzed"}
}
}
}
}
}
'
#Index two alphas and a bravo. The order is important.
curl -XPOST "http://localhost:9200/_bulk" -d '
{"index":{"_index":"main","_type":"alpha","_id":"A1"}}
{"alpha_id":"A1"}
{"index":{"_index":"main","_type":"bravo","_id":"B1"}}
{"bravo_id":"B1", "charlies":[{"charlie_id":"B1_C"}]}
{"index":{"_index":"main","_type":"alpha","_id":"A2"}}
{"alpha_id":"A2"}
'
# Query for alpha 1 using the charlie_id in bravo 1. This causes the exception.
curl -XPOST "http://localhost:9200/main/alpha/_search" -d '
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": [{
"and": [{
"nested": {
"path": "charlies",
"query": {
"constant_score": {
"filter": {"term": {"charlie_id": "B1_C"}}
}
}
}
}]
}, {
"term": {
"alpha_id": "A1"
}
}]
}
}
}
}
'
# Query for alpha 2 using the charlie_id in bravo 1. This does not cause the exception
curl -XPOST "http://localhost:9200/main/alpha/_search" -d '
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": [{
"and": [{
"nested": {
"path": "charlies",
"query": {
"constant_score": {
"filter": {"term": {"charlie_id": "B1_C"}}
}
}
}
}]
}, {
"term": {
"alpha_id": "A2"
}
}]
}
}
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment