日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Elasticsearch nested嵌套类型

發布時間:2023/12/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Elasticsearch nested嵌套类型 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一次接觸es,感覺啥啥都是懵逼狀態,不得不吐槽一下,出了問題網上寫的那些文章真的是看不懂,要么代碼不全,要么就是各種抄,根本結局不了問題,還是決定自己寫一個吧,沒準哪天就幫別人節省了時間.
話不多說,這里使用的版本是7.12.1,查看es版本方法:
直接在瀏覽器訪問es端口號就行 ps:搞了一天才發現看的是2.0的api 我吐了

博主現在查看的api是7.14版本的(直接看最新的了)
為什么要用nested嵌套結構呢?具體可以看官網的解釋,這里就不在多做贅述了:
https://www.elastic.co/guide/en/elasticsearch/reference/7.14/nested.html

簡單來說就是需要一個多層嵌套的對象結構,例如:
公司下面有部門員工,員工下面有所屬項目,都是一對多的關系,后面的代碼也是使用的這個邏輯(可能有點不太合理,但是大致是這么個意思)

公司:
____部門名稱
____部門員工:
________id
________姓名
________年齡
________所屬項目:
____________項目名稱

直接上代碼

1.聲明結構

PUT localhost:9200/test_mapping7{"mappings": {"properties":{"group": {"type": "text"},"user":{"type": "nested", //嵌套類型type應為nested"properties":{"id": {"type": "keyword"},"name":{"type": "text"},"age":{ "type": "short"},"project":{"type": "nested","properties":{"name": {"type": "text"}}}}}} } }

nested類型結構的屬性應用properties表示,以下是官網api

2.聲明成功后我們可以查詢一下聲明的結構

GET localhost:9200/test_mapping7?pretty //pretty是輸出json格式化后的數據{ "test_mapping7" : { "aliases" : { }, "mappings" : { "properties" : { "group" : { "type" : "text" }, "user" : { "type" : "nested", "properties" : { "age" : { "type" : "short" }, "id" : { "type" : "keyword" }, "name" : { "type" : "text" }, "project" : { "type" : "nested", "properties" : { "name" : { "type" : "text" } } } } } } }, "settings" : { "index" : { "routing" : { "allocation" : { "include" : { "_tier_preference" : "data_content"} } }, "number_of_shards" : "1", "provided_name" : "test_mapping7", "creation_date" : "1630464120543", "number_of_replicas" : "1", "uuid" : "stKB9OdESUmnqJwBKOK-pQ", "version" : { "created" : "7120199" } } } } }

3.插入數據
重點: 插入數據這塊必須在index名后面加上/_doc/ , 本身這里應該是設置type的字段,但是在實際插入數據時,用自定義的stu,class等都會報錯,難道只能插入doc類型? ps:正常猜想是在mapping中沒有設置最外層的type,但是實際操作時聲明type會報錯

POST localhost:9200/test_mapping7/_doc/1 {"group": "研發","user":[{"id": 1,"name": "zhangsan","age": 12,"project":[{"name": "pro1"}]},{"id": 2,"name": "lisi","age": 15,"project":[{"name": "pro12"}]}] }result:{"_index": "test_mapping7","_type": "_doc","_id": "2","_version": 1,"result": "created","_shards": {"total": 1,"successful": 1,"failed": 0},"_seq_no": 1,"_primary_term": 1 }

再插入一條

POST localhost:9200/test_mapping7/_doc/2{"group": "市場","user":[{"id": 3,"name": "xiaomei","age": 21,"project":[{"name": "pro3"}]},{"id": 4,"name": "xiaoxue","age": 25,"project":[{"name": "pro4"}]}] }result:{"_index": "test_mapping7","_type": "_doc","_id": "2","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 1,"_primary_term": 1 }

查詢一下插入的數據

GET localhost:9200/test_mapping7/_search?pretty{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "test_mapping7", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "group" : "研發", "user" : [ { "id" : 1, "name" : "zhangsan", "age" : 12, "project" : [ { "name" : "pro1" } ] }, { "id" : 2, "name" : "lisi", "age" : 14, "project" : [ { "name" : "pro12" } ] } ] } }, { "_index" : "test_mapping7", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : { "group" : "市場", "user" : [ { "id" : 3, "name" : "xiaomei", "age" : 21, "project" : [ { "name" : "pro3" } ] }, { "id" : 4, "name" : "xiaoxue", "age" : 25, "project" : [ { "name" : "pro4" } ] } ] } } ] } }

4.查詢

首先查詢一下name=zhangsan的index

GET localhost:9200/test_mapping7/_search{"query": {"nested":{"path": "user","query": {"bool":{"must": [{"match": {"user.name":"zhangsan"}}]}}}} }result:{"took": 2,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 1.2039728,"hits": [{"_index": "test_mapping7","_type": "_doc","_id": "1","_score": 1.2039728,"_source": {"group": "研發","user": [{"id": 1,"name": "zhangsan","age": 12,"project": [{"name": "pro1"}]},{"id": 2,"name": "lisi","age": 14,"project": [{"name": "pro12"}]}]}}]} }

可以看到直接返回了整個doc1對象,符合預期結果

那么我們如果查詢條件是project.name=pro4呢?劃重點
nested.path必須是從外層開始寫: user.project,不可以直接寫project
match條件也要從最外層開始寫 user.project.name=pro4,不可以直接寫project.name=pro4

POST localhost:9200/test_mapping7/_search{"query": {"nested":{"path": "user.project","query": {"bool":{"must": [{"match": {"user.project.name":"pro4"}}]}}}} }result:{"took": 2,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 1.2039728,"hits": [{"_index": "test_mapping7","_type": "_doc","_id": "2","_score": 1.2039728,"_source": {"group": "市場","user": [{"id": 3,"name": "xiaomei","age": 21,"project": [{"name": "pro3"}]},{"id": 4,"name": "xiaoxue","age": 25,"project": [{"name": "pro4"}]}]}}]} }

成功命中
未完待續,歡迎補充指正

總結

以上是生活随笔為你收集整理的Elasticsearch nested嵌套类型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。