2
votes

I want to query against nested data already loaded into Elasticsearch 5 but every query returns nothing. The data is of object datatype and nested array of json.

This the nested datatype ie team_members array of json:

[{
"id": 6,
"name": "mike",
"priority": 1
}, {
"id": 7,
"name": "james",
"priority": 2
}]

This object datatype ie the availability_slot json:

{
"monday": {
    "on": true,
    "end_time": "15",
    "start_time": "9",
    "end_time_unit": "pm",
    "start_time_unit": "am",
    "events_starts_every": 10
}
}

This is my elasticsearch mapping:

{
"meetings_development_20170716013030509": {
    "mappings": {
        "meeting": {
            "properties": {
                "account": {"type": "integer"},
                "availability_slot": {
                    "properties": {
                        "monday": {
                            "properties": {
                                "end_time": {"type": "text"},
                                "end_time_unit": {"type": "text"},
                                "events_starts_every": {
                                      "type":"integer"
                                },
                                "on": {"type": "boolean"},
                                "start_time": {"type": "text"},
                                "start_time_unit": {
                                   "type": "text"
                                }
                            }
                        }
                    }
                },
                "team_members": {
                    "type": "nested",
                    "properties": {
                        "id": {"type": "integer"},
                        "name": {"type": "text"},
                        "priority": {"type": "integer"}
                    }
                }
            }
        }
    }
 }
}

I have two queries which are failing for different reasons:

query 1 This query returns a count of zero despite the records existing in elasticsearch, I discovered the queries are failing because of the filter:

curl -u elastic:changeme http://172.19.0.4:9200/meetings_development/_search?pretty -d '{"query":{"nested":{"path":"team_members","score_mode":"avg","query":{"bool":{"must":[{"match":{"team_members.name":"mike"}},{"match":{"team_members.priority":1}}],"filter":[{"match":{"account":1}}]}}}}}'

This returns zero result:

{
"took" : 8,
"timed_out" : false,
"_shards" : {
  "total" : 5,
  "successful" : 5,
  "failed" : 0
},
"hits" : {
  "total" : 0,
  "max_score" : null,
  "hits" : [ ]
 }
}

query 1 without filter Thesame query from above without the filter works:

curl -u elastic:changeme http://172.19.0.4:9200/meetings_development/_search?pretty -d '{"query":{"nested":{"path":"team_members","score_mode":"avg","query":{"bool":{"must":[{"match":{"team_members.name":"mike"}},{"match":{"team_members.priority":1}}]}}}}}'

The query above returns 3 hits:

{
"took" : 312,
"timed_out" : false,
"_shards" : {
  "total" : 5,
  "successful" : 5,
  "failed" : 0
},
"hits" : {
  "total" : 3,
  "max_score" : 2.1451323,
  "hits" : [{**results available here**} ]
 }
}

query 2 for the object datatype

curl -u elastic:changeme http://172.19.0.4:9200/meetings_development/_search?pretty -d '{"query":{"bool":{"must":{"match":{"availability_slot.start_time":1}},"filter":[{"match":{"account":1}}]}}}'

The query returns a hit of zero but the data is in elasticsearch:

{
"took" : 172,
"timed_out" : false,
"_shards" : {
  "total" : 5,
  "successful" : 5,
  "failed" : 0
},
"hits" : {
  "total" : 0,
  "max_score" : null,
  "hits" : [ ]
 }
}

How do I get both queries to work filtering by account. Thanks

1

1 Answers

4
votes

This elasticsearch guide link was very helpful in coming up with the correct elasticsearch queries shown below:

query 1 for the nested array of json

 {
  "query" => {

 "bool": {
  "must": [
   {
    "match": {
      "name": "sales call"
    }
   },
   {"nested" => {
  "path" => "team_members",
  "score_mode" => "avg",
  "query" => {
    "bool" => {
      "must" => {
        "match" => {"team_members.name" => "mike"} 
      }
    }
   }
  }
 }
 ],
  "filter": {
    "term": {
      "account": 1
    }
   }
  },


 }
}

Just pass the query to elastic search like this:

curl http://172.19.0.4:9200/meetings_development/_search?pretty -d '{"query":{"bool":{"must":[{"match":{"name":"sales call"}},{"nested":{"path":"team_members","score_mode":"avg","query":{"bool":{"must":{"match":{"team_members.name":"mike"}}}}}}],"filter":{"term":{"account":1}}}}}'

correct syntax for query 2 for the object datatype ie json

 {
   "query": {
     "bool": {
       "must": { 
          "match": {'availability_slot.monday.start_time' => '9'} 
       },
     "filter": [{
          "match":  {'account': 1}
     }]
   }
 }
}

You the pass this to elasticsearch like this:

curl http://172.19.0.4:9200/meetings_development/_search?pretty -d '{"query":{"bool":{"must":{"match":{"availability_slot.monday.start_time":"9"}},"filter":[{"match":{"account":1}}]}}}'