当前位置:   article > 正文

ElasticSearch如何使用以及java代码如何查询并排序ES中的数据(距离排序)_es根据经纬度召回后如何按照距离排序

es根据经纬度召回后如何按照距离排序

import org.elasticsearch.client.RestHighLevelClient;

import org.elasticsearch.client.transport.TransportClient;

import org.elasticsearch.common.geo.GeoDistance;

import org.elasticsearch.common.settings.Settings;

import org.elasticsearch.common.transport.TransportAddress;

import org.elasticsearch.common.unit.DistanceUnit;

import org.elasticsearch.common.unit.TimeValue;

import org.elasticsearch.common.xcontent.XContentBuilder;

import org.elasticsearch.common.xcontent.XContentFactory;

import org.elasticsearch.index.query.BoolQueryBuilder;

import org.elasticsearch.index.query.GeoDistanceQueryBuilder;

import org.elasticsearch.index.query.GeoValidationMethod;

import org.elasticsearch.index.query.QueryBuilder;

import org.elasticsearch.index.query.QueryBuilders;

import org.elasticsearch.search.SearchHit;

import org.elasticsearch.search.SearchHits;

import org.elasticsearch.search.builder.SearchSourceBuilder;

import org.elasticsearch.search.sort.GeoDistanceSortBuilder;

import org.elasticsearch.search.sort.SortBuilders;

import org.elasticsearch.search.sort.SortOrder;

import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.io.IOException;

import java.net.InetAddress;

import java.net.UnknownHostException;

/**

  • es测试

*/

public class EsDemo

{

private static final RequestOptions COMMON_OPTIONS;

static {

RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();

// 默认缓存限制为100MB,此处修改为30MB。

builder.setHttpAsyncResponseConsumerFactory(

new HttpAsyncResponseConsumerFactory

.HeapBufferedResponseConsumerFactory(30 * 1024 * 1024));

COMMON_OPTIONS = builder.build();

}

protected RestHighLevelClient client = new RestHighLevelClient(

RestClient.builder(new HttpHost(“127.0.0.1”, 9200, “http”)));;

private String indexName = “hbp”;

private String type = “_doc”;

//从ES中查询数据

@Test

public void test1() throws IOException {

Integer pageSize = 10;

Integer PageNum = 0;

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

BoolQueryBuilder bq = QueryBuilders.boolQuery();

//按时间范围查询

QueryBuilder dateBuilder = QueryBuilders.rangeQuery(“pub_date”)

.gte(“2020-09-01 10:30:00”).lte(“2020-09-02 13:50:00”).format(“yyyy-MM-dd HH:mm:ss”);//时间范围查询

//根据id查询

List idList = new ArrayList();

idList.add(“1”);

idList.add(“2”);

idList.add(“3”);

// idList.add(“4”);

// idList.add(“5”);

QueryBuilder idBuilder = QueryBuilders.termsQuery(“_id”, idList);

// 以某点为中心,搜索指定范围

GeoDistanceQueryBuilder distanceQueryBuilder = new GeoDistanceQueryBuilder(“location”);

distanceQueryBuilder.point(39.662263, 118.197815);

//查询单位:km

distanceQueryBuilder.distance(“6”, DistanceUnit.KILOMETERS);

//构建检索

/*这里说明一下,distanceQueryBuilder就是距离查询条件,idBuilder就是id过滤条件,

dateBuilder就是时间范围查询条件,可以定义一个或多个条件(QueryBuilder),下面的

排序也是同理,可以构造多个sortBuilder*/

QueryBuilder query = bq

// .filter(distanceQueryBuilder)

// .must(idBuilder)

.filter(dateBuilder)

;

searchSourceBuilder.query(query);

//按时间排序

// searchSourceBuilder.sort(“pub_date”, SortOrder.DESC);

//按距离排序

GeoDistanceSortBuilder distanceSort = SortBuilders.geoDistanceSort(“location”, 39.662263, 118.197815);

distanceSort.order(SortOrder.ASC);

distanceSort.geoDistance(GeoDistance.ARC);

distanceSort.unit(DistanceUnit.KILOMETERS);

distanceSort.validation(GeoValidationMethod.STRICT);

searchSourceBuilder.sort(distanceSort);

//查询es数据

searchSourceBuilder.from(PageNum);

searchSourceBuilder.size(pageSize);

searchSourceBuilder.timeout(new TimeValue(100,TimeUnit.SECONDS));

SearchRequest searchRequest = new SearchRequest(“hbp”);

searchRequest.source(searchSourceBuilder);

SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

SearchHits hits = response.getHits();

for(SearchHit hit : hits){

System.out.println(hit.getSourceAsString());

System.out.println(hit.getSortValues()[0]);//获取排序的距离,index为排序的位置

}

//get方式数据查询 ,参数为Index,type和id

// GetRequest request = new GetRequest(indexName, “7”);

// GetResponse getResponse1 = client.get(request, COMMON_OPTIONS);

// System.out.println(getResponse1.getSourceAsString());

client.close();

}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!
2d36.jpg" alt=“img” style=“zoom: 33%;” />

最后

[外链图片转存中…(img-iqBZAZnb-1711967552793)]

[外链图片转存中…(img-368cQVrX-1711967552793)]

[外链图片转存中…(img-7seEsk98-1711967552794)]

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/636751
推荐阅读
相关标签
  

闽ICP备14008679号