备忘&分享
配置elasticearch & kibana服务
环境要求:elasticearch 需要搭建JDK环境 不同版本要求不同的JDK版本
下载地址: https://elasticsearch.cn/download/
elasticsearch的版本和支持的jdk版本情况: 参考官网
elasticearch & kibana 服务版本要保持一致。
1.依次进到 ES 和 Kibana 的 bin 文件夹双击 elasticsearch.bat 和 kibana.bat
2.访问默认的本地地址:
elasticsearch -> localhost:9200
Kibana -> localhost:5601
一.配置项目
相关类库
compile('org.locationtech.jts:jts-core:1.18.1')
compile('org.locationtech.spatial4j:spatial4j:0.8')
compile('org.springframework.boot:spring-boot-starter-data-elasticsearch')
//这个不是必须的,如果你的httpclient 和ES冲突可以使用。主要细节,这块有可能导致httpclient与EScelint冲突,这块也折腾我好一会。主要是版本号的冲突,修改版本号就行了。
compile('org.apache.httpcomponents:httpclient:4.5.2')
配置项yaml
spring:
elasticsearch:
rest:
## ES 的地址
uris: localhost:9200
config文件
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchClientConfig {
@Bean
@Qualifier("highLevelClient")
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient highLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
// 该方法接收一个RequestConfig.Builder对象,对该对象进行修改后然后返回。
@Override
public RequestConfig.Builder customizeRequestConfig(
RequestConfig.Builder requestConfigBuilder) {
return requestConfigBuilder.setConnectTimeout(5000 * 1000) // 连接超时(默认为1秒)
.setSocketTimeout(6000 * 1000);// 套接字超时(默认为30秒)//更改客户端的超时限制默认30秒现在改为100*1000分钟
}
}));// 调整最大重试超时时间(默认为30秒).setMaxRetryTimeoutMillis(60000);
return highLevelClient;
}
}
JAVA定义实例
实体类
注意事项:@Document(indexName = “objecteswkt”) indexName 必须小写,FieldType自定义类型 @ID 是必备的,其他可以不写。时间要用DATE类型。不然没发检索。Keyword适用于精准检索,text适用于分词检索/模糊配置
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import lombok.Data;
@Document(indexName = "objecteswkt")
@Data
public class ObjectesWkt {
@Id
@Field(type = FieldType.Long)
private long objectid;
@Field(type = FieldType.Boolean)
private boolean isdel;
@Field(type = FieldType.Integer)
private int status;
@Field(type = FieldType.Integer)
private int confirm_uid;
@Field(type = FieldType.Date)
private Date confirm_timestamp;
@Field(type = FieldType.Date)
private Date updatetime;
@Field(type = FieldType.Keyword)
private String layername;
@Field(type = FieldType.Double)
private Double mileage;
private String geo;
}
kibana设置ES对应实例的映射
**此处注意事项较多:
1.时间在ES中是时间戳(重点)
2.空间类型 如果您的数据只有点使用 geo_piont 类型。如果不只是有点请使用geo_shape类型(重中之重)
**
PUT /objecteswkt
{
"settings": {
"number_of_replicas": 1,
"number_of_shards": 1
},
"mappings" : {
"properties" : {
"_class" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"confirm_timestamp" : {
"type" : "date",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"confirm_uid" : {
"type" : "long"
},
"geo" : {
"type" : "geo_shape",
"ignore_malformed": true
},
"isdel" : {
"type" : "boolean"
},
"layername" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"objectid" : {
"type" : "long"
},
"status" : {
"type" : "long"
},
"updatetime" : {
"type" : "date",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
kibana的查询
#DELETE /objecteswkt
GET /_cat/indices?v
GET /objecteswkt/_mapping
GET /objecteswkt/_search
{
"query":{
"term":{
"layername":{
"value":"hrarea"
}
}
}
}
GET /objecteswkt/_search
{
"_source": ["confirm_uid"],
"query":{
"geo_shape":{
"geo":{
"relation": "intersects",
"shape":{
"type": "polygon",
"coordinates": [
[[ 114.0, 30.0 ], [ 116.0, 30.0], [ 116.0, 28.0 ],
[ 114.0,28.0 ],[ 114.0, 30.0 ] ]]
}
}
}
},
"collapse":{
"field":"confirm_uid"
}
}
POST objecteswkt/_search
{
"_source": ["confirm_uid"],
"query":{
"match_all":{
}
},
"collapse":{
"field":"confirm_uid"
}
}
GET objecteswkt/_search
{
"_source": ["confirm_uid","layername","status","isdel","mileage"],
"query": {
"bool": {
"must": [
{
"match": {
"isdel": true
}
},{
"match":{
"status":0
}
}
],
"filter": [
{
"range": {
"confirm_timestamp": {
"gte": 1631721600000,
"lte": 1631807999000
}
}
}
]
}
},
"size": 0,
"aggs": {
"group_layername": {
"terms": {
"field": "layername.keyword"
},
"aggs": {
"my_agg": {
"sum": {
"field": "mileage"
}
}
}
}
}
}
GET objecteswkt/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"isdel": true
}
},{
"match":{
"status":0
}
}
]
}
},
"size": 0,
"aggs": {
"group_uid": {
"terms": {
"field": "confirm_uid"
},
"aggs": {
"group_layername": {
"terms": {
"field": "layername.keyword"
},
"aggs": {
"my_agg": {
"sum": {
"field": "mileage"
}
}
}
}
}
}
}
}
GET objecteswkt/_search
{
"size": 0,
"aggs": {
"sumagg": {
"sum": {
"field": "mileage.keyword"
}
}
}
}
java调用
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.geo.builders.CoordinatesBuilder;
import org.elasticsearch.common.geo.builders