标签:
使用:
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中文分词查询的处理
标签:
原文地址:http://www.cnblogs.com/xibei666/p/5929970.html