elastic search6.2.2 實現用戶搜索記錄查詢(去重、排序)
阿新 • • 發佈:2018-02-28
replicas pos 結果 etime rep col cas rms idt
elastic search6.2.2 實現搜索記錄查詢 ,類似新浪微博這種,同樣的搜索記錄後面時間點的會覆蓋前面的(主要思路:關鍵詞去重,然後按時間排序)
先創建索引
//我的搜索 PUT my_search { "settings":{ "number_of_replicas": 0 }, "mappings":{ "_doc":{ "properties":{ "user": {"type": "integer"}, "word": {"type": "keyword"},"datetime": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } } } }
插入搜索記錄
POST my_search/_doc // 自動生成ID { "user": 1, "word": "區塊鏈", "datetime":"2018-02-08 23:58:59" } { "user": 1,"word": "劉平", "datetime":"2018-03-08 23:58:59" } { "user": 1, "word": "你好", "datetime":"2018-04-08 23:58:59" } { "user": 1, "word": "我好", "datetime":"2018-05-08 23:59:59" } { "user": 1, "word": "你好", "datetime":"2018-06-08 23:58:59" } { "user": 1, "word": "他好","datetime":"2018-07-09 23:58:59" } { "user": 1, "word": "他好", "datetime":"2018-08-09 23:58:59" }
寫入文檔後的搜索記錄可視界面(以按時間排序)
下面要讀取出來了
POST my_search/_search // 我的最近搜索記錄(展示10條) { "query":{ "match":{ "user":"1" } }, "aggs":{ "my_so":{ "terms":{ "field":"word", "size":10, "order":{ "max_time":"desc" } }, "aggs":{ "max_time":{ "max":{ "field":"datetime" } } } } }, "size":0, "from":0 }
搜索結果:
elastic search6.2.2 實現用戶搜索記錄查詢(去重、排序)