ElatsicSearch學習之對象關係

嵌套對象

nested data type
nested數據類型:允許對象數組中的對象被獨立索引。
使用nested和properties關鍵字
在內部,nested文檔會被保存在兩個lucene文檔中,在查詢時做join處理。

# 創建 Nested 對象 Mapping
PUT my_movies
{
      "mappings" : {
      "properties" : {
        "actors" : {
          "type": "nested",	//指定type爲nested嵌套對象
          "properties" : {
            "first_name" : {"type" : "keyword"},
            "last_name" : {"type" : "keyword"}
          }},
        "title" : {
          "type" : "text",
          "fields" : {"keyword":{"type":"keyword","ignore_above":256}}
        }
      }
    }
}

#查詢插入數據
POST my_movies/_doc/1
{
  "title":"Speed",
  "actors":[
    {
      "first_name":"Keanu",
      "last_name":"Reeves"
    },

    {
      "first_name":"Dennis",
      "last_name":"Hopper"
    }

  ]
}

# Nested 查詢
POST my_movies/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {"title": "Speed"}},
        {
          "nested": {
            "path": "actors",
            "query": {
              "bool": {
                "must": [
                  {"match": {
                    "actors.first_name": "Keanu"
                  }},

                  {"match": {
                    "actors.last_name": "Hopper"
                  }}
                ]
              }
            }
          }
        }
      ]
    }
  }
}

文檔父子關係

# 設定 Parent/Child Mapping
PUT my_blogs
{
  "settings": {
    "number_of_shards": 2
  },
  "mappings": {
    "properties": {
      "blog_comments_relation": {  
        "type": "join",	//聲明它是一個父子文檔
        "relations": {
          "blog": "comment"  //blog表示父文檔,comment表示子文檔
        }
      },
      "content": {
        "type": "text"
      },
      "title": {
        "type": "keyword"
      }
    }
  }
}


#索引父文檔
PUT my_blogs/_doc/blog1   //blog1是父文檔ID
{
  "title":"Learning Elasticsearch",
  "content":"learning ELK @ geektime",
  "blog_comments_relation":{  //聲明文檔類型,父文檔
    "name":"blog"
  }
}

#索引父文檔
PUT my_blogs/_doc/blog2
{
  "title":"Learning Hadoop",
  "content":"learning Hadoop",
    "blog_comments_relation":{  
    "name":"blog"
  }
}


#索引子文檔
# comment1是子文檔ID  routing確保父子文檔路由在同一個分片上
PUT my_blogs/_doc/comment1?routing=blog1  
{
  "comment":"I am learning ELK",
  "username":"Jack",
  "blog_comments_relation":{
    "name":"comment",
    "parent":"blog1"
  }
}

#想查詢父文檔,同時把對應的子文檔也查詢出來
PSOT /my_blogs/_search
{
	"query":{
		"parent_id":{
			"type":"comment",
			"id":"blog2"
		}
	}
}

# Has Child 查詢,返回父文檔
POST my_blogs/_search
{
  "query": {
    "has_child": {
      "type": "comment",
      "query" : {
                "match": {
                    "username" : "Jack"
                }
            }
    }
  }
}


# Has Parent 查詢,返回相關的子文檔
POST my_blogs/_search
{
  "query": {
    "has_parent": {
      "parent_type": "blog",
      "query" : {
                "match": {
                    "title" : "Learning Hadoop"
                }
            }
    }
  }
}
Nested Object Parent/Child
優點 文檔存儲子啊一起,讀取性能高 父子文檔可以獨立更新
缺點 更新嵌套的子文檔時,需要更新整個文檔 需要額外的內存維護關係,讀取性能相對差
使用場景 子文檔偶爾更新,以查詢爲主 子文檔頻繁更新
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章