2

我有以下映射:

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"
     }
    }
}'

这显示了不同的结果。

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

4

1 回答 1

1

第一个查询意味着:

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

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

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

于 2014-01-16T00:14:50.367 回答