前言

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)); }
}
}

参考

Elasticsearch 权威指南(中文版)

Elastic Search操作入门的更多相关文章

  1. 学习ELk之----02. Elastic Search操作入门

    我们将使用Postman来进行日志写入操作.Postman的下载地址,你可以Google一下. 1. 在上一节中,我们启动完成ELK的Docker后,可以在浏览器中打开:http://192.168. ...

  2. Elastic Search快速入门

    https://blog.csdn.net/weixin_42633131/article/details/82902812 通过这个篇文章可以快速入门,快速搭建一个elastic search de ...

  3. Elastic Search中Document的CRUD操作

    一. 新增Document在索引中增加文档.在index中增加document.ES有自动识别机制.如果增加的document对应的index不存在.自动创建,如果index存在,type不存在自动创 ...

  4. elastic search book [ ElasticSearch book es book]

    谁在使用ELK 维基百科, github都使用 ELK (ElasticSearch es book) ElasticSearch入门 Elasticsearch入门,这一篇就够了==>http ...

  5. elastic search(以下简称es)

    参考博客园https://www.cnblogs.com/Neeo/p/10304892.html#more 如何学好elasticsearch 除了万能的百度和Google 之外,我们还有一些其他的 ...

  6. elastic search 学习笔记

    Elastic search在数据分析的应用中相当于一个数据库的搜索引擎. 跟MySQL类似,它有自己的查询语言,只不过不是关系型数据库,属于NoSQL. 可以根据索引从分布式服务器文件系统中快速存取 ...

  7. elastic search 学习 一

    初步阅读了elastic search 的文档,并使用command实践操作. 大概明白其概念模型.

  8. tpot从elastic search拉攻击数据之一 找本地数据端口

    前面,我们已经在ubuntu服务器上部署好了tpot,并启动进行数据捕获 可以通过64297端口登陆到kibana可视化平台查看捕获到攻击的情况. 现在要拉取攻击数据了,但是该怎么拉呢? 看了一上午的 ...

  9. 深入分析Elastic Search的写入过程

    摘要 之前写过一篇ElasticSearch初识之吐槽,不知觉竟然过去了两年了.哎,时光催人老啊.最近又用到了ES,想找找过去的总结文档,居然只有一篇,搞了半年的ES,遇到那么多的问题,产出只有这么点 ...

随机推荐

  1. [贪心经典算法]Kruskal算法

    Kruskal算法的高效实现需要一种称作并查集的结构.我们在这里不介绍并查集,只介绍Kruskal算法的基本思想和证明,实现留在以后讨论. Kruskal算法的过程: (1) 将全部边按照权值由小到大 ...

  2. Kafka Streams演示程序

    本文从以下六个方面详细介绍Kafka Streams的演示程序: Step 1: 下载代码 Step 2: 启动kafka服务 Step 3: 准备输入topic并启动Kafka生产者 Step 4: ...

  3. hibernate.cfg.xml的详细解释

    <!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式-->                    < ...

  4. ubuntu 只有客人会话登录(第一次深刻感受文件权限的威力 )

    为了测试docker的挂载权限,把宿主机的/etc/passwd文件挂载到了虚机当中,进入虚机想看下能不能直接对我宿主机上的文件进行操作,把/etc/passwd删掉了最后十行...结果宿主机上的/e ...

  5. ADO.NET中DataSet、DataTable、DataRow的数据复制方法

    DataSet 对象是支持 ADO.NET的断开式.分布式数据方案的核心对象 ,用途非常广泛.我们很多时候需要使用其中的数据,比如取得一个DataTable的数据或者复制另一个DataTabe中的数据 ...

  6. 【bzoj1026】[SCOI2009]windy数 数位dp

    题目描述 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? 输入 包含两个整数 ...

  7. 使用canvas控制gif图片的播放与暂停

    if ('getContext' in document.createElement('canvas')) { HTMLImageElement.prototype.play = function() ...

  8. 【刷题】BZOJ 3513 [MUTC2013]idiots

    Description 给定n个长度分别为a_i的木棒,问随机选择3个木棒能够拼成三角形的概率. Input 第一行T(T<=100),表示数据组数. 接下来若干行描述T组数据,每组数据第一行是 ...

  9. bzoj3489: A simple rmq problem (主席树)

    //========================== 蒟蒻Macaulish:http://www.cnblogs.com/Macaulish/  转载要声明! //=============== ...

  10. Codeforces Round #469 (Div. 2) E. Data Center Maintenance

    tarjan 题意: 有n个数据维护中心,每个在h小时中需要1个小时维护,有m个雇主,他们的中心分别为c1,c2,要求这两个数据中心不能同时维护. 现在要挑出一个数据中心的子集,把他们的维护时间都推后 ...