2019獨角獸企業重金招聘Python工程師標準>>>
我們在通過elasticsearch查詢text類型的字段時,我們使用aggs進行聚合某個text類型field。這時elasticsearch會自動進行分詞將分詞后的結果進行聚合。獲取每一個分詞出現在文檔的文檔個數。注意:是文檔的次數不是文檔中分詞出現的次數,也就是說即便某個詞在某個文檔中出現了多次,但是只記錄這個詞的doc_count次數為1.
添加一個可分詞的text字段模板:
需要添加 analyzer 和 fielddata兩個屬性
[java]?view plain?copy
- "allContent":?{??
- ???????????????"type":?"text",??
- ???????????????"analyzer":?"ik_smart",??
- ???????????????"fielddata":?true??
- ??????????????}??
?
?
查詢語句例子:
?
[java]?view plain?copy
- GET?voice*/_search??
- {??
- ??"_source":?"{transData.allContent}",??
- ??"query":?{},??
- ??"aggs":?{??
- ????"hotword":?{??
- ??????"terms":?{??
- ????????"field":?"transData.allContent",??
- ????????"size":?10,??
- ????????"order":?{??
- ??????????"_count":?"desc"??
- ????????}??
- ??????}??
- ????}??
- ??},??
- ??"size":?0??
- }??
這里的size:0控制的是結果中hits展示的個數。
?
查詢結果例子:
?
[java]?view plain?copy
- {??
- ??"took":?0,??
- ??"timed_out":?false,??
- ??"_shards":?{??
- ????"total":?5,??
- ????"successful":?5,??
- ????"failed":?0??
- ??},??
- ??"hits":?{??
- ????"total":?1,??
- ????"max_score":?0,??
- ????"hits":?[]??
- ??},??
- ??"aggregations":?{??
- ????"hotword":?{??
- ??????"doc_count_error_upper_bound":?1,??
- ??????"sum_other_doc_count":?314,??
- ??????"buckets":?[??
- ????????{??
- ??????????"key":?"ok",??
- ??????????"doc_count":?119??
- ????????},??
- ????????{??
- ??????????"key":?"一",??
- ??????????"doc_count":?123??
- ????????},??
- ????????{??
- ??????????"key":?"一下",??
- ??????????"doc_count":?114??
- ????????},??
- ????????{??
- ??????????"key":?"一個",??
- ??????????"doc_count":?91??
- ????????},??
- ????????{??
- ??????????"key":?"一個月",??
- ??????????"doc_count":?52??
- ????????},??
- ????????{??
- ??????????"key":?"一些",??
- ??????????"doc_count":?23??
- ????????},??
- ????????{??
- ??????????"key":?"一包",??
- ??????????"doc_count":?13??
- ????????},??
- ????????{??
- ??????????"key":?"一塊",??
- ??????????"doc_count":?11??
- ????????},??
- ????????{??
- ??????????"key":?"一天",??
- ??????????"doc_count":?4??
- ????????},??
- ????????{??
- ??????????"key":?"一定",??
- ??????????"doc_count":?2??
- ????????}??
- ??????]??
- ????}??
- ??}??
- }??