当前位置: 首页 > 面试题库 >

在Elasticsearch中映射具有多层,嵌套与父子关系的书籍

舒斯伯
2023-03-14
问题内容

在为可以搜索多本书的索引创建映射时,最好使用如下所示的嵌套映射,或者使用具有父子关系的文档

book: {
  properties: {
    isbn:     {       //- ISBN of the book
      type: 'string'  //- 9783791535661
    },
    title:    {       //- Title of the book
      type: 'string'  //- Alice in Wonderland
    },
    author:   {       //- Author of the book(maybe should be array)
      type: 'string'  //- Lewis Carroll
    },
    category: {       //- Category of the book(maybe should be array)
      type: 'string'  //- Fantasy
    },
    toc: {            //- Array of the chapters in the book
      type: 'nested',
      properties: {
        html: {           //- HTML Content of a chapter
          type: 'string'  //- <!DOCTYPE html><html>...</html>
        },
        title: {          //- Title of the chapter
          type: 'string'  //- Down the Rabbit Hole 
        },
        fileName: {       //- File name of this chapter
          type: 'string'  //- chapter_1.html
        }, 
        firstPage: {      //- The first page of this chapter
          type: 'integer' //- 3
        }, 
        numberOfPages: {  //- How many pages are in this chapter
          type: 'integer' //- 27
        },
        sections: {       //- An array of all of the sections within a chapter
          type: 'nested',
          properties: {
            html: {           //- The html content of a section
              type: 'string'  //- <section>...</section>
            },
            title: {          //- The title of a section
              type: 'string'  //- section number 2 or something
            },
            figures: {        //- Array of the figures within a section
              type: 'nested',
              properties: {
                html: {           //- HTML content of a figure
                  type: 'string'  //- <figure>...</figure>
                },
                caption: {        //- The name of a figure
                  type: 'string'  //- Figure 1
                },
                id: {             //- Id of a figure
                  type: 'string', // figure4
                }
              }
            },
            paragraphs: {     //- Array of the paragraphs within a section
              type: 'nested',
              properties: {   
                html: {           //- HTML content of a paragraph
                  type: 'string', //- <p>...</p>
                }
                id: {             //- Id of a paragraph
                  type: 'string', // paragraph3
                }
              }
            }
          }
        }
      }
    }
  }
}

整个书籍html的大小约为250kB。我想查询诸如

- the best matching paragraph including it's nearest paragraphs on either side
- the best matching section from a single book including any child sections
- the best figure given it is inside a section with a matching title
- etc

我真的不知道我想执行的查询的细节,但是具有很大的灵活性以便能够尝试非常怪异的查询而不必过多更改所有映射非常重要。


问题答案:

如果使用该nested类型,则所有内容都将包含在同一_source文档中,对于大型书籍而言,这可能会很麻烦。

而如果您为每个章节和/或章节使用父级/子级文档,则最终可能会遇到较小的块,这更容易咀嚼…

与往常一样,它在很大程度上取决于您要进行的查询,因此您应该首先考虑要支持的所有用例,然后才能更好地确定哪种方法是最佳的。

还有另一种方法既不使用嵌套也不使用父/子,并且仅涉及非规范化。具体来说,您可以选择要考虑的最小“实体”,例如一个部分,然后为每个部分创建独立的文档。在这些章节文档中,您将具有书籍标题,作者,章节标题,章节标题等字段。

您可以在自己的索引中尝试每种方法,并查看它们在用例中的作用。



 类似资料:
  • 问题内容: Book,User和Review说,我正在构建具有复杂模型的应用程序。 评论包含书籍和用户ID。为了能够搜索至少包含一个评论的“图书”,我已将“图书”设置为“评论”的父级,并且具有这样的路由。但是,我还需要找到撰写包含某些短语的评论的用户。 是否可以同时将书和用户作为评论的父级?有没有更好的方法来处理这种情况? 请注意,我无法更改数据建模的方式/不愿意这样做,因为数据已从持久性数据库传

  • 我用下面的方法尝试了嵌套映射。 我在声明“root_cause”时出错:[{“type”:“mapper_parsing_exception”,“reason”:“root映射定义有不支持的参数:[type:nested]。” 感谢您的帮助。

  • 我正在使用弹性搜索和Spring数据弹性。并尝试使用多重搜索。问题是在使用类字段搜索时,它不适用于嵌套字段。我的映射如下所示 我有一个带有searchQuery的endpoint,比如: 问题是查询不适用于嵌套字段。有什么建议吗? 使现代化 事实上,嵌套对象可以查询为 有没有两个类似的问题

  • 问题内容: 我有下表: 我想让Folder类具有父子关系。 问题答案: 我相信正确的映射将是: 该会的工作只有当每个家长有最多一个孩子,上面的代码适用于更一般的情况下,父母可以有许多儿童。另外,为简单起见,我省略了get / set方法。

  • 使用ES(或Solr)建模productvariants让我大吃一惊 考虑(人为的例子): 不同的产品(如T恤) 每个产品都有一组属性(productid、名称、desc、品牌、颜色、知名度) 每个产品都有一组具有属性的productvariants(productvariantid(productid++大小的组合)、productid、大小、可用性、价格) 这似乎是product和Produc

  • 随着ElasticSearch计划删除映射类型,这是否也意味着不再使用嵌套文档和嵌套查询?ElasticSearch将如何支持无类型上下文中的嵌套对象查询? 我正在考虑的功能是能够只返回匹配搜索条件的嵌套数组中的命中。 编辑1:ElasticSearch版本6中的映射查询示例 ElasticSearch 6映射 ElasticSearch 6查询