Last active
August 29, 2015 14:13
-
-
Save haydenmarchant/213e03924e79b4412200 to your computer and use it in GitHub Desktop.
This file contains 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
#delete index | |
curl -XDELETE 'http://localhost:9200/blog' | |
#Create index with mapping | |
curl -XPUT 'http://localhost:9200/blog' -d ' | |
{ | |
"mappings": { | |
"post": { | |
"properties": { | |
"title": { | |
"type": "string" | |
}, | |
"body": { | |
"type": "string" | |
}, | |
"nested_comments": { | |
"type": "nested", | |
"properties": { | |
"comment": { | |
"type": "string" | |
}, | |
"date": { | |
"type": "date" | |
}, | |
"name": { | |
"type": "string" | |
}, | |
"stars": { | |
"type": "long" | |
} | |
} | |
}, | |
"unnested_comments": { | |
"type": "object", | |
"properties": { | |
"comment": { | |
"type": "string" | |
}, | |
"date": { | |
"type": "date" | |
}, | |
"name": { | |
"type": "string" | |
}, | |
"stars": { | |
"type": "long" | |
} | |
} | |
}, | |
"tags": { | |
"type": "string" | |
}, | |
"title": { | |
"type": "string" | |
} | |
} | |
} | |
} | |
}' | |
#index a few posts | |
curl -XPUT 'http://localhost:9200/blog/post/1' -d '{ | |
"title": "Nest eggs", | |
"body": "Making your money work...", | |
"nested_comments": [ | |
{ | |
"name": "John Smith", | |
"comment": "Great article", | |
"age": 28, | |
"date": "2014-09-01" | |
}, | |
{ | |
"name": "Alice White", | |
"comment": "More like this please", | |
"age": 31, | |
"date": "2014-10-22" | |
}], | |
"unnested_comments": [ | |
{ | |
"name": "John Smith", | |
"comment": "Great article", | |
"age": 28, | |
"date": "2014-09-01" | |
}, | |
{ | |
"name": "Alice White", | |
"comment": "More like this please", | |
"age": 31, | |
"date": "2014-10-22" | |
} | |
] | |
}' | |
curl -XPUT 'http://localhost:9200/blog/post/2' -d '{ | |
"title": "Bad news", | |
"body": "Markets have been really bad - stay away from the stock market...", | |
"nested_comments": [ | |
{ | |
"name": "Michael Smith", | |
"comment": "Silly article", | |
"age": 32, | |
"date": "2015-01-01" | |
}, | |
{ | |
"name": "Jack Reany", | |
"comment": "Should we sell then?", | |
"age": 14, | |
"date": "2015-01-02" | |
}, | |
{ | |
"name": "Mark Hamilton", | |
"comment": "Do you have proof for this?", | |
"age": 76, | |
"date": "2015-01-02" | |
} | |
], | |
"unnested_comments": [ | |
{ | |
"name": "Michael Smith", | |
"comment": "Silly article", | |
"age": 32, | |
"date": "2015-01-01" | |
}, | |
{ | |
"name": "Jack Reany", | |
"comment": "Should we sell then?", | |
"age": 14, | |
"date": "2015-01-02" | |
}, | |
{ | |
"name": "Mark Hamilton", | |
"comment": "Do you have proof for this?", | |
"age": 76, | |
"date": "2015-01-02" | |
} | |
] | |
}' | |
#trying to automatically get count for fields in unnested object | |
curl -XGET 'http://localhost:9200/blog/post/_search' -d ' | |
{ | |
"filter": { | |
"script": { | |
"script": "doc['unnested_comments.id'].values.size() == 2" | |
} | |
} | |
} | |
' | |
#trying to automatically get count for fields in nested object | |
curl -XGET 'http://localhost:9200/blog/post/_search' -d ' | |
{ | |
"filter": { | |
"script": { | |
"script": "doc['nested_comments.name'].values.size() == 2" | |
} | |
} | |
} | |
' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment