【问题标题】:Elasticsearch - return results per 'hit' rather than documentElasticsearch - 每个“命中”而不是文档返回结果
【发布时间】:2017-04-27 20:20:18
【问题描述】:

我对 ES 很陌生,

我正在使用 ingest-attachments-processor-plugin 和 elasticsearch 索引 pdf 书籍,以将我的查询结果作为 PER hit 而不是结果 PER 文档返回。例如,如果我正在索引几本 pdf 书籍,我想大致返回每个匹配发生的段落,这样如果 1 个文档中有 3 个匹配项,则应该有 3 个结果(段落),如果另一个匹配项有 2 个匹配项文件,应该有 5 次点击。我们的目标是创建一个可以从这些书中检索正确答案的工具。

1) 这可以通过弹性搜索实现吗? Solr 能解决这个问题吗? 2)我如何让它返回说一段,我不相信ES理解句子结构吗?有这个插件吗? 3) 我是否应该按段落解析文档(就像 Watson 使用 Document Conversion API 处理答案单元一样)然后索引到 ES 中?

这是我目前在 python 中使用的查询:

def execute_es_query(query_input, index, doc_type):
body_query = {
    "stored_fields": [],
    "query": {
        "query_string": {
            "default_field": "attachment.content",
            "query": query_input
        }
    },
    "highlight": {
        "pre_tags": "<span>",
        "post_tags": "</span>",
        "fields": {
            "attachment.content":{}
        }
    }
}
response = es_client.search(index= index, doc_type = doc_type, body = body_query)
return response['hits']['hits']

def fetch_response(response):
num = 1
for i in response:
    print "Result:", num
    for j in i['highlight']['attachment.content']:
        print j
    print "\n"
    num+=1

【问题讨论】:

  • 你想让 ES 在结果中只返回段落字段,而不是整个文档吗?
  • 是的,就像书中的段落作为每个结果而不是整个文档作为一个结果。

标签: python elasticsearch


【解决方案1】:

如果您将段落存储为nestedchild 文档,则可以使用inner-hits feature

源自文档:

{
  "query" : {
    "nested" : {
      "path" : "attachment",
        "query_string": {
          "default_field": "attachment.content",
          "query": query_input
         }
         "inner_hits" : {} 
       }
     }
}

然后,响应将在每个返回的文档中包含一个名为 inner_hits 的字段,其中包含每个返回的文档的匹配段落。

此方法仍会将文档作为主要搜索结果返回。如果您想搜索段落,我的建议是将它们作为单独的类型进行索引。

【讨论】:

  • 谢谢,我想我必须在索引之前将整个文档解析为段落。
猜你喜欢
  • 2019-12-09
  • 2021-12-09
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
  • 2013-09-02
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多