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

标签 elasticsearch elasticsearch.js

在为可以搜索多本书的索引创建映射时,最好使用如下所示的嵌套映射,或者使用带有parent-child relationship的文档

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文档中,对于大型书籍而言,这可能会很麻烦。

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

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

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

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

关于elasticsearch - 在Elasticsearch中映射具有多层,嵌套与父子关系的书籍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35153578/

相关文章:

elasticsearch - 如何从Web站点使用Elastic&Kibana收集日志和收集日志

javascript - 如何使用 elasticsearch.js 和 Angular 在 Elasticsearch 上搜索嵌套对象

elasticsearch - Elasticsearch _all 索引

elasticsearch - 如何使用tags_schema突出显示Elasticsearch术语?

elasticsearch - 如何用 Elasticsearch 中的另一个字段覆盖 @timestamp 字段?

node.js - 在与数据库的连接上同步 elasticsearch - nodeJS

python-3.x - 添加未在ElasticSearch索引中显示的新文档

javascript - ElasticSearch.js 搜索查询返回 0 个匹配项,但生成的 URL 包含正确的匹配项

node.js - 如何使用elasticsearch.js客户端列出所有索引