Komang
Komang

Reputation: 5064

Elasticsearch parent-child conditional fetching children

I have 2 type documents mapped using parent-child relations, mappings as follow:

{
  "venue": {
    "properties": {
      "id": {
        "type": "long",
        "index": "not_analyzed"
      },
      "address": {
        "type": "string",
        "index": "analyzed"
      },
      "capacity": {
        "type": "integer",
        "index": "not_analyzed"
      },
      "rate_standard": {
        "type": "nested",
        "properties": {
          "rate": {
            "type": "double",
            "precision_step": 1,
            "index": "not_analyzed"
          },
          "minimum": {
            "type": "integer",
            "index": "not_analyzed"
          }
        }
      }
    }
  },
  "calendar": {
    "_parent": {
      "type": "venue"
    },
    "properties": {
      "day": {
        "type": "date",
        "format": "yyyy-MM-dd",
        "precision_step": 1,
        "index": "not_analyzed"
      },
      "is_available": {
        "type": "boolean",
        "index": "not_analyzed"
      },
      "rate": {
        "type": "double",
        "precision_step": 1,
        "index": "not_analyzed"
      },
      "minimum": {
        "type": "integer",
        "index": "not_analyzed"
      }
    }
  }
}

I would like to run queries against them as follows:

Had tried to build the query but its seems messed up at nesting bool query

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "capacity": {
              "gte": 2
            }
          }
        },
        {
          "match": {
            "address": {
              "query": "mampang jakarta indonesia",
              "operator": "and"
            }
          }
        }
      ],
      "must_not": {
        "has_child": {
          "type": "calendar",
          "query": {
            "bool": {
              "must": [
                {
                  "term": {
                    "available": false
                  }
                },
                {
                  "range": {
                    "day": {
                      "gte": "2014-01-01",
                      "lte": "2014-12-01"
                    }
                  }
                }
              ],
              "must_not": {},
              "should": {}
            }
          }
        }
      },
      "should": {}
    }
  }
}

Upvotes: 0

Views: 97

Answers (1)

Komang
Komang

Reputation: 5064

This one seems to works

{
  "query": {
    "bool": {
      "must": [
        { "range" : {"capacity": { "gte": 2 }} },
        { "match": {"address": { "query" : "mampang jakarta indonesia", "operator" : "and" }} }
      ],
      "must_not": [
        {
            "has_child": {       
                "type": "calendar",
                "query": {
                  "bool": {
                    "must":[
                        {"term": {"available" : false }},
                        {"range": {"day": {"gte": "2014-01-01", "lte": "2014-12-01" }}}
                      ]
                  }
                }
            }
        }
      ]
    }
  }
}

Tho still unsure if this is the appropriate query to achive that

Upvotes: 0

Related Questions