参考资料:https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/index.html
注:es版本为2.4.0
1.下载安装:
下载地址:https://www.elastic.co/downloads/elasticsearch
下载之后解压,运行 bin/elasticsearch 即可
2.maven依赖:
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version> 2.4 . 0 </version>
</dependency> <dependency> <groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version> 1.2 . 8 </version>
</dependency> |
3.具体的demo:
package com.tch.test.elasticsearch.test;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import com.alibaba.fastjson.JSON;
public class ElasticSearchTest {
public static final String INDEX = "test" ;
public static final String TYPE = "organization" ;
public static TransportClient buildClient() throws UnknownHostException {
return TransportClient.builder().build()
.addTransportAddress( new InetSocketTransportAddress(InetAddress.getByName( "localhost" ), 9300 ));
}
public static void closeClient(TransportClient client) {
if (client != null ) {
client.close();
}
}
/**
* 创建mapping(类似数据库的表结构)
*
* @param client
* es client
* @param index
* 索引名字
* @param type
* 索引下type的名字
* @throws Exception
*/
public static void createOrgMapping(TransportClient client) throws Exception {
IndicesExistsResponse res = client.admin().indices().prepareExists(INDEX).execute().actionGet();
if (res.isExists()) {
DeleteIndexRequestBuilder delIdx = client.admin().indices().prepareDelete(INDEX);
delIdx.execute().actionGet();
}
CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(INDEX);
final XContentBuilder organizationBuilder = XContentFactory.jsonBuilder()
.startObject()
.startObject(TYPE)
.startObject( "properties" )
.startObject( "id" )
.field( "type" , "long" )
.endObject()
.startObject( "name" )
.field( "type" , "string" )
.endObject()
.startObject( "type" )
.field( "type" , "integer" )
.endObject()
.startObject( "industrialModeCodes" )
.field( "type" , "integer" )
.endObject()
.startObject( "updatedAt" )
.field( "type" , "date" )
.endObject()
.endObject()
.endObject()
.endObject();
createIndexRequestBuilder.addMapping(TYPE, organizationBuilder);
// MAPPING DONE
createIndexRequestBuilder.execute().actionGet();
}
/**
* 往索引中添加数据
*
* @param client
*/
public static void index(TransportClient client, ESOrganization organization) {
IndexResponse response = client.prepareIndex(INDEX, TYPE, String.valueOf(organization.getId()))
.setSource(JSON.toJSONString(organization)).get();
System.out.println(response.getId());
}
public static void getById(TransportClient client, String id) {
GetResponse response = client.prepareGet(INDEX, TYPE, id).get();
System.out.println(response.getSourceAsString());
}
public static void deleteById(TransportClient client, String id) {
DeleteResponse response = client.prepareDelete(INDEX, TYPE, id).get();
System.out.println(response);
}
public static void update(TransportClient client, ESOrganization organization)
throws InterruptedException, ExecutionException {
UpdateRequest updateRequest = new UpdateRequest(INDEX, TYPE, String.valueOf(organization.getId()))
.doc(JSON.toJSONString(organization));
UpdateResponse response = client.update(updateRequest).get();
System.out.println(response);
}
public static void multiGet(TransportClient client, String... ids) {
MultiGetResponse multiGetItemResponses = client.prepareMultiGet().add(INDEX, TYPE, ids).get();
for (MultiGetItemResponse itemResponse : multiGetItemResponses) {
GetResponse response = itemResponse.getResponse();
if (response.isExists()) {
String json = response.getSourceAsString();
System.out.println(json);
}
}
}
/**
* 复杂一点的搜索
* @param client
* @param searchReq
*/
public static void search(TransportClient client, OrgSearchReq searchReq) {
BoolQueryBuilder orgQueryBuilder = QueryBuilders.boolQuery();
if (searchReq.getKeyword() != null && searchReq.getKeyword().length > 0 ) {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
for (String keyword : searchReq.getKeyword()) {
queryBuilder.should(QueryBuilders.matchQuery( "name" , keyword));
}
orgQueryBuilder.must(queryBuilder);
}
if (!CollectionUtils.isEmpty(searchReq.getTypes())) {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
for (Integer type : searchReq.getTypes()) {
queryBuilder.should(QueryBuilders.matchQuery( "type" , type));
}
orgQueryBuilder.must(queryBuilder);
}
if (!CollectionUtils.isEmpty(searchReq.getIndustrialModeCodes())) {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
for (Integer industrialMode : searchReq.getIndustrialModeCodes()) {
queryBuilder.should(QueryBuilders.matchQuery( "industrialModeCodes" , industrialMode));
}
orgQueryBuilder.must(queryBuilder);
}
if (searchReq.getUpdatedAt() != null && searchReq.getUpdatedAt().length > 0 ) {
if (searchReq.getUpdatedAt()[ 0 ] != null ) {
orgQueryBuilder.must(QueryBuilders.rangeQuery( "updatedAt" ).from(searchReq.getUpdatedAt()[ 0 ]));
}
if (searchReq.getUpdatedAt().length == 2 && searchReq.getUpdatedAt()[ 1 ] != null ) {
orgQueryBuilder.must(QueryBuilders.rangeQuery( "updatedAt" ).to(searchReq.getUpdatedAt()[ 1 ]));
}
}
if (StringUtils.isEmpty(searchReq.getSortField())) {
searchReq.setSortField( "updatedAt" );
}
System.out.println( "orgQueryBuilder: " +orgQueryBuilder);
SearchResponse searchResponse = client.prepareSearch(INDEX).setTypes(TYPE)
.setQuery(orgQueryBuilder)
.addSort(searchReq.getSortField(), getSortOrder(searchReq.getSortType()))
.setFrom(searchReq.getOffset())
.setSize(searchReq.getSize())
.execute()
.actionGet();
List<ESOrganization> orgs = new ArrayList<>();
for (SearchHit hit : searchResponse.getHits()) {
ESOrganization org = JSON.parseObject(hit.getSourceAsString(), ESOrganization. class );
orgs.add(org);
}
System.out.println(JSON.toJSONString(orgs, true ));
}
private static SortOrder getSortOrder(String str) {
if (StringUtils.isEmpty(str)) {
return SortOrder.ASC;
}
for (SortOrder sortOrder : SortOrder.values()) {
if (sortOrder.name().equalsIgnoreCase(str)) {
return sortOrder;
}
}
return SortOrder.ASC;
}
private static ESOrganization getUpdateOrg() {
ESOrganization organization = new ESOrganization();
organization.setId(1l);
organization.setType( 1111111111 );
return organization;
}
public static void addIndex(TransportClient client) {
String[] orgNames = {
"小米科技有限责任公司" ,
"乐视网信息技术(北京)股份有限公司" ,
"网易(杭州)网络有限公司" ,
"阿里巴巴(中国)有限公司" ,
"上海希格斯网络科技有限公司" ,
"running man" ,
"chicken run" ,
"tom runs fast"
};
for ( int i = 0 ; i< orgNames.length; i++) {
ESOrganization organization = new ESOrganization();
organization.setId(Long.valueOf(i+ 1 ));
organization.setName(orgNames[i]);
organization.setType(i * 1 );
organization.setIndustrialModeCodes( new Integer[]{ 1 +i, 20 +i, 30 +i});
organization.setUpdatedAt( new Date());
index(client, organization);
}
}
public static void main(String[] args) throws Exception {
TransportClient client = null ;
try {
client = buildClient();
createOrgMapping(client);
addIndex(client);
update(client, getUpdateOrg());
getById(client, "1" );
multiGet(client, "2" , "3" );
deleteById(client, "8" );
//索引创建成功之后1秒钟左右,才会变得可用
TimeUnit.SECONDS.sleep( 1 );
search(client, getSearchReq());
} finally {
closeClient(client);
}
}
private static OrgSearchReq getSearchReq() {
OrgSearchReq req = new OrgSearchReq();
req.setKeyword( new String[]{ "阿里巴巴" , "乐视" });
req.setTypes(Arrays.asList( 3 ));
req.setIndustrialModeCodes(Arrays.asList( 23 , 33 ));
req.setUpdatedAt( new Date[]{ null , new Date()});
return req;
}
} |
package com.tch.test.elasticsearch.test;
import java.util.Date;
public class ESOrganization {
private Long id;
private String name;
private Integer type;
private Integer[] industrialModeCodes;
private Date updatedAt;
public Long getId() {
return id;
}
public void setId(Long id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this .type = type;
}
public Integer[] getIndustrialModeCodes() {
return industrialModeCodes;
}
public void setIndustrialModeCodes(Integer[] industrialModeCodes) {
this .industrialModeCodes = industrialModeCodes;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this .updatedAt = updatedAt;
}
} |
package com.tch.test.elasticsearch.test;
import java.util.Date;
import java.util.List;
public class OrgSearchReq {
private String[] keyword;
private List<Integer> types;
private List<Integer> industrialModeCodes;
private Date[] updatedAt;
private Integer page;
private Integer size;
private String sortField;
private String sortType;
private static final int FIRST_PAGE = 1 ;
private static final int PAGE_SIZE = 10 ;
public String[] getKeyword() {
return keyword;
}
public void setKeyword(String[] keyword) {
this .keyword = keyword;
}
public List<Integer> getTypes() {
return types;
}
public void setTypes(List<Integer> types) {
this .types = types;
}
public List<Integer> getIndustrialModeCodes() {
return industrialModeCodes;
}
public void setIndustrialModeCodes(List<Integer> industrialModeCodes) {
this .industrialModeCodes = industrialModeCodes;
}
public Date[] getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date[] updatedAt) {
this .updatedAt = updatedAt;
}
public Integer getPage() {
if (page == null || page < 0 ) {
return FIRST_PAGE;
}
return page;
}
public void setPage(Integer page) {
this .page = page;
}
public Integer getSize() {
if (size == null || size < 0 ) {
return PAGE_SIZE;
}
return size;
}
public void setSize(Integer size) {
this .size = size;
}
public String getSortField() {
return sortField;
}
public void setSortField(String sortField) {
this .sortField = sortField;
}
public String getSortType() {
return sortType;
}
public void setSortType(String sortType) {
this .sortType = sortType;
}
public int getOffset() {
return (getPage() - 1 ) * getSize();
}
} |
4.demo解释:
上面的demo中,ESOrganization.java为存到es中的数据结构,OrgSearchReq.java为搜索的条件包装类。
简单的CRUD就不说了,下面主要说一下搜索(search)的部分:
搜索中使用了
BoolQueryBuilder orgQueryBuilder = QueryBuilders.boolQuery(); |
在搜索条件复杂的情况下,我们只需要根据自己的需求使用 BoolQueryBuilder 的 must 和 should来添加 query 的条件,
这里的 must 类似 sql 中的 and, should 类似 sql 中的 or
例如上面的demo中:
BoolQueryBuilder orgQueryBuilder = QueryBuilders.boolQuery(); if (searchReq.getKeyword() != null && searchReq.getKeyword().length > 0 ) {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
for (String keyword : searchReq.getKeyword()) {
queryBuilder.should(QueryBuilders.matchQuery( "name" , keyword));
}
orgQueryBuilder.must(queryBuilder);
} if (!CollectionUtils.isEmpty(searchReq.getTypes())) {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
for (Integer type : searchReq.getTypes()) {
queryBuilder.should(QueryBuilders.matchQuery( "type" , type));
}
orgQueryBuilder.must(queryBuilder);
} if (!CollectionUtils.isEmpty(searchReq.getIndustrialModeCodes())) {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
for (Integer industrialMode : searchReq.getIndustrialModeCodes()) {
queryBuilder.should(QueryBuilders.matchQuery( "industrialModeCodes" , industrialMode));
}
orgQueryBuilder.must(queryBuilder);
}
if (searchReq.getUpdatedAt() != null && searchReq.getUpdatedAt().length > 0 ) {
if (searchReq.getUpdatedAt()[ 0 ] != null ) {
orgQueryBuilder.must(QueryBuilders.rangeQuery( "updatedAt" ).from(searchReq.getUpdatedAt()[ 0 ]));
}
if (searchReq.getUpdatedAt().length == 2 && searchReq.getUpdatedAt()[ 1 ] != null ) {
orgQueryBuilder.must(QueryBuilders.rangeQuery( "updatedAt" ).to(searchReq.getUpdatedAt()[ 1 ]));
}
}
if (StringUtils.isEmpty(searchReq.getSortField())) {
searchReq.setSortField( "updatedAt" );
} |
对应到sql语句就是:
where ( name = 'xxx1' or name = 'xxx2' ) and (type = xxx3 or type = xxx4) and (industrialModeCodes = xxx5 or industrialModeCodes = xxx6) and updatedAt > xxx7 and updatedAt < xxx8
|
.addSort(searchReq.getSortField(), getSortOrder(searchReq.getSortType())) .setFrom(searchReq.getOffset()) .setSize(searchReq.getSize()) |
对应的就是sql中的排序和分页。
但有一点不同就是
QueryBuilders.matchQuery( "name" , keyword)
|
和sql中的 name=xxx还有一点不同,因为前者会做分词,例如name为'小米有限公司' 经过分词之后,我们使用matchQuery搜索的时候,keyword输入 '小米' 也能搜到这个记录。
如果要完全等同,那就要使用:
QueryBuilders.termQuery( "name" , "小米" )
|
termQuery不会使用分词,而会使用精确匹配。
范围搜索则是使用rangeQuery:
QueryBuilders.rangeQuery( "updatedAt" ).from(xxx)
|
5.查看索引的mapping:
http://localhost:9200/test/organization/_mapping?pretty
6.查看索引中的记录:
http://localhost:9200/test/organization/_search?pretty
7.es中index和type的区别
https://www.elastic.co/blog/index-vs-type
8.
2.0之后,filter已经和query合并:
9.elastic如何trouble shooting:
相关推荐
2. **Java API入门** - Elasticsearch Java API允许开发者在Java应用程序中直接操作Elasticsearch集群,创建索引、插入文档、执行查询等。 - 首先,你需要添加Elasticsearch的Java客户端库到项目依赖中,通常使用...
本手册不仅详细描述了如何使用Java作为客户端访问ElasticSearch的代码和方法,同时也描述了在使用ElasticSearch遇到的坑和解决方案,是新手入门不可多得的教材,也是老手借鉴提高的材料。
Elasticsearch Java API是Elasticsearch提供的官方客户端之一,允许开发者通过Java代码来操作Elasticsearch,提供了一个更为便捷和安全的方式来与Elasticsearch集群交互。 入门Elasticsearch时,首先应该熟悉...
(狂神)ElasticSearch快速入门笔记,ElasticSearch基本操作以及爬虫(Java-ES仿京东实战),包含了小狂神讲的东西,特别适合新手学习,笔记保存下来可以多看看。好记性不如烂笔头哦~,ElasticSearch,简称es,es是一个...
在 ELK 技术栈(Elasticsearch, Logstash, Kibana)中,Elasticsearch 负责存储和索引日志数据,SpringBoot 是一个流行的 Java 应用开发框架,它们经常一起用于企业级的日志管理和分析系统。 **1. Elasticsearch ...
001 - 开篇.mp4 002 - 技术选型.mp4 003 - 教学大纲.mp4 008 - 入门 - HTTP - 索引 - 创建.mp4 009 - 入门 - HTTP - 索引 - 查询 & 删除.mp4 010 - 入门 - HTTP - 文档 - 创建 (Put & ...025 - 入门 - JavaAPI - 文档
Elaticsearch,简称为es, es是一个开源的高扩展的分布式...es也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTful API来隐藏Lucene的复杂性,从而让全文搜索变得简单。
首先,需要确保系统中安装了Java 8,因为Elasticsearch是用Java编写的。接着,可以从Elasticsearch官方网站下载对应操作系统的压缩包。对于Linux系统,可以通过执行`./bin/elasticsearch`命令启动Elasticsearch服务...
Elasticsearch 2.x系列是这一产品的中早期版本,它提供了一系列的API以供开发者使用和维护集群。 标题中提到的“离线api”意味着提供的资料是一套完整的Elasticsearch 2.x版本API的参考文档,该文档以PDF格式呈现,...
BBoss 是一个针对 Elasticsearch 的企业级开发框架,它提供了丰富的 Java API,使得开发者可以更简单地进行索引管理、查询构建、结果分析等操作。BBoss 还支持多版本兼容,方便应对 Elasticsearch 的升级问题。 **...
本篇文章将详细介绍 LOCAL 安装 Elasticsearch 和 Kibana,以及使用开发工具/ Java SDK 创建索引和搜索数据。 一、本地安装 要安装 Elasticsearch 和 Kibana,需要创建一个供它们使用的网络。这个网络将被用于 ...
在本项目"springboot+elasticsearch"中,开发者提供了一个全面的入门教程,涵盖了Elasticsearch的基本操作,包括增、删、改、查等基本功能,这对于初学者理解这两者如何协同工作非常有帮助。 首先,让我们深入了解...
**Elasticsearch RESTful搜索引擎与Java Jest库的入门教程** Elasticsearch是一个强大的分布式、开源的全文搜索引擎,它提供了一个可扩展的、近实时的搜索和分析引擎。RESTful API是Elasticsearch的核心特性,允许...
二、Java API入门 1. 添加依赖 在你的Java项目中,首先需要引入Elasticsearch的Java客户端库。如果你使用的是Maven,可以在pom.xml文件中添加以下依赖: ```xml <groupId>org.elasticsearch <artifactId>...
Java API是Elasticsearch的重要组成部分,提供了与Elasticsearch交互的客户端对象。所有操作都是异步的,可以通过监听器或未来对象进行处理,并且支持批量操作。为了使用Java API,你需要在Maven项目中添加Elastic...
ElasticSearch 提供了官方的 Java API,允许开发者使用 Java 语言来操作 ElasticSearch。 - **TransportClient**:这是一种直接与集群通信的方式。然而,从 Elasticsearch 7.0 开始已经标记为废弃,并计划在 8.0 ...
Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。 我们建立一个网站或应用程序,并要...
"Elasticsearch 入门操作" Elasticsearch 是一个基于 Lucene 库的搜索引擎,提供了一个分布式、支持多用户的全文搜索引擎,具有 HTTP Web 接口和无模式 JSON 文档。所有其他语言可以使用 RESTful API 通过端口 9200...
Elasticsearch 2.1.1服务端集群搭建.及java客户端demo代码实现。Elasticsearch权威指南(中文版,Elasticsearch java api 学习入门源码部署相关资料