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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Elasticsearch nested嵌套类型

發(fā)布時(shí)間:2023/12/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Elasticsearch nested嵌套类型 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

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

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

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

直接上代碼

1.聲明結(jié)構(gòu)

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

nested類型結(jié)構(gòu)的屬性應(yīng)用properties表示,以下是官網(wǎng)api

2.聲明成功后我們可以查詢一下聲明的結(jié)構(gòu)

GET localhost:9200/test_mapping7?pretty //pretty是輸出json格式化后的數(shù)據(jù){ "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.插入數(shù)據(jù)
重點(diǎn): 插入數(shù)據(jù)這塊必須在index名后面加上/_doc/ , 本身這里應(yīng)該是設(shè)置type的字段,但是在實(shí)際插入數(shù)據(jù)時(shí),用自定義的stu,class等都會(huì)報(bào)錯(cuò),難道只能插入doc類型? ps:正常猜想是在mapping中沒有設(shè)置最外層的type,但是實(shí)際操作時(shí)聲明type會(huì)報(bào)錯(cuò)

POST localhost:9200/test_mapping7/_doc/1 {"group": "研發(fā)","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 }

查詢一下插入的數(shù)據(jù)

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" : "研發(fā)", "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": "研發(fā)","user": [{"id": 1,"name": "zhangsan","age": 12,"project": [{"name": "pro1"}]},{"id": 2,"name": "lisi","age": 14,"project": [{"name": "pro12"}]}]}}]} }

可以看到直接返回了整個(gè)doc1對象,符合預(yù)期結(jié)果

那么我們?nèi)绻樵儣l件是project.name=pro4呢?劃重點(diǎn)
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"}]}]}}]} }

成功命中
未完待續(xù),歡迎補(bǔ)充指正

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。