Skip to content

Instantly share code, notes, and snippets.

@clintongormley
Created November 17, 2012 15:10
Show Gist options
  • Save clintongormley/4096675 to your computer and use it in GitHub Desktop.
Save clintongormley/4096675 to your computer and use it in GitHub Desktop.
Nested documents

Create an index with a nested mapping:

curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1'  -d '
{
   "mappings" : {
      "test" : {
         "properties" : {
            "title" : {
               "type" : "string"
            },
            "rows" : {
               "include_in_root" : 1,
               "type" : "nested",
               "properties" : {
                  "row" : {
                     "position_offset_gap" : 5,
                     "type" : "string"
                  }
               }
            },
            "col_names" : {
               "type" : "string"
            }
         }
      }
   },
   "settings" : {
      "number_of_shards" : 1
   }
}
'

Index some documents:

curl -XPUT 'http://127.0.0.1:9200/test/test/1?pretty=1'  -d '
{
   "title" : "dogs species",
   "rows" : [
      {
         "row" : [
            "Boxer",
            "good dog",
            "Germany"
         ]
      },
      {
         "row" : [
            "Irish Setter",
            "great dog",
            "Irland"
         ]
      }
   ],
   "col_names" : [
      "name",
      "description",
      "country_of_origin"
   ]
}
'

curl -XPUT 'http://127.0.0.1:9200/test/test/2?pretty=1'  -d '
{
   "title" : "Misc stuff",
   "rows" : [
      {
         "row" : [
            "Setter is important"
         ]
      },
      {
         "row" : [
            "Irland is green"
         ]
      }
   ],
   "col_names" : [
      "foo"
   ]
}
'

Query the nested documents AND the root object - the scores get combined:

curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '
{
   "query" : {
      "bool" : {
         "should" : [
            {
               "nested" : {
                  "query" : {
                     "match_phrase" : {
                        "rows.row" : {
                           "query" : "irland setter",
                           "slop" : "100"
                        }
                     }
                  },
                  "path" : "rows"
               }
            },
            {
               "match_phrase" : {
                  "row" : {
                     "query" : "irland setter",
                     "slop" : "100"
                  }
               }
            }
         ]
      }
   }
}
'

# {
#    "hits" : {
#       "hits" : [
#          {
#             "_source" : {
#                "title" : "dogs species",
#                "col_names" : [
#                   "name",
#                   "description",
#                   "country_of_origin"
#                ],
#                "rows" : [
#                   {
#                      "row" : [
#                         "Boxer",
#                         "good dog",
#                         "Germany"
#                      ]
#                   },
#                   {
#                      "row" : [
#                         "Irish Setter",
#                         "great dog",
#                         "Irland"
#                      ]
#                   }
#                ]
#             },
#             "_score" : 0.32379207,
#             "_index" : "test",
#             "_id" : "1",
#             "_type" : "test"
#          },
#          {
#             "_source" : {
#                "title" : "Misc stuff",
#                "col_names" : [
#                   "foo"
#                ],
#                "rows" : [
#                   {
#                      "row" : [
#                         "Setter is important"
#                      ]
#                   },
#                   {
#                      "row" : [
#                         "Irland is green"
#                      ]
#                   }
#                ]
#             },
#             "_score" : 0.13218756,
#             "_index" : "test",
#             "_id" : "2",
#             "_type" : "test"
#          }
#       ],
#       "max_score" : 0.32379207,
#       "total" : 2
#    },
#    "timed_out" : false,
#    "_shards" : {
#       "failed" : 0,
#       "successful" : 1,
#       "total" : 1
#    },
#    "took" : 3
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment