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!

相关文章
相关标签/搜索