ElasticSearchJava中使用ES
Posted LvhaoIT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ElasticSearchJava中使用ES相关的知识,希望对你有一定的参考价值。
Java内使用ES
文章目录
1、JAVA整合ElasticSearch
-
导入Java与ElasticSerach的依赖
<!--ES--> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.9.3</version> </dependency> <!--es客户端--> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.9.3</version> </dependency> <!--处理es日志--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.8.2</version> </dependency> <!--json转换--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.5</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.75</version> </dependency>
-
编写测试类,尝试获取ES连接
public class ESTestClient public static void main(String[] args) throws Exception //创建es客户端 RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200))); System.out.println(esClient); //关闭客户端连接 esClient.close();
2、JAVA内操作索引
2.1 创建索引
public class ESTestIndexCreate
public static void main(String[] args) throws Exception
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200)));
System.out.println(esClient);
//创建索引
CreateIndexRequest request = new CreateIndexRequest("user");
CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);
//拿到响应状态
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("索引操作: "+ acknowledged);
//关闭客户端连接
esClient.close();
2.2 获取索引信息
public class ESTestGetIndex
public static void main(String[] args) throws Exception
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200)));
System.out.println(esClient);
//创建索引连接
GetIndexRequest request = new GetIndexRequest("user");
GetIndexResponse getIndexResponse = esClient.indices().get(request, RequestOptions.DEFAULT);
System.out.println("索引查询: "+ getIndexResponse.getAliases());
System.out.println("索引查询: "+ getIndexResponse.getMappings());
System.out.println("索引查询: "+ getIndexResponse.getSettings());
//关闭客户端连接
esClient.close();
2.3 删除索引信息
public class ESTestIndexDelete
public static void main(String[] args) throws Exception
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200)));
System.out.println(esClient);
//删除索引
DeleteIndexRequest request = new DeleteIndexRequest("user2");
AcknowledgedResponse delete = esClient.indices().delete(request, RequestOptions.DEFAULT);
//拿到响应状态
boolean acknowledged = delete.isAcknowledged();
System.out.println("索引删除操作: "+ acknowledged);
//关闭客户端连接
esClient.close();
3、JAVA内增删改doc
创建一个实体类User,用来和es中user索引数据匹配
@Data
public class User
private String name;
private String sex;
private String tel;
private Integer age;
public static User createRandUser(int i)
User user = new User();
user.setTel("手机"+i);
user.setSex("性别"+i);
user.setName("zhangsan"+i);
user.setAge(30+i-1);
return user;
3.1 新增文档信息
public class ESTestDocInsert
public static void main(String[] args) throws Exception
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200)));
System.out.println(esClient);
IndexRequest request = new IndexRequest();
//方式1 这种方式会根据id 将数据进行新增 ,如果存在相同id,则会整体覆盖
//request.index("user").id("1004");
//不指定id的情况,es会自己生成一个随机串变成id
request.index("user");
//构建对象
User user = new User();
user.setName("zhangsan22");
user.setSex("男性");
user.setTel("1111");
//向es插入数据,必须将数据转成json格式
ObjectMapper mapper = new ObjectMapper();
String userJson = mapper.writeValueAsString(user);
request.source(userJson, XContentType.JSON);
IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
//关闭客户端连接
esClient.close();
3.2 批量新增文档信息
/**
* 根据id数组批量删除
*/
public class ESTestDocInsertBulk
public static void main(String[] args) throws Exception
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200)));
System.out.println(esClient);
//创建请求头
BulkRequest request = new BulkRequest("user");
List<String> idList = Arrays.asList("1,2,3,4".split(","));
int i = 1;
ObjectMapper mapper = new ObjectMapper();
for (String id : idList)
User user = User.createRandUser(i);
String userJson = mapper.writeValueAsString(user);
request.add(new IndexRequest().index("user").id(id).source(userJson, XContentType.JSON));
i++;
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());
System.out.println(response.getItems());
//关闭客户端连接
esClient.close();
3.3 根据id删除文档
/**
* 直接根据id删除
*/
public class ESTestDocDeleteById
public static void main(String[] args) throws Exception
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200)));
System.out.println(esClient);
//构建删除请求
DeleteRequest request = new DeleteRequest("user");
request.id("1003");
DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
//关闭客户端连接
esClient.close();
3.4 根据id批量删除
/**
* 根据id数组批量删除
*/
public class ESTestDocDeleteBulk
public static void main(String[] args) throws Exception
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200)));
System.out.println(esClient);
BulkRequest request = new BulkRequest("user");
List<String> idList = Arrays.asList("1,2,3,4".split(","));
for (String id : idList)
request.add(new DeleteRequest().index("user").id(id));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());
System.out.println(response.getItems());
//关闭客户端连接
esClient.close();
3.5 根据id部分修改
这里说的是部分修改,整体覆盖修改参考带id新增
/**
* 直接根据id部分修改
*/
public class ESTestDocUpdate
public static void main(String[] args) throws Exception
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200)));
System.out.println(esClient);
//修改数据 部分修改
UpdateRequest request = new UpdateRequest();
request.index("user").id("1003");
//字段 修改后值
request.doc(XContentType.JSON,"age","23","sex","男性");
UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
//关闭客户端连接
esClient.close();
4、JAVA内查询doc
4.1 根据id查询文档
/**
* 直接根据id查找
*/
public class ESTestDocQueryById
public static void main(String[] args) throws Exception
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200)));
System.out.println(esClient);
//查询数据
GetRequest request = new GetRequest("user");
request.id("_Ee4D4IBS68PlTMezCd0");
GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
User user = JSON.parseObject(response.getSourceAsString(), User.class);
System.out.println(user.toString());
//关闭客户端连接
esClient.close();
4.2 查询全部 matchAll
private static void queryALl(RestHighLevelClient esClient)throws Exception
//查询索引中的全部数据
SearchRequest request = new SearchRequest();
request.indices("user");
//查询全部 matchAll
request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.outelasticsearchjava开发
一、引入包
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.2.4</version>
</dependency>
二、创建索引
@Test
public void creatIndex() throws UnknownHostException {
//1、创建一个Setting对象,相当于是一个配置信息,主要配置集群的名称,
Settings settings = Settings.builder()
.put("cluster.name", "my-elasticsearch").build();
//2、创建一个客户端client对象,内部通过tcp协议,所以使用93
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9301));
client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9301));
client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9303));
//3、使用client对象创建一个索引
client.admin().indices().prepareCreate("index_hello").get();
//4、关闭client对象
client.close();
}
三、创建mapping(创建一个Mappping信息,应该是一个json数据,可以是字符串,也可以是XContentBuilder对象)
@Test
public void creatMapping() throws IOException {
TransportClient transportClient=ElasticSearchClientTest.creatClient();
IndicesAdminClient client= ElasticSearchClientTest.creatClient().admin().indices();
client.prepareCreate("twitter")
.setSettings(Settings.builder()
.put("index.number_of_shards", 5)
.put("index.number_of_replicas", 1)
)
.get();
/* client.preparePutMapping("twitter").setType("user")
.setSource("{
" +
" "properties": {
" +
" "name": {
" +
" "type": "text"
" +
" }
" +
" }
" +
"}", XContentType.JSON)
.get();*/
XContentBuilder xc=XContentFactory.jsonBuilder();
xc.startObject()
.startObject("properties")
.startObject("id")
.field("type","long")
.field("store",true)
.endObject()
.startObject("name")
.field("type","text")
.field("store",true)
.field("index",true)
.field("analyzer","ik_smart")
.endObject()
.startObject("content")
.field("type","text")
.field("store",true)
.field("index",true)
.field("analyzer","ik_smart")
.endObject()
.endObject()
.endObject();
client.preparePutMapping("twitter").setType("article").setSource(xc).get();
transportClient.close();
}
四、创建document
/**
* 使用Map添加文档
* IndexResponse response = client.prepareIndex("twitter", "tweet")
* .setSource(json, XContentType.JSON)
* .get();
*/
@Test
public void creatDocument() throws UnknownHostException {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id",100);
map.put("name","cli_java");
map.put("content","Map is a key:values pair collection. It represents a JSON structure分布式搜索elasticsearch配置文件详解 - 关于代码的那");
TransportClient transportClient=ElasticSearchClientTest.creatClient();
//IndicesAdminClient client= ElasticSearchClientTest.creatClient().admin().indices();
IndexResponse response=transportClient.prepareIndex("twitter", "article") .setSource(map, XContentType.JSON) .get();
transportClient.close();
}
五、查询
@Test
public void getIndex() throws UnknownHostException,IOException {
TransportClient transportClient=ElasticSearchClientTest.creatClient();
//<!--索引库查询-根据id查询-->
/*GetResponse response =transportClient.prepareGet("twitter","article","ER_Lpm4Bob3tI_qjogjc").get();
Map<String,Object> map=response.getSource();
System.out.println(map.get("content"));*/
//<!--结束索引库查询-根据id查询-->
//第二种创建一个查询对象
IdsQueryBuilder queryBuilder=QueryBuilders.idsQuery().addIds("ER_Lpm4Bob3tI_qjogjc","Eh_gpm4Bob3tI_qjggjb");
//执行查询
SearchResponse searchResponse=transportClient.prepareSearch("twitter")
.setTypes("article")
.setQuery(queryBuilder)
.get();
SearchHits searchHits=searchResponse.getHits();
Iterator<SearchHit> iterator= searchHits.iterator();
while(iterator.hasNext()){
SearchHit searchHit=iterator.next();
System.out.println(searchHit.getSourceAsString());
Map<String, Object> map=searchHit.getSourceAsMap();
System.out.println(map.get("content"));
}
transportClient.close();
//结束第二种方式创建查询对象
}
//条件查询
@Test
public void getIndex() throws UnknownHostException,IOException {
TermQueryBuilder qb=QueryBuilders.termQuery("name","cli_java");
ElasticSearchClientTest.useTermGetIndex(qb);
}
@Test
public void testQueryString() throws UnknownHostException {
QueryStringQueryBuilder qsq=QueryBuilders.queryStringQuery("独立寒秋,湘江北去,万里雪飘").defaultField("content");
ElasticSearchClientTest.useTermGetIndex(qsq);
}
private static void useTermGetIndex(TermQueryBuilder queryBuilder) throws UnknownHostException {
TransportClient transportClient=ElasticSearchClientTest.creatClient();
//执行条件查询
SearchResponse searchResponse=transportClient.prepareSearch("twitter")
.setTypes("article")
.setQuery((org.elasticsearch.index.query.QueryBuilder) queryBuilder)
//分页信息
//.setFrom(start)
//.setSize(pageSize)
.get();
SearchHits searchHits=searchResponse.getHits();
Iterator<SearchHit> iterator= searchHits.iterator();
while(iterator.hasNext()){
SearchHit searchHit=iterator.next();
System.out.println(searchHit.getSourceAsString());
Map<String, Object> map=searchHit.getSourceAsMap();
System.out.println(map.get("content"));
}
transportClient.close();
}
//高亮查询
@Test
public void testQueryFormSize() throws UnknownHostException {
QueryStringQueryBuilder qsq=QueryBuilders.queryStringQuery("庆余年").defaultField("name");
for(int i=1;i<111;i+=10){
ElasticSearchClientTest.useTermGetIndex(qsq,i,10);
System.out.println("-------------------------------------------------------------------");
}
}
private static void useTermGetIndex(QueryStringQueryBuilder queryBuilder,int start,int pageSize) throws UnknownHostException {
//高亮设置
HighlightBuilder hb=new HighlightBuilder();
hb.field("name");
hb.preTags("<em>");
hb.postTags("</em>");
TransportClient transportClient=ElasticSearchClientTest.creatClient();
//第二种创建一个查询对象
//执行查询
SearchResponse searchResponse=transportClient.prepareSearch("twitter")
.setTypes("article")
.setQuery((org.elasticsearch.index.query.QueryBuilder) queryBuilder)
//分页信息
.setFrom(start)
.setSize(pageSize)
//高亮查询
.highlighter(hb)
.get();
SearchHits searchHits=searchResponse.getHits();
Iterator<SearchHit> iterator= searchHits.iterator();
while(iterator.hasNext()){
SearchHit searchHit=iterator.next();
System.out.println(searchHit.getSourceAsString());
Map<String, Object> map=searchHit.getSourceAsMap();
System.out.println(map.get("content"));
System.out.println("**************************************************");
Map<String, HighlightField> m=searchHit.getHighlightFields();
System.out.println(m.get("name"));
}
以上是关于ElasticSearchJava中使用ES的主要内容,如果未能解决你的问题,请参考以下文章
Elasticsearchjava.lang.ClassNotFoundException: org.elasticsearch.client.core.CountRequest