es簡單打造站內搜索

最近挺忙的,在外出差,又同時幹兩個項目。白天一個晚上一個,特別是白天作的項目,立刻就要上線了,在客戶這裏 三天兩頭開會,問題不少真的很想好好靜下來懟代碼,半夜作夢都能fix bugs~ 和客戶交流真的是門技術,一不當心你就會掉坑裏,慢慢來吧~ 

站內搜素其實也是老生常談,估計不少程序員門都作過或者接觸過,記得大三那會 那是比較常見的解決方案就是lucene.net 和盤古分詞,後來又用jieba分詞,html

首先就是和數據庫同步,咱們把數據扔給lucene.net  ,lucene.net 拿到數據 進行分詞,而後保存在索引庫中,當用戶搜索的時候,就從索引庫中進行搜索。lucene.net 對中文分詞不是太優化,因此經常使用的就是盤古分詞  庖丁解牛  jieba分詞,這種方式適合我的站點 數據量不是太大的狀況下,目前不多有采用這種解決方案的,看官們感興趣的能夠百度瞭解一波,實現起來也不難。 
前端

前端時間elastc上市,市值50億美金,剛開始我還嚇一大跳~ 接觸es是去年, 項目作日誌統計使用exceptionless,因此也就初步瞭解了elasticsearch  也逐步瞭解logstash kibana   速度是真的快,吊打sqlserver啊! 哈哈 畢竟不是一系列的東西=java

今天簡單實現的站內搜索採用的就是 elasticsearch,數據源就是這段時間天天爬取博客園獲取到的將近6000篇文章,放到sqlserver了,後續會共享node

起初 想要搞sqlserver 和 es的數據同步,我寫的這個服務每小時就會爬取博客園一次 獲取最新50條數據,重複的就不算了。數據同步能夠採用logstash,首先就是全量同步,再次就是增量同步,多是由於版本緣由吧,都是採用的最新版本,採用logstash進行數據同步 總是失敗,有待探索,索性就用ef 先作個全量同步,再靠這個定時服務作之後的增量,數據自己就是通過去重處理的,何況也不存在修改 刪除的狀況git

 

首先就是配置java環境變量  而後部署 elk 官網地址是 : https://www.elastic.co/cn/程序員

下載好三件套以後 咱們能夠把es部署成windows服務  在bin目錄下 運行elasticsearch-service.batgithub

服務開啓後,es默認http地址是 http://localhost:9200/sql

 

es啓動成功後  啓動kibana 服務  一樣也是在bin目錄下執行kibana.bat,kibana對es來講 真的是一個神器,數據庫

能夠在上面操做dsl  作數據分析等待  默認地址是http://localhost:5601c#

 

而後就是安裝ik了,ik是中文分詞插件,github地址是:https://github.com/medcl/elasticsearch-analysis-ik

從releases下載 我下載的最新版 6.4.2 下載後複製到es的plugins 目錄下,解壓就好了。而後去kibana檢查是否安裝成功,具體操做見github 

ik分詞策略有ik_max_word 和 ik_smart   ik_max_word會將文本作最細粒度的拆分,例如「中華人民共和國國歌」會被拆分爲「中華人民共和國、中華人民、中華、華人、人民共和國、人民、人、民、共和國、共和、和、國國、國歌」,會窮盡各類可能的組合;

ik_smart會將文本作最粗粒度的拆分,例如「中華人民共和國國歌」會被拆分爲「中華人民共和國、國歌」;

 

ik安裝後以後 就是在kibana建立index  和mapping了

es和咱們經常使用的sqlserver等關係型數據庫對好比下:

DB:DataBases=>Tables=>Rows=>Columns

ES:Indices=>Types=>Documents=>Fields

建立Index

在kibana Dev Tools 操做dsl    

 

PUT /cnblogdb  (注意 必須爲小寫)
POST
/cnblogdb/articles/_mapping { "properties": { "content": { "type": "text", "analyzer": "ik_smart", "search_analyzer": "ik_smart" }, "title":{ "type":"text", "analyzer": "ik_smart", "search_analyzer": "ik_smart" }, "summary":{ "type":"text", "analyzer": "ik_smart", "search_analyzer": "ik_smart" }, "author":{ "type":"text", "analyzer": "ik_smart", "search_analyzer": "ik_smart", "fielddata": true, "fields": { "raw":{ "type":"keyword" } } } } }

 

能夠看到 在_mapping 的時候 author字段 加了fielddata 屬性  和fields   

關於fielddata 詳細介紹可移步 https://www.elastic.co/guide/cn/elasticsearch/guide/current/preload-fielddata.html

在這裏設置fielddata爲true是由於 後續的根據author字段進行聚合檢索 es在默認狀況下對text類型的字段是不可聚合的

 

設置 fields :{raw:{type:keyword }} 是由於咱們在對author字段進行聚合的時候,由於上面的ik分詞策略,因此咱們聚合到的結果是分詞後的結果,

