目录
安装elasticsearch
maven
数据来源: 网易数据
mysql的相关语法文章来源:https://www.toymoban.com/news/detail-608832.html
Java代码文章来源地址https://www.toymoban.com/news/detail-608832.html
安装elasticsearch
docker run -p 9200:9200 -p 9300:9300 --name es_643 -d elasticsearch:6.4.3
maven
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.16.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
数据来源: 网易数据
https://c.m.163.com/ug/api/wuhan/app/data/list-total?t=334061612053
mysql的相关语法
- =
- in
- like
- >
- between and
- is null
- is not null
- order by
- max min avg sum (发现es新语法stats直接统计聚合)
Java代码
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import lombok.Data;
import lombok.ToString;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.DisMaxQueryBuilder;
import org.elasticsearch.index.query.ExistsQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.avg.InternalAvg;
import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
import org.elasticsearch.search.aggregations.metrics.min.InternalMin;
import org.elasticsearch.search.aggregations.metrics.stats.InternalStats;
import org.elasticsearch.search.aggregations.metrics.sum.InternalSum;
import org.elasticsearch.search.aggregations.support.ValueType;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortMode;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
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 org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
public class EsTest {
public static void main(String[] args) throws Exception {
// String data = HttpUtil.get(
// "https://c.m.163.com/ug/api/wuhan/app/data/list-total?t=334061612053");
// JSONObject parseObject = JSONObject.parseObject(data);
// JSONObject dataobj = parseObject.getJSONObject("data");
// JSONArray areaTree = dataobj.getJSONArray("areaTree");
// List<IndexQuery> queries = new ArrayList<>();
// for (int i = 0; i < areaTree.size(); i++) {
// JSONObject jsonObject = areaTree.getJSONObject(i);
// String name = jsonObject.getString("name");
// String id = jsonObject.getString("id");
// JSONObject total = jsonObject.getJSONObject("total");
// Integer confirm = total.getInteger("confirm");
// Integer input = total.getInteger("input");
// Integer severe = total.getInteger("severe");
// Integer heal = total.getInteger("heal");
// Integer dead = total.getInteger("dead");
// Integer suspect = total.getInteger("suspect");
// Area area = new Area();
// area.setId(id);
// area.setPid("-1");
// area.setName(name);
// area.setConfirm(confirm);
// area.setInput(input);
// area.setSevere(severe);
// area.setHeal(heal);
// area.setDead(dead);
// area.setSuspect(suspect);
// IndexQuery indexQuery = new IndexQuery();
// indexQuery.setId(id.toString());
// indexQuery.setObject(area);
// indexQuery.setIndexName("area");
// queries.add(indexQuery);
//
// }
Settings settings = Settings.builder().put("cluster.name", "docker-cluster").build();
TransportClient transportClient = new PreBuiltTransportClient(settings);
transportClient.addTransportAddress(new
TransportAddress(InetAddress.getByName("localhost"), 9300));
ElasticsearchTemplate elasticsearchTemplate = new ElasticsearchTemplate(
transportClient);
// boolean exists = elasticsearchTemplate.indexExists(Area.class);
// System.out.println(exists);
// if (exists) {
// boolean deleteIndex = elasticsearchTemplate.deleteIndex(Area.class);
// System.out.println(deleteIndex);
// }
// boolean index = elasticsearchTemplate.createIndex(Area.class);
// System.out.println(index);
// elasticsearchTemplate.bulkIndex(queries);
// queries.clear();
//mysql =
// NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
// nativeSearchQueryBuilder.withQuery(QueryBuilders.termQuery("name.keyword","中国"));
// NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
// AggregatedPage<Area> areas = elasticsearchTemplate.queryForPage(searchQuery, Area.class);
// System.out.println(areas.getContent());
//mysql in
// NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
// nativeSearchQueryBuilder.withQuery(QueryBuilders.termsQuery("name.keyword", "美国", "日本本土"));
// NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
// AggregatedPage<Area> areas = elasticsearchTemplate.queryForPage(searchQuery, Area.class);
// System.out.println(areas.getContent());
//mysql like
// NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
// nativeSearchQueryBuilder.withQuery(QueryBuilders.wildcardQuery("name.keyword", "*斯坦*"));
// NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
// AggregatedPage<Area> areas = elasticsearchTemplate.queryForPage(searchQuery, Area.class);
// System.out.println(areas.getContent());
//mysql >10000
// NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
// nativeSearchQueryBuilder.withQuery(QueryBuilders.rangeQuery("confirm").from(1000000));
// NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
// AggregatedPage<Area> areas = elasticsearchTemplate.queryForPage(searchQuery, Area.class);
// System.out.println(areas.getContent());
//mysql between 1000 and 10000
// NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
// nativeSearchQueryBuilder.withQuery(QueryBuilders.rangeQuery("confirm").from(1000).to(10000));
// NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
// AggregatedPage<Area> areas = elasticsearchTemplate.queryForPage(searchQuery, Area.class);
// System.out.println(areas.getContent());
//mysql is null
// NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
// BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// ExistsQueryBuilder existsQueryBuilder = QueryBuilders.existsQuery("input");
// boolQueryBuilder.mustNot(existsQueryBuilder);
// nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
// NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
// AggregatedPage<Area> areas = elasticsearchTemplate.queryForPage(searchQuery, Area.class);
// System.out.println(areas.getContent());
//mysql is not null
// NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
// BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// ExistsQueryBuilder existsQueryBuilder = QueryBuilders.existsQuery("input");
// boolQueryBuilder.must(existsQueryBuilder);
// nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
// NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
// AggregatedPage<Area> areas = elasticsearchTemplate.queryForPage(searchQuery, Area.class);
// System.out.println(areas.getContent());
//mysql order by
// NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
// FieldSortBuilder sortBuilder = SortBuilders.fieldSort("confirm").order(SortOrder.DESC);
FieldSortBuilder sortBuilder = SortBuilders.fieldSort("confirm").order(SortOrder.ASC);
// nativeSearchQueryBuilder.withSort(sortBuilder);
// NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
// AggregatedPage<Area> areas = elasticsearchTemplate.queryForPage(searchQuery, Area.class);
// System.out.println(areas.getContent());
//mysql max min avg
// NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
// nativeSearchQueryBuilder.addAggregation(
// AggregationBuilders.max("max_confirm").field("confirm").valueType(ValueType.LONG));
// nativeSearchQueryBuilder.addAggregation(
// AggregationBuilders.min("min_confirm").field("confirm").valueType(ValueType.LONG));
// nativeSearchQueryBuilder.addAggregation(
// AggregationBuilders.avg("avg_confirm").field("confirm").valueType(ValueType.LONG));
// nativeSearchQueryBuilder.addAggregation(
// AggregationBuilders.sum("sum_confirm").field("confirm").valueType(ValueType.LONG));
// nativeSearchQueryBuilder.addAggregation(
// AggregationBuilders.stats("stats_confirm").field("confirm").valueType(ValueType.LONG));
// nativeSearchQueryBuilder.withFields("id");
// NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
// AggregatedPage<Area> areas = elasticsearchTemplate.queryForPage(searchQuery, Area.class);
// InternalMax maxConfirm = (InternalMax) areas.getAggregation("max_confirm");
// InternalMin minConfirm = (InternalMin) areas.getAggregation("min_confirm");
// InternalAvg avgConfirm = (InternalAvg) areas.getAggregation("avg_confirm");
// InternalSum sumConfirm = (InternalSum) areas.getAggregation("sum_confirm");
// InternalStats statsConfirm = (InternalStats) areas.getAggregation("stats_confirm");
// System.out.println(BigDecimal.valueOf(maxConfirm.getValue()));
// System.out.println(BigDecimal.valueOf(minConfirm.getValue()));
// System.out.println(BigDecimal.valueOf(avgConfirm.getValue()).setScale(0, RoundingMode.HALF_UP));
// System.out.println(BigDecimal.valueOf(sumConfirm.getValue()));
// System.out.println("--------------------Stats---------------------------");
// System.out.println(BigDecimal.valueOf(statsConfirm.getMax()));
// System.out.println(BigDecimal.valueOf(statsConfirm.getMin()));
// System.out.println(BigDecimal.valueOf(statsConfirm.getAvg()));
// System.out.println(BigDecimal.valueOf(statsConfirm.getSum()));
// System.out.println(BigDecimal.valueOf(statsConfirm.getCount()));
}
}
@Data
@ToString
@Document(indexName = "area")
class Area {
@Id
private String id;
private String pid;
@Field(type = FieldType.Keyword)
private String name;
private Integer confirm;
private Integer input;
private Integer severe;
private Integer heal;
private Integer dead;
private Integer suspect;
}
到了这里,关于ES-1:Java的 ElasticsearchTemplate Or ElasticsearchRepository基础常用查询写法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!