繁体   English   中英

在Elasticsearch中组合非嵌套和嵌套查询

[英]Combined non-Nested and Nested Query in Elasticsearch

我想用ES进行图书搜索。 所以我决定将作者姓名和标题(作为嵌套文档)放入索引中,如下所示:

curl -XPUT localhost:9200/library/search_books/1 -d'{
  "author": "one",
  "books": [
    {
      "title": "two",
    },
    {
      "title": "three",
    }
  ]
}'

我没有得到的结果是:我如何构建搜索查询,在搜索“一二”时只查找第二本书,在搜索“二三”时找不到任何内容,在搜索“一”时查找所有书籍?

也许是这样的?

{
  "query":{
    "bool":{
      "must":[
        {
          "term":{
            "author":"one"
          }
        },
        {
          "nested":{
            "path":"books",
            "query":{
              "term":{
                "books.title":"two"
              }
            }
          }
        }
      ]
    }
  }
}

该查询基本上表示文档必须有author: onebooks.title: two 您可以轻松地重新配置该查询。 例如,如果您只想搜索作者,请删除嵌套部分。 如果你想要一本不同的书,可以更改嵌套等等。

这假设您使用的是实际的嵌套文档 ,而不是内部对象。 对于内部对象,您可以使用完全限定的路径而无需特殊的嵌套查询。

编辑1 :你可以在索引时通过巧妙的提升来实现这一点,尽管它只是一个近似的解决方案。 如果“作者”被大量提升,即使标题与查询的两个部分匹配,它也会比仅仅标题的匹配排序更高。 然后,您可以使用min_score截止值来阻止这些显示。

它只是一个松散的近似,因为有些可能会蔓延。 对于“正确”匹配之间的一般排序,它也可能做一些奇怪的事情。

Edit2:使用query_string更新以显示“单个输入”选项:


{
  "query":{
    "query_string" : {
      "query" : "+author:one +books.title:two"
    }
  }
}

这是假设你使用默认的“内部对象”。 如果你有真正的嵌套类型,query_string变得更加复杂:


{
  "query":{
    "query_string" : {
      "query" : "+author:one +BlockJoinQuery (filtered(books.title:two)->cache(_type:__books))"
    }
  }
}

巨大的免责声明我没有测试这两个query_strings中的任何一个,所以它们可能不完全正确。 但他们表明Lucene语法并不过分友好。


Edit3 - 这是我最好的主意:

在考虑之后,您的最佳解决方案可能是索引连接作者和书名的特殊字段。 像这样的东西:

{
  "author": "one",
  "books": [
    {
      "title": "two",
    },
    {
      "title": "three",
    }
  ],
  "author_book": [ "one two", "one three" ]
}

然后在搜索时,您可以在author_book上执行完全匹配的author_book

{
  "query" : {
    "term" : {
      "author_book" : "one two"
    }
  }
}

我在这篇文章中找到了答案: 使用Elasticsearch的子项和嵌套文档 嵌套文档是关键。 映射:

{
  "book":{
    "properties": {
      "tags": { "type": "multi_field",
        "fields": {
            "tags": { "type": "string", "store":"yes", "index": "analyzed" },
            "facet": { "type": "string", "store":"yes", "index": "not_analyzed" }
        }
      },
      "editions": { "type": "nested", 
        "properties": {
          "title_author": { "type": "string", "store": "yes", "index": "analyzed" },
          "title": { "type": "string", "store": "yes", "index": "analyzed" }
        }
      }
    }
  }
}

文件:

"tags": ["novel", "crime"],
  "editions": [
    {
      "title": "two",
      "title_author": "two one"
    },
    {
      "title": "three",
      "title_author": "three one"
    }
  ]

现在我可以搜索:

{

  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "editions",
            "query": {
              "match": {
                "editions.title_author": {
                  "query": "one two",
                  "operator": "and"
                }
              }
            }
          }
        }
      ]
    }
  }
}

如果搜索“两个三”,我就不会得到一个匹配。 我会得到一个“一二”或“一三”。 在1.1.0版本中,将有另一个带有multi_match查询和cross_fields选项的选项,它允许不重复标题,只将作者名称添加到每个嵌套文档中。 这将使指数保持较小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM