Elasticsearch 6.3.1 Create a parent-child document (join datatype)

Why use join datatype

Creation process

1. Index Description

shop index corresponds to a cloth type, the document in the cloth type (clothing brand, color, size)

2. Create Index
curl -X PUT  -H "Content-Type:application/json" 'http://127.0.0.1:9200/shop/' -d  '{
  "mappings": {
    "cloth": {
      "properties": {
        "name": {
          "type": "text"
        },
        "size":{"type" : "text"},
        "color":{"type" : "text"},
        "info": {
          "type": "join",
          "relations": {
            "base": "next"
          }
        }
      }
    }
  }
}'

Return {"acknowledged":true,"shards_acknowledged":true,"index":"shop"} to indicate success

3. Index data
  1. Index parent document (nike Address)
curl -X PUT  -H "Content-Type:application/json" 'http://127.0.0.1:9200/shop/cloth/1' -d  '{
  "brand": "nike",
  "info": {
    "name": "base"
  }
}' 

I'll just write one, Address does it by itself, note _id

  1. Index Subdocument
curl -X PUT  -H "Content-Type:application/json" 'http://127.0.0.1:9200/shop/cloth/3?routing=1' -d  '{
  "color":"red",
  "size":"XXL",
  "info": {
    "name": "next", 
    "parent": "1" 
  }
}'

Note that _id and routing, parent,routing are required

Solve the rest of the documents yourself

  1. has_child queries for brands containing XL size clothes
curl -X GET  -H "Content-Type:application/json" 'http://127.0.0.1:9200/shop/_search?pretty' -d  '{
  "query": {
    "has_child": {
      "type": "next",
      "query": {
        "common": {
          "size": {
            "query": "xl"
          }
        }
      }
    }
  }
}'


4. has_parents to see what clothes the nike brand has

curl -X GET  -H "Content-Type:application/json" 'http://127.0.0.1:9200/shop/_search?pretty' -d  '{
  "query": {
    "has_parent": {
      "parent_type": "base",
      "query": {
        "term": {
          "brand": "nike"
        }
      }
    }
  }
}'

Write here first, there are problems entering QQ group 630300475

Tags: curl JSON REST

Posted on Wed, 05 Feb 2020 11:13:01 -0500 by philooo