文档: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. git学习笔记:常用命令总结

    本文根据廖雪峰的博客,记录下自己的学习笔记.主要记录常用的命令,包括仓库初始化.添加文件.提交修改.新建分支.内容暂存.分支管理.标签管理等内容. git是分布式版本控制系统. 首先是安装,从官网下载 ...

  2. github菜鸟入门

    github菜鸟入门教程 闲来无事,研究了下github的玩法,完毕总结:简单好玩,上档次! 一.首先注册github的账号 二.下载安装git 三.新建仓库 1.点击右上方的+号选择首项新建仓库 2 ...

  3. angularJs 2-quickstart学习记录

    angular官网文档 我尝试文档中下载<快速起步>种子的方法进行quickstart. cd quickstart npm install npm start npm install 这 ...

  4. 在struts.xml中配置默认action遇到的问题

    初始代码: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC & ...

  5. JavaScript深拷贝

    1,JSON.parse(JSON.stringify(obj)) 使用JSON实现深拷贝必须要求对象是符合JSON安全的,不了解JSON安全的自行百度. 2,lodash/underscore  _ ...

  6. php 批量下载文件

    public function batchDownload() { $filename = 'tmp.zip'; $zipName = date('YmdHi') . '.zip'; $files = ...

  7. idea类里面编译报错,快速定位快捷键设置

    settings---->keyMap------->Main menu----------->在搜索框里输入error,找到Next Highlighted Error 和Prev ...

  8. DOTween的基本用法

    首先声明一点,不要简单的认为 DOTween 只能用在 Transform 组件上完成一些简单的动画,或者是完成一些 UI 动画,DOTween 的用途是很广的,unity中有很多组件都可以使用 DO ...

  9. switch_root vs pivot_root vs chroot【转】

    1. pivot_root can/should be used together with chroot pivot_root new_root put_old pivot_root moves t ...

  10. ExtJS中获取选中行的数据

    listeners: { select:function(rowModel,record){ var data = rowModel.getLastSelected(); console.log(&q ...