Getting Started with Elastic Search in .NET
I have been working on many application during my career. Many if not all had some searching capabilities. The more complex the search got, the harder it was to control its performance and impact on database transactions. If you also would like to support full text search, your problems become larger. We could use Full text search capabilities in Oracle or SQL Server, but we would need to setup a separate instance if we want to limit impact on the transactions yet again. Or we can pick another solution, better suited to solve the problem at hand. This is where Elastic Search comes in.
It is a web services layer setup on top of Lucene, a search engine written in Java. As a result, we will need to have Java Runtime installed on all machine where we are going to install Elastic Search, including developer machines. You can download it from oracle web site, and it is free. http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html. I would recommend making sure you have 64 bit installed.
You can download Elastic Search at https://www.elastic.co/downloads/elasticsearch. Let’s start by doing so. Once you have the zip file, we will install windows service that hosts Elastic Search on windows. This is the easiest way to run Elastic on Windows. Once you unzip the download, switch to bin folder using command window. Then run service.bat to install windows service. Just type the following
service install
If you get the following message: JAVA_HOME environment variable must be set. We need to setup this environment variable. Go to properties of “My Computer”. Then select advanced system settings, then environment variables. Add new system variable called JAVA_HOME. It’s value should be something similar to “C:Program FilesJavajre1.8.0_74”. Then run service install again. You should see something like the following. if so, you are done with step 1.
Next step is to start new .NET project. I would pick class library to house our search code in. We will test it using tests project.
So, start new class library project. I’ll call mine Search.Library. When we integrate Elastic with .NET, we should use .NET client library. I use NEST, which is the best one in my opinion. To install it use Nuget. I would switch to NuGet package manager console window and type
install-package NEST
or you can use package manager window. NEST will install ElasticSearch.Net package, one of its dependencies and JSON.NET. We could then technically speaking start testing, but we need to take a few more steps in advance.
I have been using Elastic Search for a while. In theory, you can use schema-less approach with it. However, in practice, this does not work really well. Schema has many advantages. We can be very precise, especially for nullable data, when Elastic does not really know how to index the data. So, if would apply default full text search approaches to all the data. This may or may not be what we want.
In an example let’s define a class that we will use for samples. Let’s call it Location, corresponding to a city.
Because we need to support geographic location, we defined a separate location type.
We need to configure the mappings in Elastic. We may want to use free handed search on all fields, which in Elastic Search will be referred to as analyzed field. Analyzed fields are broken into words then indexed for speedy word based search. Say in the case of area codes we want to search similarly to LIKE ‘%%’ in SQL Server though. Any such fields we need to flag as “not analyzed”. In addition we may want to use custom analyzer to account for case sensitive search on not-analyzed fields. We also want to use the same approach for all the fields that we want to use exact match on. We should also think about primary keys. In this case we want to flag CityId as id field in elastic search. I feel that thinking about your mappings and queries upfront will save you some headaches down the road.
In the mapping creation above _client is an instance of ElasticClient. Then we run through the property of our type, Location, and setup up each property. In case of zip we set it up fot wild card search. The reset of string properties are setup for stadard word base indexed search. Finally, I setup location as type GeoPoint for spatial search. We are going to run through the code in unit tests to make sure our mappings work Ok.
If we want to look at our mappings, we can easily do this in Chrome. Go to extensions and search for “Sense”. This will install Elastic Search plugin. You can click on the plugin after that, and you will something similar to the following.
To look at the mappings, just type get _mapping and hit green arrow.
Our mappings look as follows.
We will discuss queries in subsequent posts. You can download current project here.
Getting Started with Elastic Search in .NET的更多相关文章
- elastic search查询命令集合
Technorati 标签: elastic search,query,commands 基本查询:最简单的查询方式 query:{"term":{"title" ...
- elastic search 学习笔记
Elastic search在数据分析的应用中相当于一个数据库的搜索引擎. 跟MySQL类似,它有自己的查询语言,只不过不是关系型数据库,属于NoSQL. 可以根据索引从分布式服务器文件系统中快速存取 ...
- elastic search 学习 一
初步阅读了elastic search 的文档,并使用command实践操作. 大概明白其概念模型.
- 分库分表后跨分片查询与Elastic Search
携程酒店订单Elastic Search实战:http://www.lvesu.com/blog/main/cms-610.html 为什么分库分表后不建议跨分片查询:https://www.jian ...
- 自学elastic search
工作也有一段时间了,虽然来这个公司之后学会了几门不同的语言,但想拨尖还是任重道远. 想往高级程序员甚至是架构师方向发展.他仍然是我的学习对象.我现在做着的,无非是他玩剩下的罢了. luncene之前有 ...
- Elastic Search 上市了,市值翻倍,这群人财务自由了!
国庆长假,大部分人还深浸在风花雪月之中,而就在昨天(美国时间10月5号),我们 Java 程序员所熟知的大名鼎鼎的 Elastic Search 居然在美国纽约证券交易所上市了! 当说到搜索时,大部分 ...
- Elastic Search 安装和配置
目标 部署一个单节点的ElasticSearch集群 依赖 java环境 $java -version java version "1.8.0_161" Java(TM) SE R ...
- [elastic search][redis] 初试 ElasticSearch / redis
现有项目组,工作需要. http://www.cnblogs.com/xing901022/p/4704319.html Elastic Search权威指南(中文版) https://es.xiao ...
- elastic search文档详解
在elastic search中文档(document)类似于关系型数据库里的记录(record),类型(type)类似于表(table),索引(index)类似于库(database). 文档一定有 ...
- elastic search 查询
eelastic search主要有两种查询方式,一种是查询字符串,一种是请求体(json格式)查询. 查询字符串: 查询字符串的功能相对简单,使用容易. 比如GET http://localhost ...
随机推荐
- 如何使用find命令在Linux中查找文件
Linux Find命令是Linux系统管理员工具库中最强大的工具之一. Find是一个命令行实用程序,它允许您根据用户给定的表达式搜索目录层次结构中的文件和目录,并对每个匹配的文件应用用户指定的操作 ...
- LevelDb日知录之五:MemTable详解
[LevelDb日知录之五:MemTable详解] LevelDb日知录前述小节大致讲述了磁盘文件相关的重要静态结构,本小节讲述内存中的数据结构Memtable,Memtable在整个体系中的重要地位 ...
- UNITY录制视屏解决方案 - ShareREC For Unity3D
注意buildSetting里面,AutoGraphic改成opengl es2,否则魅蓝手机上容易出现1/4屏 一.导入项目 1.到Mob官网下载ShareREC For Unity3D 的SDK包 ...
- 单源最短路:Bellman-Ford算法 及 证明
描述: 求图中某一点到其他任一点的最短距离. 操作: 1. 初始化 结果保存在一个dist数组里,源点的结果初始化为0,其他初始化为无穷大(如INT32_MAX). 2. 计算: 两重for循环,第一 ...
- LoadRunner--Analysis各项指标详解
转载 https://blog.csdn.net/liangfengchang/article/details/45070321 一.常用到的性能测试术语 1.事务(Transaction) 在web ...
- Excel中保留有效数字的问题
在工作表界面中按 <alt>+<F11>,进入代码页面,然后再 WORKBOOK中插入模块,把以下代码COPY入模块中.就可以在工作表中使用 =YXSZ(数值,保留位数). 如 ...
- UVa 11136 Hoax or what (STL)
题意:有 n 天,每天有m个数,开始的前一天没有数据,然后每天从这个里面拿出一个最大的和最小的,求 n 天的最大的和最小的差值相加. 析:一看就知道用set啊,多简单的STL,不过要注意,开long ...
- 团体程序设计天梯赛L1-019 谁先倒 2017-03-22 17:35 33人阅读 评论(0) 收藏
L1-019. 谁先倒 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳 ...
- Codeforces 768A Oath of the Night's Watch 2017-02-21 22:13 39人阅读 评论(0) 收藏
A. Oath of the Night's Watch time limit per test 2 seconds memory limit per test 256 megabytes input ...
- DRBD+Heartbeat实现自动切换
1>HeartBeat介绍 Heartbeat 项目是 Linux-HA 工程的一个组成部分,它实现了一个高可用集群系统.心跳服务和集群通信是高可用集群的两个关键组件,在 Heartbeat 项 ...