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 が Trivia である」の数を示します。

2 番目のクエリでは、「CategoryFacet」に追加のフィルタリングが含まれていません。正確な結果セットからファセットを返すだけです。

于 2014-01-16T00:14:50.367 に答える