Created
          September 17, 2012 20:07 
        
      - 
      
- 
        Save lukas-vlcek/3739469 to your computer and use it in GitHub Desktop. 
    Nested Query example for https://groups.google.com/d/topic/elasticsearch/PkZRPcq0YSk/discussion
  
        
  
    
      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/sh | |
| echo "\n --- delete index" | |
| curl -X DELETE 'http://localhost:9200/' | |
| echo "\n --- create index and put mapping into place" | |
| curl -X POST 'http://localhost:9200/myindex/' -d '{ | |
| "mappings" : { | |
| "person" : { | |
| "properties" : { | |
| "name" : {"type" : "string"}, | |
| "works" : { | |
| "type" : "nested", | |
| "include_in_parent" : false, | |
| "properties" : { | |
| "title" : {"type" : "string"}, | |
| "current" : {"type" : "boolean"}, | |
| "dummy" : {"type" : "string"} | |
| } | |
| } | |
| } | |
| } | |
| }, | |
| "settings" : { | |
| "number_of_shards" : 1, | |
| "number_of_replicas" : 0 | |
| } | |
| }' | |
| echo "\n --- index data" | |
| curl -X PUT 'http://localhost:9200/myindex/person/1' -d ' | |
| { | |
| "name" : "Lukas", | |
| "works" : [ | |
| { | |
| "title" : "developer", | |
| "current" : true, | |
| "dummy" : "match" | |
| }, | |
| { | |
| "title" : "dad", | |
| "current" : true | |
| }, | |
| { | |
| "title" : "husband", | |
| "current" : true, | |
| "dummy" : "foo" | |
| }, | |
| { | |
| "title" : "brother", | |
| "current" : true, | |
| "dummy" : "bar" | |
| } | |
| ] | |
| }' | |
| curl -X PUT 'http://localhost:9200/myindex/person/2' -d ' | |
| { | |
| "name" : "Karel", | |
| "works" : [ | |
| { | |
| "title" : "developer", | |
| "current" : true, | |
| "dummy" : "match" | |
| } | |
| ] | |
| }' | |
| curl -X PUT 'http://localhost:9200/myindex/person/3' -d ' | |
| { | |
| "name" : "Jan", | |
| "works" : [ | |
| { | |
| "title" : "developer", | |
| "current" : false, | |
| "dummy" : "match" | |
| } | |
| ] | |
| }' | |
| echo "\n --- optimize" | |
| curl -X POST 'http://localhost:9200/_optimize' | |
| #!/bin/sh | |
| echo "\n --- query" | |
| curl -X GET 'http://localhost:9200/_search?pretty=true' -d ' | |
| { | |
| "query" : { | |
| "nested" : { | |
| "path" : "works", | |
| "query" : { | |
| "bool" : { | |
| "must" : [ | |
| { "text" : { "works.dummy" : "match"} } | |
| ], | |
| "should" : [ | |
| { "text" : { "works.title" : "developer"} }, | |
| { "text" : { "works.current" : true } } | |
| ] | |
| } | |
| } | |
| } | |
| }, | |
| "fields" : [ | |
| "name" | |
| ] | |
| }' | |
| echo "\n --- done" | |
| # =========================================== | |
| # Output | |
| # =========================================== | |
| { | |
| "took" : 4, | |
| "timed_out" : false, | |
| "_shards" : { | |
| "total" : 1, | |
| "successful" : 1, | |
| "failed" : 0 | |
| }, | |
| "hits" : { | |
| "total" : 3, | |
| "max_score" : 2.9213471, | |
| "hits" : [ { | |
| "_index" : "myindex", | |
| "_type" : "person", | |
| "_id" : "1", | |
| "_score" : 2.9213471, | |
| "fields" : { | |
| "name" : "Lukas" | |
| } | |
| }, { | |
| "_index" : "myindex", | |
| "_type" : "person", | |
| "_id" : "2", | |
| "_score" : 2.9213471, | |
| "fields" : { | |
| "name" : "Karel" | |
| } | |
| }, { | |
| "_index" : "myindex", | |
| "_type" : "person", | |
| "_id" : "3", | |
| "_score" : 1.4967837, | |
| "fields" : { | |
| "name" : "Jan" | |
| } | |
| } ] | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
It is possible to get the the persons which are a "developer" and a "dad" by example?
Thank you