生活随笔
收集整理的這篇文章主要介紹了
elasticsearch分词聚合查询demo
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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??????????}????????]??????}????}??}??
轉載于:https://my.oschina.net/xiaominmin/blog/1785461
總結
以上是生活随笔為你收集整理的elasticsearch分词聚合查询demo的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。