4

Web サイトの各ページ ビューを ES インデックスに保存しています。各ページはentity_idによって認識されます。特定の時点以降の一意のページ ビューの合計数を取得する必要があります。次のマッピングがあります。

{
   "my_index": {
      "mappings": {
         "page_views": {
            "_all": {
               "enabled": true
            },
            "properties": {
               "created": {
                  "type": "long"
               },
               "entity_id": {
                  "type": "integer"
               }
            }
         }
      }
   }
}

Elasticsearch docs によると、それを行う方法はカーディナリティ集計を使用することです。これが私の検索リクエストです。

GET my_index/page_views/_search
{
  "filter": {
    "bool": {
      "must": [
        [
          {
            "range": {
              "created": {
                "gte": 9999999999
              }
            }
          }
        ]
      ]
    }
  },
  "aggs": {
    "distinct_entities": {
      "cardinality": {
        "field": "entity_id",
        "precision_threshold": 100
      }
    }
  }
}

将来のタイムスタンプを使用したため、結果が返されないことに注意してください。そして、私が得ている結果は次のとおりです。

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
   },
   "aggregations": {
      "distinct_entities": {
         "value": 116
      }
   }
}

検索クエリのページ訪問がまったくないことを考えると、一意のページ訪問が 116 になる理由がわかりません。私は何を間違っていますか?

4

1 に答える 1

3

集計はカーディナリティのグローバル値を返しています。フィルタリングされたセットのカーディナリティのみを返したい場合は、フィルター集計を使用し、その中にカーディナリティ集計をネストする方法があります。わかりやすくするためにフィルター処理されたクエリを省略します (簡単に追加できます)。私が試したクエリは次のようになります。

curl -XPOST "http://localhost:9200/my_index/page_views/_search " -d'
{
   "size": 0, 
   "aggs": {
      "filtered_entities": {
         "filter": {
            "bool": {
               "must": [
                  [
                     {
                        "range": {
                           "created": {
                              "gte": 9999999999
                           }
                        }
                     }
                  ]
               ]
            }
         },
         "aggs": {
            "distinct_entities": {
               "cardinality": {
                  "field": "entity_id",
                  "precision_threshold": 100
               }
            }
         }
      }
   }
}'

戻り値:

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 4,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "filtered_entities": {
         "doc_count": 0,
         "distinct_entities": {
            "value": 0
         }
      }
   }
}

ここにあなたが遊ぶことができるいくつかのコードがあります:

http://sense.qbox.io/gist/bd90a74839ca56329e8de28c457190872d19fc1b

ちなみに、Elasticsearch 1.3.4 を使用しました。

于 2014-12-12T23:44:29.910 に答える