es - elasticsearch mapping - field data type - 9

139 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :join field type 有什么作用?
答 :join 用来定义同一索引同一分片内文档的父子关系

问 :join 如何使用?
答 :

# join
PUT /join_test
{
  "mappings" : {
    "properties" : {
      "my_join" : {
        "type"      : "join",
        "relations" : {
          "question" : "answer"
        }
      }
    }
  }
}

# 索引父文档,并指定分片
POST /join_test/_doc/1?routing=1
{
  "text"    : "question1",
  "my_join" : "question"
}

# 索引子文档,并指定分片
POST /join_test/_doc/2?routing=1
{
  "text"    : "answer1",
  "my_join" : {
    "name"   : "answer",
    "parent" : 1
  }
}

# 根据父文档id查找子文档
GET /join_test/_search
{
  "query" : {
    "parent_id" : {
      "type" : "answer",
      "id"   : "1"
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.13353139,
    "hits" : [
      {
        "_index" : "join_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.13353139,
        "_routing" : "1",
        "_source" : {
          "text" : "answer1",
          "my_join" : {
            "name" : "answer",
            "parent" : 1
          }
        }
      }
    ]
  }
}