jillesvangurp / kt-search

Multi platform kotlin client for Elasticsearch & Opensearch with easily extendable Kotlin DSLs for queries, mappings, bulk, and more.
MIT License
95 stars 23 forks source link

Add support for has_parent, has_child and parent_id queries #115

Closed csh97 closed 4 months ago

csh97 commented 4 months ago

Implements https://github.com/jillesvangurp/kt-search/issues/116

Adds support for remaining join queries: has_parent, has_child and parent_id (https://www.elastic.co/guide/en/elasticsearch/reference/current/joining-queries.html)

Changes to note:

has_parent

SearchDSL().apply {
        query = hasParent("parent") {
            query = matchAll()
            minChildren = 1
            maxChildren = 2
            scoreMode = ScoreMode.max
            ignoreUnmapped = true
            innerHits {
                filterSource("name")
                innerHitSize = 1
                name = "adfa"
                sort { add("a") }
            }
        }
    }.json(pretty = true).let { println(it) }

Produces:

{
  "query": {
    "has_parent": {
      "parent_type": "parent",
      "query": {
        "match_all": {
        }
      },
      "min_children": 1,
      "max_children": 2,
      "score_mode": "max",
      "ignore_unmapped": true,
      "inner_hits": {
        "_source": [
          "name"
        ],
        "size": 1,
        "name": "adfa",
        "sort": [
          {
            "a": {
              "order": "DESC"
            }
          }
        ]
      }
    }
  }
}

has_child

SearchDSL().apply {
        query = hasChild("child") {
            query = matchAll()
            minChildren = 1
            maxChildren = 2
            scoreMode = ScoreMode.max
            ignoreUnmapped = true
            innerHits {
                filterSource("name")
                innerHitSize = 1
                name = "adfa"
                sort { add("a") }
            }
        }
    }.json(pretty = true).let { println(it) }

Produces:

{
  "query": {
    "has_child": {
      "type": "child",
      "query": {
        "match_all": {
        }
      },
      "min_children": 1,
      "max_children": 2,
      "score_mode": "max",
      "ignore_unmapped": true,
      "inner_hits": {
        "_source": [
          "name"
        ],
        "size": 1,
        "name": "adfa",
        "sort": [
          {
            "a": {
              "order": "DESC"
            }
          }
        ]
      }
    }
  }
}

parent_id

SearchDSL().apply {
        query = parentId(type = "child", id = "123") {
            ignoreUnmapped = true
        }
    }.json(pretty = true).let { println(it) }

Produces:

{
  "query": {
    "parent_id": {
      "type": "child",
      "id": "123",
      "ignore_unmapped": true
    }
  }
}
jillesvangurp commented 4 months ago

Nice. Also thanks for documenting this.