好比author爲 張教主  聚合結果就成了張,教主 這樣的結果,設置他就相似有了個別名。

 

 

c#中操做es 使用Nest 

github地址是 https://github.com/elastic/elasticsearch-net

 數據源地址是:  http://zycoder.oss-cn-qingdao.aliyuncs.com/ali/blog.sql

這個數據是sqlserver的腳本數據 整到es也是很簡單的

建立esclient   es多見於分佈式  多節點 咱們搞着學習就沒必要要了

 

var node = new Uri("http://localshot:9200");
var settings = new ConnectionSettings(node);
var client = new ElasticClient(settings);

看項目 界面截圖 就是一個簡單的多字段匹配檢索 和 聚合 

建立Model 此model是與type相對應的

[ElasticsearchType(Name ="articles")]  
    public partial class Articles
    {
        public int Id { get; set; }

        [Text(Analyzer = "ik_smart")]  
        public string Title { get; set; }

        public string ItemUrl { get; set; }
        [Text(Analyzer = "ik_smart")]
        public string Sumary { get; set; }
        [Text(Analyzer = "ik_smart", Fielddata = true)]
        public string Author { get; set; }

        public string PubDate { get; set; }
        [Text(Analyzer = "ik_smart")]
        public string Content { get; set; }
    }

首先就是首頁的高亮檢索了  代碼以下:

       public ActionResult GetArticles()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            string keyWords = Request.Params["keyWords"];
            string author = Request.Params["author"];
            int.TryParse(Request.Params["page"], out int page);
            page = page <= 1 ? 1 : page;
            int start = (page - 1) * 10;

            var query = new SearchDescriptor<Articles>();
            if (!string.IsNullOrWhiteSpace(author))
            {
                query= query.Query(q => q.Match(m => m.Field("author").Query(author)));
            }
            else
            {
                query = query.Query(q => q.MultiMatch(m => m.Fields(
                        fd => fd.Fields("title", "sumary", "author")
                        ).Query(keyWords)
                        ));
            }
            query = query.Highlight(h => h
               .PreTags(@"<span style='color:red'>")
                  .PostTags("</span>")
                  .Fields(
                      f => f.Field(obj => obj.Title),
                      f => f.Field(obj => obj.Sumary),
                      f => f.Field(obj => obj.Author)
                   )
            ).Sort(c => c.Field("_score", SortOrder.Descending).Field("id", SortOrder.Descending))
                         .From(start).Size(10);
var response = _client.Search<Articles>(query); var list = response.Hits.Select(c => new Articles { Id = c.Source.Id, Title = c.Highlights == null ? c.Source.Title : c.Highlights.Keys.Contains("title") ? string.Join("", c.Highlights["title"].Highlights) : c.Source.Title, Author = c.Highlights == null ? c.Source.Author : c.Highlights.Keys.Contains("author") ? string.Join("", c.Highlights["author"].Highlights) : c.Source.Author, Sumary = c.Highlights == null ? c.Source.Sumary : c.Highlights.Keys.Contains("sumary") ? string.Join("", c.Highlights["sumary"].Highlights) : c.Source.Sumary, PubDate = c.Source.PubDate }); sw.Stop(); ViewBag.Times = sw.ElapsedMilliseconds; ViewBag.PageIndx = page; ViewData["list"] = list.ToList(); return View(); }

Sort(c => c.Field("_score", SortOrder.Descending).Field("id", SortOrder.Descending)) 這裏咱們能夠多留意一下,在匹配搜索的時候,
默認排序是根據匹配得分進行排序的,因此咱們想要獲取最新最匹配的數據,首先就是根據匹配得分進行排序 在根據時間

面板結果以下:

 

Nest進行搜索 語法不作過多討論 谷歌 百度

而後就是根據author進行聚合 相似數據庫語法就是 select author,count(author) from article group by author 

dsl 結果以下所示:

size就是最靠前的10位了  小魚兒同志貢獻最多  我所提供的數據源裏有56篇文章~

代碼以下:

     public ActionResult HomeRight()
        {
            var response= _client.Search<Articles>(s => s.Aggregations(aggs => aggs.Terms(
                  "aggs", t => t.Field("author.raw").Size(20).CollectMode(TermsAggregationCollectMode.BreadthFirst)
                  )).Size(0));
            var buckets= response.Aggregations.Terms("aggs").Buckets;
            var authorGroups= buckets.Select(q => new AuthorGroup
            {
                AuthorName = q.Key,
                Count = (int)q.DocCount
            }).ToList();
            ViewData["list"] = authorGroups;
            return View();
        }

在c#中 咱們就是把dsl 改成lambda去查詢 

在聚合的時候 最後 Size(0); 不是取0條數據 而是在聚合搜索的時候 默認也會獲取documents 默認爲10條 可是咱們只是聚合並不須要搜索文檔 因此就設置爲0 

也減少了內存開銷,增長查詢速度。

更多資料就是看官方文檔了,提供的很全面。

Share End!

相關文章
相關標籤/搜索