文档: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. SGD训练时收敛速度的变化研究。

    一个典型的SGD过程中,一个epoch内的一批样本的平均梯度与梯度方差,在下图中得到了展示. 无论什么样的网络结构,无论是哪一层网络的梯度,大体上都遵循下面这样的规律: 高信号/噪音比一段时间之后,信 ...

  2. CoUninitialize引发的一个错误

    程序开始已使用CoUninitialize,后边的函数又再次调用CoUninitialize,判断返回值,导致函数提前退出返回异常.

  3. 1003 Emergency Dijkstra

    这题做的心很累,我用的还是 1018的思路做的,但是 使用dfs 求最大人数对于某些有问题(现在也不知道错哪了), 看了别人的代码后才发现,其实完全不用这么麻烦,只需设置一个点的权重,一遍DJ(自创简 ...

  4. jsp(web作业)

    一:jsp简介 SP即Java Server Pages,它和servlet技术一样,都是sun公司定义的一种用于开发动态web资源的技术.该技术的最大特点在于:写JSP就像写html,但它相比htm ...

  5. Anfora 自动装载类

    1.目录结构 + Anfora | + Autoload | - ClassLoader.php | - Autoload.php - autoload.php 2.注册类加载器 src/Anfora ...

  6. Windows服务器开发和Linux服务器软件开发的区别

    大型网页游戏服务器开发(Windows+Linux) 进程监听 负载均衡 数据库分发 MapGIS IGServer(Java C++)

  7. Note of Python Math

    Note of Python Math math 库是Python 提供的内置数学类函数库,而其中复数类型常用于科学计算,一般计算并不常用,因此math 库不支持复数类型.math 库一共提供4个数学 ...

  8. win7 ssh linux虚拟机(ubuntu12.04)

    环境: 1. 管理vmware Workstation8.0 2. Ubuntu 12.04.iso安装文件 3.Ssh登录软件putty 步骤 1.安装,安装linux系统时,在“硬件”里设置“网络 ...

  9. Windows + Apache + WSGI 部署Django

    注意Python Apache和mod_wagi的版本要一致哦 1.安装Apache服务器(下载后,解压即可,目录不能有中文) 2.安装mod_wsgi (pip install 它的路径) 3.打开 ...

  10. android 界面设计

    wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetric ...