drf97973
2013-10-25 14:26
浏览 167
已采纳

使用ElasticSearch匹配多个文档

I am relatively new to ElasticSearch. I am using it as a search platform for pdf documents. I break the PDFs into text-pages and enter each one as an elasticSearch record with it's corresponding page ID, parent info, etc.

What I'm finding difficult is matching a given query not only to a single document in ES, but making it match any document with the same parent ID. So if two terms are searched, if the terms existed on page 1 and 7 of the actual PDF document (2 separate entries into ES), I want to match this result.

Essentially my goal is to be able to search through the multiple pages of a single PDF, matching happening on any of the document-pages in the PDF, and to return a list of matching PDF documents for the search result, instead of matching "pages"

图片转代码服务由CSDN问答提供 功能建议

我对ElasticSearch比较新。 我用它作为pdf文档的搜索平台。 我将PDF分解为文本页面并将其作为弹性搜索记录输入相应的页面ID,父信息等。

我发现难以匹配给定的查询而不是 仅限ES中的单个文档,但使其与具有相同父ID的任何文档匹配。 因此,如果搜索了两个术语,如果实际PDF文档的第1页和第7页中存在术语(ES中有2个单独的条目),我想匹配此结果。

基本上我的目标 是能够搜索单个PDF的多个页面,匹配PDF中的任何文档页面上的匹配,并返回搜索结果的匹配PDF文档列表,而不是匹配“页面”

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doudun6928 2013-10-25 15:27
    已采纳

    It's somewhat tricky. First of all, you will have to split your query into terms yourself. Having a list of terms (let's say foo, bar and baz, you can create a bool query against type representing PDFs (parent type) that would look like this:

    {
        "bool" : {
            "must" : [{
                "has_child" : {
                    "type": "page",
                    "query": {
                        "match": {
                            "page_body": "foo"
                        }
                    }
                }
            }, {
                "has_child" : {
                    "type": "page",
                    "query": {
                        "match": {
                            "page_body": "bar"
                        }
                    }
                }
            }, {
                "has_child" : {
                    "type": "page",
                    "query": {
                        "match": {
                            "page_body": "baz"
                        }
                    }
                }
            }]
       }
    }
    

    This query will find you all PDFs that contain at least one page with each term.

    点赞 打赏 评论
  • dppfxf909679 2013-10-27 09:45

    You will need to use the "has_child" query on pages. I'm assumed that you're already defined the mapping for parent/child relationship of documents and pages. Then you can write a "has_child" query that search on pages (child type) but return PDF documents (parent type):

    {
      "query": {
        "has_child": {
          "type": "your_pages_type",
          "score_type": "max", // read document for more
          "query": {
            "query_string": {
              "query": "some text to search",
              "fields": [
                "your_pages_body"
              ],
              "default_operator": "and" // "and" if you want to search all words, "or" if you want to search any of words in query
            }
          }
        }
      }
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题