码迷,mamicode.com
首页 > 其他好文 > 详细

ElasticSearch(站内搜索)

时间:2016-10-04 01:23:45      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:

  1. 下载Elasticsearch,地址:elasticsearch.org/download 
  2. 下载jdk,百度搜索jdk下载即可
  3. 配置JAVA_HOME变量,配置方法在此文:http://jingyan.baidu.com/article/9113f81b0ceae22b3214c7e1.html
  4. 解压elasticsearch,打开cmd命令窗口,定位到bin目录,然后执行elasticsearch.bat 即可。
  5. 最后在浏览器中打开:http://localhost:9200/?pretty 效果如下:
  6. 插件head安装方法
  7. 首先下载head.zip 地址:https://github.com/mobz/elasticsearch-head
  8. 然后在elasticsearch安装目录下的plugins文件夹下建立head文件夹
  9. 将下载后的head文件copy到head文件夹下
  10. 运行es,在浏览器中打开:http://localhost:9200/_plugin/head/ 出现这个界面就可以。

使用:

1、Nuget下载安装 plainelastic.net 第三方类库;

2、编写ElasticSearchHelper类:

using PlainElastic.Net;
using PlainElastic.Net.Queries;
using PlainElastic.Net.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BookMgr
{
    public class ElasticSearchHelper
    {
        public static readonly ElasticSearchHelper Intance = new ElasticSearchHelper();

        private ElasticConnection Client;

        private ElasticSearchHelper()
        {
            Client = new ElasticConnection("192.168.1.8", 9200);
        }

        /// <summary>
        /// 数据索引
        /// </summary>       
        /// <param name="indexName">索引名称</param>
        /// <param name="indexType">索引类型</param>
        /// <param name="id">索引文档id,不能重复,如果重复则覆盖原先的</param>
        /// <param name="jsonDocument">要索引的文档,json格式</param>
        /// <returns>索引结果</returns>
        public IndexResult Index(string indexName, string indexType, string id, string jsonDocument)
        {

            var serializer = new JsonNetSerializer();

            string cmd = new IndexCommand(indexName, indexType, id);

            OperationResult result = Client.Put(cmd, jsonDocument);

            var indexResult = serializer.ToIndexResult(result.Result);

            return indexResult;
        }

        /// <summary>
        /// 数据索引
        /// </summary>       
        /// <param name="indexName">索引名称</param>
        /// <param name="indexType">索引类型</param>
        /// <param name="id">索引文档id,不能重复,如果重复则覆盖原先的</param>
        /// <param name="jsonDocument">要索引的文档,object格式</param>
        /// <returns>索引结果</returns>
        public IndexResult Index(string indexName, string indexType, string id, object document)
        {
            var serializer = new JsonNetSerializer();

            var jsonDocument = serializer.Serialize(document);

            return Index(indexName, indexType, id, jsonDocument);
        }

        /// <summary>
        /// 全文检索
        /// </summary>
        /// <typeparam name="T">搜索类型</typeparam>
        /// <param name="indexName">索引名称</param>
        /// <param name="indexType">索引类型</param>
        /// <param name="query">查询条件(单个字段或者多字段或关系)</param>
        /// <param name="from">当前页(0为第一页)</param>
        /// <param name="size">页大小</param>
        /// <returns>搜索结果</returns>
        public SearchResult<T> Search<T>(string indexName, string indexType, QueryBuilder<T> query, int from, int size)
        {
            var queryString = query.From(from).Size(size).Build();

            var cmd = new SearchCommand(indexName, indexType);

            var result = Client.Post(cmd, queryString);

            var serializer = new JsonNetSerializer();

            return serializer.ToSearchResult<T>(result);
        }
    }
}

3、MVC中调用ElasticSearchHelper类:

        [HttpPost]
        public ActionResult BMsg(Book DataModel)
        {
            DataModel.BookWriteTime = DateTime.Now;
            DBBookMgr.BooksOpeart(DataModel, OpeartOption.NEW);

            var result = ElasticSearchHelper.Intance.Index("db_book", "searchbook", DataModel.Id.ToString(), DataModel);       

            return View(DataModel);
        }

看下效果图:使用框架(MVC5+Dapper+ElasticSearch)

技术分享

4、参考博客:
ElasticSearch的安装:http://www.cnblogs.com/panzi/p/5659697.html

ElasticSearch系列学习: http://www.cnblogs.com/eggTwo/p/4039779.html

ElasticSearch使用:http://blog.csdn.net/wulex/article/details/52144145

 演示代码下载

 

5、未完待续内容:
ElasticSearch中文分词查询的处理

 

ElasticSearch(站内搜索)

标签:

原文地址:http://www.cnblogs.com/xibei666/p/5929970.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!