【问题标题】:How to use facet filtering with nested documents on ElasticSearch如何在 ElasticSearch 上对嵌套文档使用分面过滤
【发布时间】:2014-01-15 17:01:12
【问题描述】:

我有以下映射:

curl -XPUT 'http://localhost:9200/bookstore/user/_mapping' -d '
{
  "user": {
    "properties": {
      "user_id": { "type": "integer" },
      "gender": { "type": "string", "index" : "not_analyzed" },
      "age": { "type": "integer" },
      "age_bracket": { "type": "string", "index" : "not_analyzed" },
      "current_city": { "type": "string", "index" : "not_analyzed" },
      "relationship_status": { "type": "string", "index" : "not_analyzed" },
      "books" : {
        "type": "nested",
        "properties" : {
          "b_oid": { "type": "string", "index" : "not_analyzed" },
          "b_name": { "type": "string", "index" : "not_analyzed" },
          "bc_id": { "type": "integer" },
          "bc_name": { "type": "string", "index" : "not_analyzed" },
          "bcl_name": { "type": "string", "index" : "not_analyzed" },
          "b_id": { "type": "integer" }
        }
      }
    }
  }
}'

现在,我尝试查询具有“性别”的用户:“男性”,购买了某个类别的书“bcl_name”:“琐事”并显示“b_name”书名。我不知何故无法让它运行。

我有问题

curl -XGET 'http://localhost:9200/bookstore/user/_search?pretty=1' -d '{
    "size": 0,
    "from": 0,
    "query": {
     "filtered": {
         "query": {
             "terms": {
                 "gender": [
                     "Male"
                 ]
             }
         }
     }
    },
    "facets": {
        "CategoryFacet": {
             "terms": {
                 "field": "books.b_name",
                 "size": 5,
                 "shard_size": 1000,
                 "order": "count"
             },
             "nested": "books",
             "facet_filter": {
                 "terms": {
                     "books.bcl_name": [
                         "Trivia"
                     ]
                 }
             }
        }
    }
}'

它返回一个结果,但我不确定这是否正确。我找了一些例子,例如找到了这个(http://www.spacevatican.org/2012/6/3/fun-with-elasticsearch-s-children-and-nested-documents/)。我可以像这样重写我的查询:

curl -XGET 'http://localhost:9200/bookstore/user/_search?pretty=1' -d '{
    "size": 0,
    "from": 0,
    "query": {
     "filtered": {
         "query": {
             "terms": {
                 "gender": [
                     "Male"
                 ]
             }
         },
         "filter": {
             "nested": {
                 "path": "books",
                 "query": {
                     "filtered": {
                         "query": {
                             "match_all": {}
                         },
                         "filter": {
                             "and": [
                                 {
                                     "term": {
                                         "books.bcl_name": "Trivia"
                                     }
                                 }
                             ]
                         }
                     }
                 }
             }
         }
     }
    },
    "facets": {
     "CategoryFacet": {
         "terms": {
             "field": "books.b_name",
             "size": 5,
             "shard_size": 1000,
             "order": "count"
         },
         "nested": "books"
     }
    }
}'

显示不同的结果。

作为初学者,我现在有点迷茫。有人可以给我提示如何解决这个问题吗?提前非常感谢!

【问题讨论】:

    标签: nested elasticsearch filtering facets


    【解决方案1】:

    第一次查询意味着:

    • 搜索gender : "Male"的用户
    • 但“CategoryFacet”包括gender : "Male" AND books.bcl_name : "Trivia" 的计数

    因此,在结果集中,您将获得所有“男性”用户,但您的 CategoryFacet 会为您提供“男性用户且其 books.bcl_name 为 Trivia”的计数。

    在第二个查询中,您的“CategoryFacet”不包括额外的过滤。它只是从确切的结果集中返回方面。

    【讨论】:

    • 感谢您的回复。我仍然有点困惑,因为我读到查询部分中使用的过滤器不会影响分面结果。我无法实现的是我可以从嵌套文档方面过滤器中过滤父文档字段。有人可以给我一个这个用例的工作示例吗?
    • 实际上查询部分中的过滤器会影响方面。如果您不想这样做,请使用搜索过滤器。我在新的 elasticsearch 指南中找不到它,但这里有一篇关于它的好博文:substantial.com/blog/2013/01/16/…
    猜你喜欢
    • 1970-01-01
    • 2017-07-04
    • 2018-05-10
    • 2015-05-06
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    相关资源
    最近更新 更多