简体   繁体   English

弹性搜索:如何在单个索引中搜索两个文档?

[英]Elastic search : How to search in two documents in single index?

I've two documents configured in a single index named inventory.我在一个名为inventory 的索引中配置了两个文档。

  1. profile (this have users and mobile information)个人资料(这有用户和移动信息)
  2. p2p-relation p2p关系

Inventory index mapping properties are as follows:库存索引映射属性如下:

{
    "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"}
            }
        }
}

profile has the following information profile 有以下信息

users:用户:

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

mobiles:手机:

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

p2p-relation has the which user used which mobile information: 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" }

In our business case, we need to retrieve the list of android/iOS mobiles owned by an user which we get as input from the customer.在我们的业务案例中,我们需要检索用户拥有的 android/iOS 手机列表,我们从客户那里获得输入。

That is, if the user1 requests for /mymobiles?query=os==Android , it should translate this into ES and expecting也就是说,如果 user1 请求/mymobiles?query=os==Android ,它应该将其转换为 ES 并期待

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

as the result and if the user2 requests for the same, it should return empty.结果,如果 user2 请求相同,它应该返回空。

I've tried with query and bool.我试过查询和布尔。 But it search only within a single document.但它只在单个文档中搜索。 How to achieve this in elastic search?如何在弹性搜索中实现这一点?

Above model falls under many to many relationship data model. model以上属于多对多关系数据model。 An user can have many mobile phones assigned to him and a mobile device can be assigned to many users over the period of time.一个用户可以有许多移动电话分配给他,一个移动设备可以在一段时间内分配给许多用户。 So changed the data model to storing it as nested objects.因此将数据 model 更改为将其存储为嵌套对象。

{
  "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"}                               
            }
        }
}}}

But the pain is, whenever a mobile phone is assigned to an user, I have the extra effort of updating two rows.但痛苦的是,每当将手机分配给用户时,我都需要花费额外的精力来更新两行。

Data stored as follows数据存储如下

{   "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"] }

And to answer the /mymobiles?query=os==Android, I've used the following query.为了回答 /mymobiles?query=os==Android,我使用了以下查询。

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

I've referred the following link to do this: https://medium.com/@mena.meseha/briefly-describe-how-to-store-complex-relational-data-in-elasticsearch-f30277317b1我参考了以下链接来执行此操作: https://medium.com/@mena.meseha/briefly-describe-how-to-store-complex-relational-data-in-elasticsearch-f30277317b1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM