简体   繁体   English

Elasticsearch-如何设置N对N关系

[英]Elasticsearch - How to set n-to-n relationship

I am trying to create a Books library application. 我正在尝试创建一个Books库应用程序。 A user can borrow many books. 用户可以借很多本书。 A book can be borrowed by many users in its lifetime. 一生中很多用户都可以借用一本书。 In ElasticSearch there will be user documents and book documents. 在ElasticSearch中将有用户文档和书籍文档。 How can I search for all books that a user borrowed? 如何搜索用户借用的所有图书?

  1. If I create a user-book document, then the book details are repeated as many times as the book is borrowed. 如果创建用户书籍文档,则书籍详细信息会重复到书籍借阅的次数。

  2. If I have an array in Book document listing all the users that borrowed the book, then the array will be enormous for popular books. 如果我在“图书”文档中有一个数组,其中列出了所有借用该图书的用户,那么该数组对于热门图书将是巨大的。

Which is better? 哪个更好? or is there a simpler and better solution that the above 2? 还是有一个比上述2更简单更好的解决方案?

Main question is IMHO what do you want to search? 主要问题是恕我直言,您想搜索什么?

  1. If you want to search for books (title, isbn...), then store books. 如果要搜索书籍(书名,isbn ...),请存储书籍。
  2. If you want to search for Users (name, address, city...), then store users. 如果要搜索用户(名称,地址,城市...),则存储用户。
  3. If you want to search for users that borrow a book, then store a user and an array of borrowed books. 如果要搜索借书的用户,则存储用户和借书的数组。

That means that if you need 1 and 3, you will store twice the information about your book (one time in book type and another time in user type). 这意味着,如果您需要1和3,则将存储有关您的图书的信息的两倍(一次为书籍类型,另一次为用户类型)。

If you need to add informations about borrow date and return date, just add it to your array of "checkout" objects. 如果您需要添加有关借用日期和还款日期的信息,只需将其添加到“ checkout”对象数组中即可。

I will probably design it like this... 我可能会这样设计...

{
  "name":"david",
  "birthdate":"1971-12-26",
  "books":[
    {
      "title":"Star wars",
      "author":"Georges Lucas",
      "borrowdate":"2012-12-18",
      "returndate":null
    },
    {
      "title":"Star Trek",
      "author":"Whatever his name",
      "borrowdate":"2012-07-01",
      "returndate":"2012-08-15"
    }
  ]
}

Does it help? 有帮助吗?

David. 大卫。

Create three types in your index, one for Books, one for User and one for a checkout. 在索引中创建三种类型,一种用于“书籍”,一种用于“用户”,一种用于结帐。 Store info about users and books using types "user" and "book" respectively. 使用“用户”和“书”类型分别存储有关用户和书的信息。 Every time a user checks out a book, store a document containing the user ID and book ID using type "checkout". 每次用户签出一本书时,请使用“签出”类型存储包含用户ID和书ID的文档。 Query the checkout type for all the books checked out by a user and all the users who checked out a book. 查询用户签出的所有书籍和所有签出书籍的用户的签出类型。 Meanwhile, information for users and books is stored only once. 同时,有关用户和书籍的信息仅存储一次。

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

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