gpt4 book ai didi

java - Elasticsearch : How to search in two documents in single index?

转载 作者:行者123 更新时间:2023-12-01 17:23:06 25 4
gpt4 key购买 nike

我在名为 inventory 的单个索引中配置了两个文档。

  1. 个人资料(包含用户和移动信息)
  2. p2p 关系

库存索引映射属性如下:

{
"mappings": {
"properties": {
"type": {"type": "keyword"},
"id": {"type": "keyword"},
"sourceId": {"type": "keyword"},
"targetId": {"type": "keyword"},
"firstName": {"type": "text"},
"lastName": {"type": "text"},
"createdBy": {"type": "text"},
"p_type": {"type": "text"},
"model": {"type": "text"},
"OS": {"type": "keyword"}
}
}
}

个人资料包含以下信息

用户:

{   "type":"profile",   "id" : "user1", "p_type" : "user",  "firstName" : "Ashok", "lastName" : "S" }
{ "type":"profile", "id" : "user2", "p_type" : "user", "firstName" : "Arun", "lastName" : "V" }

手机:

{   "type":"profile",   "id" : "mobile1", "p_type" : "mobile",  "model" : "samsung", "OS" : "Android" }
{ "type":"profile", "id" : "mobile2", "p_type" : "mobile", "model" : "iPhone", "OS" : "iOS" }

p2p-relation 具有哪个用户使用哪个移动信息:

{   "type":"p2p-relation",  "id" : "user1-owns-mobile1", "sourceId" : "user1",  "targetId" : "mobile1", "createdBy" : "admin" }
{ "type":"p2p-relation", "id" : "user1-owns-mobile2", "sourceId" : "user1", "targetId" : "mobile2", "createdBy" : "admin" }

在我们的业务案例中,我们需要检索用户拥有的 Android/iOS 手机列表,该列表是我们从客户那里获取的输入。

也就是说,如果 user1 请求 /mymobiles?query=os==Android ,它应该将其转换为 ES 并期望

{ "type":"profile", "id": "mobile1", "p_type": "mobile", "model": "samsung", "OS": "Android"}

作为结果,如果 user2 请求相同的结果,它应该返回空。

我尝试过查询和 boolean 。但它仅在单个文档内搜索。如何在 Elasticsearch 中实现这一点?

最佳答案

上述模型属于多对多关系数据模型。一个用户可以拥有许多分配给他的移动电话,并且在一段时间内可以将一个移动设备分配给许多用户。因此更改了数据模型以将其存储为嵌套对象。

{
"mappings": {
"properties": {
"type": {"type": "keyword"},
"id": {"type": "keyword"},
"firstName": {"type": "text"},
"lastName": {"type": "text"},
"createdBy": {"type": "text"},
"model": {"type": "text"},
"OS": {"type": "keyword"},
"mobiles" : {
"type" : "nested",
"properties" : {
"id": {"type": "keyword"},
"model": {"type": "text"},
"OS": {"type": "keyword"},
"createdBy": {"type": "text"}
}
},
"users" : {
"type" : "nested",
"properties" : {
"id": {"type": "keyword"},
"firstName": {"type": "keyword"},
"lastName": {"type": "keyword"},
"createdBy": {"type": "text"}
}
}
}}}

但痛苦的是,每当将手机分配给用户时,我都需要花费额外的精力来更新两行。

数据存储如下

{   "type":"profile",   "id" : "user1",  "firstName" : "Ashok", "lastName" : "S", "mobiles" : [ {"id" : "uuid1", "model": "samsung", "OS" : "Android"}] }

{ "type":"profile", {"id" : "uuid1", "model": "samsung", "OS" : "Android"}, "users" : [ "id" : "user1", "firstName" : "Ashok", "lastName" : "S"] }

为了回答/mymobiles?query=os==Android,我使用了以下查询。

{ 
"query": {
"bool": {
"must": [
{
{ "term": {"OS": "android"} },
"nested": {
"path": "users",
"query": {
"bool": {
"must": [ {"term": {"users.id": "user1"} }]
}
}
}
}
]
}
}}

我引用了以下链接来执行此操作:https://medium.com/@mena.meseha/briefly-describe-how-to-store-complex-relational-data-in-elasticsearch-f30277317b1

关于java - Elasticsearch : How to search in two documents in single index?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61254991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com