弹性搜索中组合非嵌套和嵌套查询 [英] Combined non-Nested and Nested Query in Elasticsearch

查看:107
本文介绍了弹性搜索中组合非嵌套和嵌套查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use ES for a book search. So I decided to put the author name and title (as a nested document) into the index as follows:

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

我没有得到的是:
如何在搜索一二时需要结构化搜索查询查找第二本书在搜索一个时搜索两个三和所有书籍时找不到什么?

What I don't get is: How do I need to structure the search query to find only book two when searching for "one two" and find nothing when searching for "two three" and all books when searching for "one"?

推荐答案

我找到答案在这篇文章中:乐趣与弹性搜索的孩子和嵌套文档。嵌套文档是关键。映射:

I found the answer in this post: Fun With Elasticsearch's Children and Nested Documents. A nested Document is the key. The mapping:

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

And if searched for "two three" I would not get a match. I would get one with "one two" or "one three". In version 1.1.0 there will be another option with a multi_match query and the option cross_fields which would allow not to repeat the title and only add the author name to each nested document. That would keep the index smaller.

这篇关于弹性搜索中组合非嵌套和嵌套查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