文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

客户端:https://www.elastic.co/guide/en/elasticsearch/client/index.html

API:https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-from-size.html

.net

1.连接,项目中引用Nest包

var settings = new ConnectionSettings(new Uri("http://example.com:9200"))
.DefaultIndex("people"); var client = new ElasticClient(settings);

2.索引

public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
var person = new Person
{
Id = ,
FirstName = "Martijn",
LastName = "Laarman"
}; var indexResponse = client.IndexDocument(person); var asyncIndexResponse = await client.IndexDocumentAsync(person);

3.查询

var searchResponse = client.Search<Person>(s => s
.From()
.Size()
.Query(q => q
.Match(m => m
.Field(f => f.FirstName)
.Query("Martijn")
)
)
); var people = searchResponse.Documents;

4.删除

var person = new Person
{
Id =
};
var deleteResponse= client.Delete<Person>(dept);

在kibana中操作

查看

删除

添加一个新的Index

public class StudentQuery
{
public int StudentId { get; set; }
public List<string> Items { get; set; }
}
var settings = new ConnectionSettings(new Uri("http://10.15.4.155:9200/")).DefaultIndex("student");
var client = new ElasticClient(settings);
for (int i = ; i < ; i++)
{
StudentQuery student = new StudentQuery();
student.StudentId = i;
student.Items = new List<string>();
student.Items.Add("Code"+i);
student.Items.Add("Name"+i);
student.Items.Add("CardId"+i);
student.Items.Add("IdentityId"+i);
var indexResponse = client.IndexDocument(student);
}

查询

var searchResponse = client.Search<StudentQuery>(s => s.
Query(q=>q.
Match(m=>m.
Field(f=>f.Items)
.Query("CardId1")
)
)
);

在kibana中查询

GET /student/studentquery/_search/
{
"query": {
"match": {
"items": {
"query": "Code8"
}
}
}
}

对同一字段多个值查询

或者使用term

GET /_search
{
"from" : 0, "size" : 10,
"query" : {
"term" : { "user" : "kimchy" }
}
}

这里要注意match和term的区别

Why doesn’t the term query match my document?

简单来说就是string类型的字段在index之前会已fulltext的方式被es进行默认分词,比如放入“基金交易”,用term来查“基金”可能就查不到,用match就可以。

Java

@Service
public class ElasticService {
@Value("${elastic.host}")
private String host;
@Value("${elastic.port}")
private int port; private RestHighLevelClient client; public void index(String index, List<MetisQuestion> list) throws Exception {
client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(host, port, "http")));
for(MetisQuestion o :list){
IndexRequest request = new IndexRequest(index,"doc");
ObjectMapper mapper=new ObjectMapper();
String json=mapper.writeValueAsString(o);
request.source(json, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
} public void search(String index,String key){
client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(host, port, "http")));
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.types("doc");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("question", key));
searchRequest.source(sourceBuilder);
getResponse(searchRequest);
} public void search(String index,String key,String... values){
client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(host, port, "http")));
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.types("doc");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termsQuery(key,values));
sourceBuilder.size(5);
searchRequest.source(sourceBuilder);
getResponse(searchRequest);
} private void getResponse(SearchRequest searchRequest) {
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
ObjectMapper mapper=new ObjectMapper();
MetisQuestion question=mapper.readValue(sourceAsString,MetisQuestion.class);
System.out.println(question.getQuestion());
}
} catch (IOException e) {
e.printStackTrace();
}
} }

