在 elasticsearch 中映射具有多个级别的书籍,嵌套与父子关系



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

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文档中,这对于大书籍来说可能相当拗口。

然而,如果您为每个章节和/或部分使用父/子文档,您最终可能会得到更小的块,这些块更易于咀嚼......

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

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

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

嵌套
基本上

是一种将所有内容填充到同一文档中的方法。这对于搜索很有用,但它使某些事情变得更加困难。

例如,如果您尝试查找特定的章节部分,则查询将返回正确的文档 - 整本书。我想这可能不是你要找的,因此parent/child关系将是合适的方式。

或者只是不打扰,将书籍/章节/部分视为索引中的单独类型,按需查询和"加入"。

最新更新