Elastic Search操作入门
前言
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));
}
}
}
参考
Elastic Search操作入门的更多相关文章
- 学习ELk之----02. Elastic Search操作入门
我们将使用Postman来进行日志写入操作.Postman的下载地址,你可以Google一下. 1. 在上一节中,我们启动完成ELK的Docker后,可以在浏览器中打开:http://192.168. ...
- Elastic Search快速入门
https://blog.csdn.net/weixin_42633131/article/details/82902812 通过这个篇文章可以快速入门,快速搭建一个elastic search de ...
- Elastic Search中Document的CRUD操作
一. 新增Document在索引中增加文档.在index中增加document.ES有自动识别机制.如果增加的document对应的index不存在.自动创建,如果index存在,type不存在自动创 ...
- elastic search book [ ElasticSearch book es book]
谁在使用ELK 维基百科, github都使用 ELK (ElasticSearch es book) ElasticSearch入门 Elasticsearch入门,这一篇就够了==>http ...
- elastic search(以下简称es)
参考博客园https://www.cnblogs.com/Neeo/p/10304892.html#more 如何学好elasticsearch 除了万能的百度和Google 之外,我们还有一些其他的 ...
- elastic search 学习笔记
Elastic search在数据分析的应用中相当于一个数据库的搜索引擎. 跟MySQL类似,它有自己的查询语言,只不过不是关系型数据库,属于NoSQL. 可以根据索引从分布式服务器文件系统中快速存取 ...
- elastic search 学习 一
初步阅读了elastic search 的文档,并使用command实践操作. 大概明白其概念模型.
- tpot从elastic search拉攻击数据之一 找本地数据端口
前面,我们已经在ubuntu服务器上部署好了tpot,并启动进行数据捕获 可以通过64297端口登陆到kibana可视化平台查看捕获到攻击的情况. 现在要拉取攻击数据了,但是该怎么拉呢? 看了一上午的 ...
- 深入分析Elastic Search的写入过程
摘要 之前写过一篇ElasticSearch初识之吐槽,不知觉竟然过去了两年了.哎,时光催人老啊.最近又用到了ES,想找找过去的总结文档,居然只有一篇,搞了半年的ES,遇到那么多的问题,产出只有这么点 ...
随机推荐
- Struts2文件上传带进度条,虽然不是很完美
好久没有写东西,最近在做个项目,要用到文件h 传的,以前虽然也做上传,但是总觉得不好用 ,现在和队友合作做了一个带进度条的上传,觉得还行~~和大家分享一下. 首先说一下大概是这样实现的,在我们平时的上 ...
- HADOOP (十一).安装hbase
下载安装包并解压设置hbase环境变量配置hbase-site.xml启动hbase检测hbase启动情况测试hbase shell 下载安装包并解压 https://mirrors.tuna.tsi ...
- POJ 2986 A Triangle and a Circle(三角形和圆形求交)
Description Given one triangle and one circle in the plane. Your task is to calculate the common are ...
- rewrite or internal redirection cycle while processing nginx重定向报错
2018/05/07 15:03:42 [error] 762#0: *3 rewrite or internal redirection cycle while processing "/ ...
- Python练习—循环
1.输入n的值,求出n的阶乘. s=1 n = int(input("请输入一个数")) for i in range(1,n+1): s=s*i print(s) 2.折纸上月球 ...
- c# 中base64字符串和图片的相互转换
c#base64字符串转图片用到了bitmap类,封装 GDI+ 位图,此位图由图形图像及其特性的像素数据组成. Bitmap 是用于处理由像素数据定义的图像的对象. 具体bitmap类是什么可以自己 ...
- CentOS 7 开放防火墙端口
我:最近在使 CentOS 7时发现在本地不能访问linux上8080端口,以上是我的操作,修改后访问成功 CentOS 7 开放防火墙端口 命令 最近公司新的server要求用CentOS7, 发现 ...
- matlab读图函数
最基本的读图函数:imread imread函数的语法并不难,I=imread('D:\fyc-00_1-005.png');其中括号内写图片所在的完整路径(注意路径要用单引号括起来).I代表这个图片 ...
- Visual Studio 2013中使用Ribbon For WPF
1.首先需要 下载Ribbon For WPF.目前最新的版本是Microsoft Ribbon for WPF October 2010. 下载 链接: https://www.microsoft. ...
- CentOS 压缩(打包)和解压
1.tar命令 -c 创建压缩文件 -x 解开压缩文件 -t 查看压缩包内有哪些文件 -z 用 Gzip压缩或解压 -j 用 bzip2压缩或解压 -v 显示压缩或解压的过程 -f 目标文件名,在 f ...