elasticsearch 操作的更多相关文章

  1. Elasticsearch操作索引

    目录 操作索引 1. 基本概念 2. 创建索引 2.1 语法 2.2查看索引设置 2.3.删除索引 2.4 映射配置 2.5 新增数据 2.6 修改数据 2.7 删除数据 3. 查询 3.1 基本查询 ...

  2. Elasticsearch操作Document文档

    1.利用客户端操作Document文档数据        1.1 创建一个文档(创建数据的过程,向表中去添加数据)            请求方式:Post    请求地址:es所在IP:9200/索 ...

  3. python实现elasticsearch操作-CRUD API

    python操作elasticsearch常用API 目录 目录 python操作elasticsearch常用API1.基础2.常见增删改操作创建更新删除3.查询操作查询拓展类实现es的CRUD操作 ...

  4. Elasticsearch系列(3):Elasticsearch操作入门

    创建Index 新建Index,可以直接向Elastic服务器发送PUT请求,比如下面的命令创建了一个名为:logdb的Index. [root@elsearchserver ~]# curl -X ...

  5. 3.使用Spring Data ElasticSearch操作ElasticSearch(5.6.8版本)

    1.引入maven坐标 <!--spring-data-elasticsearch--><dependency> <groupId>org.springframew ...

  6. Elasticsearch 2.3.3 JAVA api说明文档

    原文地址:https://www.blog-china.cn/template\documentHtml\1484101683485.html 翻译作者:@青山常在人不老 加入翻译:cdcnsuper ...

  7. elasticsearch代码片段,及工具类SearchEsUtil.java

    ElasticSearchClient.java package com.zbiti.framework.elasticsearch.utils; import java.util.Arrays; i ...

  8. 第三百六十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现搜索的自动补全功能

    第三百六十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—用Django实现搜索的自动补全功能 elasticsearch(搜索引擎)提供了自动补全接口 官方说明:https://www.e ...

  9. 第三百六十七节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)scrapy写入数据到elasticsearch中

    第三百六十七节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)scrapy写入数据到elasticsearch中 前面我们讲到的elasticsearch( ...

随机推荐

  1. 【转】【机器学习】R 正则化函数 scale

    源:http://blog.163.com/shen_960124/blog/static/60730984201582594011277/ 1. 数据的中心化 所谓数据的中心化是指数据集中的各项数据 ...

  2. maven阿里云仓库配置

    为了加速项目构建,在maven的settings.xml 文件里配置mirrors的子节点,添加如下mirror: <mirrors> <mirror> <id>a ...

  3. python将文本转化成语音并播放

    一.问题 在学习的过程中,我们会涉及到将文本信息,转化成语音的过程,比如:我爬取了一个小说的网站,我要将里面的内容进行语音处理.目前能够进行语音的方法还是很多,比如win32com,百度ai. 二.解 ...

  4. POJ1964-City Game

    给你N×M大的矩阵,里面分别有字符‘F'和’R',要找到一个最大的只有‘F'的矩阵,不能包含有’R‘.N,M<=1000. 一开始的思路是单调栈来求最大矩形面积,因为没看清题目不能包含’R'字符 ...

  5. 关于http与https的注意点

    背景:在一次项目生产上线中遇到地址在IOS版本的app中打不开或者接口请求不返回的情况,在安卓机和PC上表现正常,经排查,问题出在http请求上,原因详解 在早期PC上和安卓手机上比较不严格,在htt ...

  6. verilog HDL-参数型数据对像 与‘define

    参数新数据对象是用来定义常量的,它可以提升verilog hdl代码的可读性和维护性. verilog hdl支持参数有两种,普通参数和局部参数.普通参数在模块例化时可以从新赋值,局部参数在模块例化时 ...

  7. PHP+XML写注册登录

    2018/12/06  今天尝试了下PHP   使用PHP+XML写注册登录   具体保存于上传的文件中 从此山水不相逢  莫问他人长与短

  8. 没执行过 rm -rf /* 的开发不是好运维

    阅读本文大概需要 1 分钟. 打开终端,获取 root 权限,执行以下命令:rm -rf /*,会发生什么呢?估计只要接触过 Linux 的人,肯定没少听过它的故事,清楚之后会发生什么可怕的事情. 科 ...

  9. Python爬虫3-parse编码与利用parse模拟post请求

    GitHub代码练习地址:①利用parse模拟post请求:https://github.com/Neo-ML/PythonPractice/blob/master/SpiderPrac04_pars ...

  10. [Postman]捕获HTTP请求(14)

    如果您使用API​​构建客户端应用程序 - 移动应用程序,网站或桌面应用程序 - 您可能希望查看应用程序中发送和接收的实际HTTP请求流量.在某些情况下,您可能会发现甚至没有记录的API.Postma ...