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

时间:2013-03-22 18:32:28

标签: elasticsearch

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

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

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

2 个答案:

答案 0 :(得分:21)

也许是这样的?

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

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

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

Edit1 :你可以在索引时通过巧妙的提升来实现这一点,尽管它只是一个近似的解决方案。如果“作者”被大量提升,即使标题与查询的两个部分匹配,它也会比仅仅标题的匹配排序更高。然后,您可以使用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上执行完全匹配的匹配:

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

答案 1 :(得分:4)

我在这篇文章中找到了答案:Fun With Elasticsearch's Children and Nested Documents。嵌套文档是关键。映射:

{
  "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选项的选项,它允许不重复标题,只将作者名称添加到每个嵌套文档中。这将使指数保持较小。

相关问题