2016-05-20 39 views
1

我有一個文件,看起來像這樣:Elasticsearch - 基數在整個字段值

{ 
    "_id":"some_id_value", 
    "_source":{ 
     "client":{ 
     "name":"x" 
     }, 
     "project":{ 
     "name":"x November 2016" 
     } 
    } 
} 

我試圖執行一個查詢,將獲取我的爲每個客戶獨特的項目名稱計數。爲此,我在project.name上使用cardinality的查詢。我相信這個特定客戶端只有4個獨特的項目名稱。但是,當我運行我的查詢時,我得到的計數爲5,我知道這是錯誤的。

項目名稱全部包含客戶端的名稱。例如,如果客戶是「X」,項目名稱將是「X測試2016年11月」或「X 2016年1月」等,我不知道這是否是考慮因素。

這是文檔類型

{ 
    "mappings":{ 
     "vma_docs":{ 
     "properties":{ 
      "client":{ 
       "properties":{ 
        "contact":{ 
        "type":"string" 
        }, 
        "name":{ 
        "type":"string" 
        } 
       } 
      }, 
      "project":{ 
       "properties":{ 
        "end_date":{ 
        "format":"yyyy-MM-dd", 
        "type":"date" 
        }, 
        "project_type":{ 
        "type":"string" 
        }, 
        "name":{ 
        "type":"string" 
        }, 
        "project_manager":{ 
        "index":"not_analyzed", 
        "type":"string" 
        }, 
        "start_date":{ 
        "format":"yyyy-MM-dd", 
        "type":"date" 
        } 
       } 
      } 
     } 
     } 
    } 
} 

映射這是我的搜索查詢

{ 
    "fields":[ 
     "client.name", 
     "project.name" 
    ], 
    "query":{ 
     "bool":{ 
     "must":{ 
      "match":{ 
       "client.name":{ 
        "operator":"and", 
        "query":"ABC systems" 
       } 
      } 
     } 
     } 
    }, 
    "aggs":{ 
     "num_projects":{ 
     "cardinality":{ 
      "field":"project.name" 
     } 
     } 
    }, 
    "size":5 
} 

這些都是結果我得到(我只貼2個結果簡潔起見) 。請發現num_projects聚合返回5,但只能返回4,這是項目的總數。

{ 
    "hits":{ 
     "hits":[ 
     { 
      "_score":5.8553367, 
      "_type":"vma_docs", 
      "_id":"AVTMIM9IBwwoAW3mzgKz", 
      "fields":{ 
       "project.name":[ 
        "ABC" 
       ], 
       "client.name":[ 
        "ABC systems Pvt Ltd" 
       ] 
      }, 
      "_index":"vma" 
     }, 
     { 
      "_score":5.8553367, 
      "_type":"vma_docs", 
      "_id":"AVTMIM9YBwwoAW3mzgK2", 
      "fields":{ 
       "project.name":[ 
        "ABC" 
       ], 
       "client.name":[ 
        "ABC systems Pvt Ltd" 
       ] 
      }, 
      "_index":"vma" 
     } 
     ], 
     "total":18, 
     "max_score":5.8553367 
    }, 
    "_shards":{ 
     "successful":5, 
     "failed":0, 
     "total":5 
    }, 
    "took":4, 
    "aggregations":{ 
     "num_projects":{ 
     "value":5 
     } 
    }, 
    "timed_out":false 
} 

FYI:項目名稱爲ABCABC Nov 2016ABC retest NovemberABC Mobile App

+0

您可以提供測試場景的要點嗎? (索引映射,一些數據樣本和查詢) –

+0

@AndreiStefan - 我已經添加了您所要求的詳細信息。希望能幫助到你。 –

回答

1

您需要爲您的project.name場下面的映射:

{ 
    "mappings": { 
    "vma_docs": { 
     "properties": { 
     "client": { 
      "properties": { 
      "contact": { 
       "type": "string" 
      }, 
      "name": { 
       "type": "string" 
      } 
      } 
     }, 
     "project": { 
      "properties": { 
      "end_date": { 
       "format": "yyyy-MM-dd", 
       "type": "date" 
      }, 
      "project_type": { 
       "type": "string" 
      }, 
      "name": { 
       "type": "string", 
       "fields": { 
       "raw": { 
        "type": "string", 
        "index": "not_analyzed" 
       } 
       } 
      }, 
      "project_manager": { 
       "index": "not_analyzed", 
       "type": "string" 
      }, 
      "start_date": { 
       "format": "yyyy-MM-dd", 
       "type": "date" 
      } 
      } 
     } 
     } 
    } 
    } 
} 

這基本上是一個叫raw在同一子放入project.name的價值放入project.name.raw但未觸及它(標記或分析它)。然後,您需要使用的查詢是:

{ 
    "fields": [ 
    "client.name", 
    "project.name" 
    ], 
    "query": { 
    "bool": { 
     "must": { 
     "match": { 
      "client.name": { 
      "operator": "and", 
      "query": "ABC systems" 
      } 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "num_projects": { 
     "cardinality": { 
     "field": "project.name.raw" 
     } 
    } 
    }, 
    "size": 5 
}