前言
Elastic Search是基於Lucene這個非常成熟的索引方案,另加上一些分布式的實現:集群,sharding,replication等。具體可以參考我同事寫的文章。
本文主要介紹ES入門,包括最簡單的操作和用C#代碼操作ES。ES本身有很多復雜的功能,本文只是一個入門。
安裝並啟動ES
去https://www.elastic.co/
下載zip文件,解壓縮到本地硬盤。實現需要安裝java環境。
雙擊elasticsearch.bat,啟動ES。
打開瀏覽器,如果有類似如下輸出,則啟動成功。
{
"name" : "Gorgeous George",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "2.3.5",
"build_hash" : "90f439ff60a3c0f497f91663701e64ccd01edbb4",
"build_timestamp" : "2016-07-27T10:36:52Z",
"build_snapshot" : false,
"lucene_version" : "5.5.0"
},
"tagline" : "You Know, for Search"
}
通過postman操作
postman是chrome的一個插件,可以作為http client模擬操作。
創建index
命令:
PUT http://localhost:9200/chzhao-index
返回
{
"acknowledged": true
}
存入數據
PUT http://localhost:9200/chzhao-index/employee/1
body內容
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
返回
{
"_index": "chzhao-index",
"_type": "employee",
"_id": "1",
"_version": 2,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
查詢數據
全部查詢
查詢全部數據
命令
http://localhost:9200/chzhao-index/employee/_search
返回
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "chzhao-index",
"_type": "employee",
"_id": "1",
"_score": 1,
"_source": {
"first_name": "chunhui",
"last_name": "zhao",
"age": 31,
"about": "i love keke",
"interests": [
"go",
"coding"
]
}
}
]
}
}
根據某一個字段查詢
輸入
http://localhost:9200/chzhao-index/employee/_search?q=last_name:Zhao
返回
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
"_index": "chzhao-index",
"_type": "employee",
"_id": "1",
"_score": 0.30685282,
"_source": {
"first_name": "chunhui",
"last_name": "zhao",
"age": 31,
"about": "i love keke",
"interests": [
"go",
"coding"
]
}
}
]
}
}
使用DSL語句查詢
輸入
POST http://localhost:9200/chzhao-index/employee/_search
參數
參數要放在body里面。因為這樣,不能用put方法。
{
"query" : {
"match" : {
"last_name" : "Zhao"
}
}
}
返回
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
"_index": "chzhao-index",
"_type": "employee",
"_id": "1",
"_score": 0.30685282,
"_source": {
"first_name": "chunhui",
"last_name": "zhao",
"age": 31,
"about": "i love keke",
"interests": [
"go",
"coding"
]
}
}
]
}
}
c#代碼
c#代碼應用了NEST等包,具體如下。
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Elastic" version="1.0.3.0" targetFramework="net45" />
<package id="Elasticsearch.Net" version="2.4.3" targetFramework="net45" />
<package id="NEST" version="2.4.3" targetFramework="net45" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
</packages>
核心代碼
namespace ElasticSearchDemo
{
public class Person
{
public string Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string[] Chains { get; set; }
public string Content { get; set; }
}
}
using Nest;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace ElasticSearchDemo
{
class ESTool
{
private ElasticClient esClient = null;
private string index = "test-index";
public ESTool()
{
var node = new Uri("http://localhost:9200");
ConnectionSettings settings = new ConnectionSettings(node);
settings.DefaultIndex(index);
this.esClient = new ElasticClient(settings);
}
public void Save()
{
IEnumerable<Person> persons = new List<Person>
{
new Person()
{
Id = "4",
Firstname = "chunhui",
Lastname = "zhao",
Chains = new string[]{ "a","b","c" },
Content = "dad"
},
new Person()
{
Id = "5",
Firstname = "keke",
Lastname = "zhao",
Chains = new string[]{ "x","y","z" },
Content = "daughter"
}
};
this.esClient.IndexMany<Person>(persons, index);
}
public void Search()
{
var rs = this.esClient.Search<Person>(s => s.Index(index));
Console.WriteLine(JsonConvert.SerializeObject(rs.Documents));
}
}
}