Files
OpenSearch-Docs-Cn/_field-types/join.md
T
Naarcha-AWS e119a93798 Index and OpenSearch TOC changes (#2946)
* Add new Query DSL section

Signed-off-by: Naarcha-AWS <[email protected]>

* Change config settings

Signed-off-by: Naarcha-AWS <[email protected]>

* Fix links

Signed-off-by: Naarcha-AWS <[email protected]>

* Add Query DSL back to Search

Signed-off-by: Naarcha-AWS <[email protected]>

* Fix remaining links

Signed-off-by: Naarcha-AWS <[email protected]>

* Last reorder

Signed-off-by: Naarcha-AWS <[email protected]>

* Add Heather's feedback

Signed-off-by: Naarcha-AWS <[email protected]>

* Add Query DSL section. Delete Index Data page

Signed-off-by: Naarcha-AWS <[email protected]>

* Fix index title

Signed-off-by: Naarcha-AWS <[email protected]>

* Add correct title to managing indexes

Signed-off-by: Naarcha-AWS <[email protected]>

* Change config yml

Signed-off-by: Naarcha-AWS <[email protected]>

* Add permalinks for each page in query DSL section

Signed-off-by: Naarcha-AWS <[email protected]>

* :wq

---------

Signed-off-by: Naarcha-AWS <[email protected]>
2023-03-08 09:53:21 -06:00

6.1 KiB

layout, title, nav_order, has_children, parent, grand_parent, redirect_from
layout title nav_order has_children parent grand_parent redirect_from
default Join 43 false Object field types Supported field types
/opensearch/supported-field-types/join/

Join field type

A join field type establishes a parent/child relationship between documents in the same index.

Example

Create a mapping to establish a parent-child relationship between products and their brands:

PUT testindex1
{
  "mappings": {
    "properties": {
      "product_to_brand": { 
        "type": "join",
        "relations": {
          "brand": "product" 
        }
      }
    }
  }
}

{% include copy-curl.html %}

Then, index a parent document with a join field type:

PUT testindex1/_doc/1
{
  "name": "Brand 1",
  "product_to_brand": {
    "name": "brand" 
  }
}

{% include copy-curl.html %}

You can also use a shortcut without object notation to index a parent document:

PUT testindex1/_doc/1
{
  "name": "Brand 1",
  "product_to_brand" : "brand" 
}

{% include copy-curl.html %}

When indexing child documents, you have to specify the routing query parameter because parent and child documents in the same relation have to be indexed on the same shard. Each child document refers to its parent's ID in the parent field.

Index two child documents, one for each parent:

PUT testindex1/_doc/3?routing=1
{
  "name": "Product 1",
  "product_to_brand": {
    "name": "product", 
    "parent": "1" 
  }
}

{% include copy-curl.html %}

PUT testindex1/_doc/4?routing=1
{
  "name": "Product 2",
  "product_to_brand": {
    "name": "product", 
    "parent": "1" 
  }
}

{% include copy-curl.html %}

Querying a join field

When you query a join field, the response contains subfields that specify whether the returned document is a parent or a child. For child objects, the parent ID is also returned.

Search for all documents

GET testindex1/_search
{
  "query": {
    "match_all": {}
  }
}

{% include copy-curl.html %}

The response indicates whether a document is a parent or a child:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "testindex1",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "Brand 1",
          "product_to_brand" : {
            "name" : "brand"
          }
        }
      },
      {
        "_index" : "testindex1",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_routing" : "1",
        "_source" : {
          "name" : "Product 1",
          "product_to_brand" : {
            "name" : "product",
            "parent" : "1"
          }
        }
      },
      {
        "_index" : "testindex1",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_routing" : "1",
        "_source" : {
          "name" : "Product 2",
          "product_to_brand" : {
            "name" : "product",
            "parent" : "1"
          }
        }
      }
    ]
  }
}

Search for all children of a parent

Find all products associated with Brand 1:

GET testindex1/_search
{
  "query" : {
    "has_parent": {
      "parent_type":"brand",
      "query": {
        "match" : {
          "name": "Brand 1"
        }
      }
    }
  }
}

{% include copy-curl.html %}

The response contains Product 1 and Product 2, which are associated with Brand 1:

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "testindex1",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_routing" : "1",
        "_source" : {
          "name" : "Product 1",
          "product_to_brand" : {
            "name" : "product",
            "parent" : "1"
          }
        }
      },
      {
        "_index" : "testindex1",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_routing" : "1",
        "_source" : {
          "name" : "Product 2",
          "product_to_brand" : {
            "name" : "product",
            "parent" : "1"
          }
        }
      }
    ]
  }
}

Search for the parent of a child

Find the parent of Product 1:

GET testindex1/_search
{
  "query" : {
    "has_child": {
      "type":"product",
      "query": {
        "match" : {
            "name": "Product 1"
        }
      }
    }
  }
}

{% include copy-curl.html %}

The response returns Brand 1 as Product 1's parent:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "testindex1",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "Brand 1",
          "product_to_brand" : {
            "name" : "brand"
          }
        }
      }
    ]
  }
}

Parent with many children

One parent can have many children. Create a mapping with multiple children:

PUT testindex1
{
  "mappings": {
    "properties": {
      "parent_to_child": {
        "type": "join",
        "relations": {
          "parent": ["child 1", "child 2"]  
        }
      }
    }
  }
}

{% include copy-curl.html %}

Join field type notes

  • There can only be one join field mapping in an index.
  • You need to provide the routing parameter when retrieving, updating, or deleting a child document. This is because parent and child documents in the same relation have to be indexed on the same shard.
  • Multiple parents are not supported.
  • You can add a child document to an existing document only if the existing document is already marked as a parent.
  • You can add a new relation to an existing join field.