且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

弹性搜索中组合非嵌套和嵌套查询

更新时间:2023-02-08 20:24:44

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

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.