【发布时间】:2016-01-04 03:24:54
【问题描述】:
我需要根据以下范围对文档进行文本搜索:
- 整个文档
- 章节
- 段落
- 句子
是否可以对文档进行索引,以便我可以根据此要求过滤查询范围?
根据答案进行编辑
我现在已经创建了以下索引
{
"settings": {
"analysis": {
"analyzer": {
"folding": {
"tokenizer": "standard",
"filter": [ "lowercase", "asciifolding" ]
}
}
}
},
"mappings": {
"books": {
"properties": {
"content": {
"type": "string",
"fields": {
"english": {
"type": "string",
"analyzer": "english"
},
"folded": {
"type": "string",
"analyzer": "folding"
}
}
},
"author": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"language": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"source": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"title": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"fileType": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
},
"sections": {
"_parent": { "type": "books" },
"properties": {
"content": {
"type": "string",
"fields": {
"english": {
"type": "string",
"analyzer": "english"
},
"folded": {
"type": "string",
"analyzer": "folding"
}
}
},
"paragraphs": {
"type": "nested",
"properties": {
"paragraph": {
"properties": {
"page": { "type": "integer" },
"number": { "type": "integer" },
"html_tag": { "type": "string" },
"content": { "type": "string" }
}
}
}
},
"author": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"language": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"source": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"title": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"fileType": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
},
"messages": {
"properties": {
"content": {
"type": "string",
"fields": {
"english": {
"type": "string",
"analyzer": "english"
},
"folded": {
"type": "string",
"analyzer": "folding"
}
}
},
"paragraphs": {
"type": "nested",
"properties": {
"paragraph": {
"properties": {
"page": { "type": "integer" },
"number": { "type": "integer" },
"html_tag": { "type": "string" },
"content": { "type": "string" }
}
}
}
},
"author": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"language": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"source": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"title": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"fileType": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
这给了我以下类型:书籍、部分(父书籍)和消息。 Sections and Messages 有嵌套类型 Paragraphs,我跳过了句子级别。
我现在可以对书籍级别的内容和章节级别的内容执行搜索。这使我可以在段落之间搜索单词。如果我想匹配段落中的两个单词,我也可以直接在段落级别进行搜索。
示例:假设我有以下文档
paragraph 1: It is a beautiful warm day.
paragraph 2: The cloud is clear.
我现在可以在内容级别搜索美丽的 AND 云并取回文档。但是,如果我使用嵌套搜索在段落级别搜索美丽的 AND 云,我不会取回文档,这正是我想要的。
我看到这个解决方案存在的问题是:
- 我需要将同一段落索引 3 次。一次在段落级别,一次在内容部分级别,一次在内容书籍级别。
- 我不明白 Books 和 Sections 之间的父子关系能给我带来什么好处。我还没有找到任何使用突出显示同时搜索两者的方法。
- 我需要一个单独的 Message 类型,它与没有父级的 Section 类型完全相同。有没有办法在没有父母的情况下让孩子打字,这样我就可以避免额外的打字?
【问题讨论】:
-
也许使用自定义分析器qbox.io/blog/introduction-to-elasticsearch-analyzers,可以创建一个自定义分析器/tokeniser/ngram 来将单词分组为句子等......然后倒排索引将具有这种结构并且可以查询为这样的(尽管即使对于该领域的专家程序员来说也不是一件容易的事)
-
能否提供一份示例文档,以便我们更好地理解?
-
感谢 Nikos M 的建议。然而,它们现在确实感觉有点复杂,我会在将来有工作时再看看它们。
-
ChintanShah25:我更新的问题是否有帮助,还是您觉得我需要添加更多信息?
标签: elasticsearch