ElasticSearch Geo Distance sql合并查询结果果有问题,全部查出来了,没有根据距离和经纬度来过滤

15:36 提问
java elasticsearch 属性如何定义为geo_point
java elasticsearch model属性如何定义为geo_point,@GeoPointField无法引入,是缺包吗。
按赞数排序
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐Elasticsearch地理位置查询-Geo Distance Range Query - 简书
Elasticsearch地理位置查询-Geo Distance Range Query
官方文档:
欢迎访问本人博客:
索引mapping定义:
索引中定义一个字段pin,添加一个属性location,type为geo_point
"properties" : {
"location" : {
"type" : "geo_point"
报文中的包含一个match all的query
, filter中的distance指定了距离范围,pin.location是经纬度
"bool" : {
"must" : {
"match_all" : {}
"filter" : {
"geo_distance" : {
"distance" : "200km",
"pin.location" : {
"lat" : 40,
"lon" : -70
拼装 query 和 sort
QueryBuilder builder = new GeoDistanceRangeQueryBuilder ("pin.location")
.point(lat,lon)
.from("0km")
.to("10000km")
.includeLower(true)
.includeUpper(false)
.optimizeBbox("memory")
.geoDistance(GeoDistance.ARC);
GeoDistanceSortBuilder sort = SortBuilders.geoDistanceSort("location");
GeoDistanceSortBuilder sort = new GeoDistanceSortBuilder("location");
sort.unit(DistanceUnit.KILOMETERS);
sort.order(SortOrder.ASC);
sort.point(lat,lon);
构造dsl生产器
SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();
sourceBuilder.query(QueryBuilders.boolQuery())
.must(builder)
sourceBuilder.sort(sort);
调用elasticsearch
final SearchResponse response = executeGet(new ClientCallback&SearchResponse&() {
public ActionFuture&SearchResponse& execute(final Client client) {
final SearchSourceBuilder sourceBuilder =
SearchParamUtils
.genSearchSourceBuilderFromSearchParam(searchParam);
String[] indexNames = new String[aliasIndexNameList.size()];
SearchRequest request = Requests.searchRequest(aliasIndexNameList.toArray(indexNames))
.types(type);
request.source(sourceBuilder.toString());
if (searchParam.getSearchType() != null) {
request.searchType(searchParam.getSearchType());
return client.search(request);
获取每条记录的距离
SearchResult searchResult = new SearchResult();
SearchHits hits = response.getHits();
for (SearchHit hit : hits.getHits()) {
Object[] sortArray = hit.getSortValues();
if(sortArray!=null&&sortArray.length&0){
BigDecimal geoDis = new BigDecimal((Double) sortArray[sortArray.length-1]);
map.put("geoDistance", geoDis.setScale(0, BigDecimal.ROUND_HALF_DOWN));
System.out.println("距离" + hit.getSource().get("geoDistance"));
专注于Java 后端框架 分布式 中间件 搜索引擎等技术分享,欢迎访问下方我的独立博客和关注微信订阅号。
Elasticsearch Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎。无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进、性能最好的、功能最全的搜索引擎库。但是,Lucene只是一个库。想要使用它,你必须使用Java来...
Neil Zhu,简书ID Not_GOD,University AI 创始人 & Chief Scientist,致力于推进世界人工智能化进程。制定并实施 UAI 中长期增长战略和目标,带领团队快速成长为人工智能领域最专业的力量。作为行业领导者,他和UAI一起在2014年...
百姓网 Elasticsearch 2.x 升级之路 导读:Elasticsearch 是广泛使用的一个软件,我们邀请了曾经在高可用架构分享过 ES 的王卫华继续分享在升级 Elasticsearch 过程中的经验。 王卫华,资深开发工程师、架构师,具有 10+ 年互联网从...
ElasticSearch是一个高度可扩展的开源搜索引擎并使用REST API,所以您值得拥有。 在本教程中,将介绍开始使用ElasticSearch的一些主要概念。 下载并运行ElasticSearch ElasticSearch可以从elasticsearch.org下...
Geo Points Geo-points 就是我们地球的经纬度,它能够用于计算地球两点之间的距离。 Geo-points 不能使用动态mapping,必须提前制定好geo_point类型 Lat/Lon Formats 假设我们把 location 字段定义为geo_po...
不知道是否因为年龄的增长,被时间逐渐磨平了棱角,甚至是开了一道口子。原本只知道一味的得到、抱怨、自以为是的我,开始变得和从前不太一样了,因为我学会了感恩。 要知道,整个生命的过程中,人总是在不停的经历着获得和失去的。而且这两者是同时进行的,也就是说每一个人随时随地都是在获得...
《我们在一种怎样的日子里?》 我们在过一场怎样的日子, 在过一种怎样的生活? 虚度光阴时空虚煎熬, 奔波辛劳时疲惫烦躁。 生活总有更好的人出现, 日子总会有更成功人士的点缀, 却,也有对未知的不安, 也有对人世疾苦的怜悯。 不论眼下多么卑微, 依旧有更卑微的人。 不论眼下有...
有没有发现,在我们周围,有的人无论在学习、工作,还是在生活中,总是很积极乐观,似乎没有什么烦心事,还有的人则更好相反,凡事总往坏处想,整体愁眉苦脸,消极悲观,好像倒霉的事总让他遇到。 以前我以为这可能是性格差异造成的,没办法,改不了。 但是,在《成功心理学——发现工作与生活...
我们都在做竞品分析,那什么才是有效的竞品分析呢?总结笔记如下: 1、什么是竞品 从四个维度 市场、用户、功能、场景(用户使用时间)。都相同的为直接竞品,用户相同,功能略有不同,但满足本质用户需求的为潜在竞品,功能相同的为替代竞品。 2、何为有效 如下三点,数据信息、分析思考...
要是让老牛看到,说不定会跟我拼命。 但是,他无疑是愤青的,虽然她打死也不会承认这点。 圆脸矮个小眼,笑起来两眼眯成一条缝,有点像弥勒佛,很有喜剧味道。 老牛经常说起在国企上班的日子,总结下就四个字:纸醉金迷。 所以,这么一个“满身铜臭”的人,在雪山高原上开了一家客栈。 “情...在Elasticsearch中,通过使用基于JSON的查询进行搜索。 查询由两个子句组成 -
叶查询子句 - 这些子句是匹配,项或范围的,它们在特定字段中查找特定值。复合查询子句 - 这些查询是叶查询子句和其他复合查询的组合,用于提取所需的信息。
Elasticsearch支持大量查询。 查询从查询关键字开始,然后以JSON对象的形式在其中包含条件和过滤器。以下描述了不同类型的查询 -
匹配所有查询这是最基本的查询; 它返回所有内容,并为每个对象的分数为1.0。 例如,
POST http://localhost:9200/schools*/_search
&match_all&:{}
&took&:1, &timed_out&:false, &_shards&:{&total&:10, &successful&:10, &failed&:0},
&total&:5, &max_score&:1.0, &hits&:[
&_index&:&schools&, &_type&:&school&, &_id&:&2&, &_score&:1.0,
&_source&:{
&name&:&Saint Paul School&, &description&:&ICSE Affiliation&,
&street&:&Dawarka&, &city&:&Delhi&, &state&:&Delhi&,
&zip&:&110075&, &location&:[28..0122136], &fees&:5000,
&tags&:[&Good Faculty&, &Great Sports&], &rating&:&4.5&
&_index&:&schools_gov&, &_type&:&school&, &_id&:&2&, &_score&:1.0,
&_source&:{
&name&:&Government School&, &description&:&State Board Affiliation&,
&street&:&Hinjewadi&, &city&:&Pune&, &state&:&MH&, &zip&:&411057&,
&location&:[18..6821995], &fees&:500, &tags&:[&Great Sports&],
&rating&:&4&
&_index&:&schools&, &_type&:&school&, &_id&:&1&, &_score&:1.0,
&_source&:{
&name&:&Central School&, &description&:&CBSE Affiliation&,
&street&:&Nagan&, &city&:&paprola&, &state&:&HP&,
&zip&:&176115&, &location&:[31..8380405],
&fees&:2200, &tags&:[&Senior Secondary&, &beautiful campus&],
&rating&:&3.3&
&_index&:&schools_gov&, &_type&:&school&, &_id&:&1&, &_score&:1.0,
&_source&:{
&name&:&Model School&, &description&:&CBSE Affiliation&,
&street&:&silk city&, &city&:&Hyderabad&, &state&:&AP&,
&zip&:&500030&, &location&:[17..4752129], &fees&:700,
&tags&:[&Senior Secondary&, &beautiful campus&], &rating&:&3&
&_index&:&schools&, &_type&:&school&, &_id&:&3&, &_score&:1.0,
&_source&:{
&name&:&Crescent School&, &description&:&State Board Affiliation&,
&street&:&Tonk Road&, &city&:&Jaipur&, &state&:&RJ&, &zip&:&176114&,
&location&:[26..7923988], &fees&:2500,
&tags&:[&Well equipped labs&], &rating&:&4.5&
全文查询这些查询用于搜索整个文本,如章节或新闻文章。 此查询根据与特定索引或文档相关联的分析器一起工作。 在本节中,我们将讨论不同类型的全文查询。
匹配查询此查询将文本或短语与一个或多个字段的值匹配。 例如,
POST http://localhost:9200/schools*/_search
&match& : {
&city&:&pune&
&took&:1, &timed_out&:false, &_shards&:{&total&:10, &successful&:10, &failed&:0},
&total&:1, &max_score&:0., &hits&:[{
&_index&:&schools_gov&, &_type&:&school&, &_id&:&2&, &_score&:0.,
&_source&:{
&name&:&Government School&, &description&:&State Board Afiliation&,
&street&:&Hinjewadi&, &city&:&Pune&, &state&:&MH&, &zip&:&411057&,
&location&:[18..6821995], &fees&:500,
&tags&:[&Great Sports&], &rating&:&4&
multi_match查询此查询将文本或短语与多个字段匹配。 例如,
POST http://localhost:9200/schools*/_search
&multi_match& : {
&query&: &hyderabad&,
&fields&: [ &city&, &state& ]
&took&:16, &timed_out&:false, &_shards&:{&total&:10, &successful&:10, &failed&:0},
&total&:1, &max_score&:0., &hits&:[{
&_index&:&schools_gov&, &_type&:&school&, &_id&:&1&, &_score&:0.,
&_source&:{
&name&:&Model School&, & description&:&CBSE Affiliation&,
&street&:&silk city&, &city&:&Hyderabad&, &state&:&AP&,
&zip&:&500030&, &location&:[17..4752129], &fees&:700,
&tags&:[&Senior Secondary&, &beautiful campus&], &rating&:&3&
查询字符串查询此查询使用查询解析器和query_string关键字。 例如,
POST http://localhost:9200/schools/_search
&query_string&:{
&query&:&good faculty&
&took&:16, &timed_out&:false, &_shards&:{&total&:10, &successful&:10, &failed&:0},
&total&:1, &max_score&:0., &hits&:[{
&_index&:&schools&, &_type&:&school&, &_id&:&2&, &_score&:0.,
&_source&:{
&name&:&Saint Paul School&, &description&:&ICSE Affiliation&,
&street&:&Dawarka&, &city&:&Delhi&, &state&:&Delhi&,
&zip&:&110075&, &location&:[28..0122136],
&fees&:5000, &tags&:[&Good Faculty&, &Great Sports&],
&rating&:&4.5&
期限等级查询这些查询主要处理结构化数据,如数字,日期和枚举。 例如,
POST http://localhost:9200/schools/_search
&term&:{&zip&:&176115&}
&took&:1, &timed_out&:false, &_shards&:{&total&:10, &successful&:10, &failed&:0},
&total&:1, &max_score&:0., &hits&:[{
&_index&:&schools&, &_type&:&school&, &_id&:&1&, &_score&:0.,
&_source&:{
&name&:&Central School&, &description&:&CBSE Affiliation&,
&street&:&Nagan&, &city&:&paprola&, &state&:&HP&, &zip&:&176115&,
&location&:[31..8380405], &fees&:2200,
&tags&:[&Senior Secondary&, &beautiful campus&], &rating&:&3.3&
范围查询此查询用于查找值的范围之间的值的对象。 为此,需要使用类似 -
gte - 大于和等于gt - 大于lte - 小于和等于lt - 小于
POST http://localhost:9200/schools*/_search
&rating&:{
&took&:31, &timed_out&:false, &_shards&:{&total&:10, &successful&:10, &failed&:0},
&total&:3, &max_score&:1.0, &hits&:[
&_index&:&schools&, &_type&:&school&, &_id&:&2&, &_score&:1.0,
&_source&:{
&name&:&Saint Paul School&, &description&:&ICSE Affiliation&,
&street&:&Dawarka&, &city&:&Delhi&, &state&:&Delhi&,
&zip&:&110075&, &location&:[28..0122136], &fees&:5000,
&tags&:[&Good Faculty&, &Great Sports&], &rating&:&4.5&
&_index&:&schools_gov&, &_type&:&school&, &_id&:&2&, &_score&:1.0,
&_source&:{
&name&:&Government School&, &description&:&State Board Affiliation&,
&street&:&Hinjewadi&, &city&:&Pune&, &state&:&MH&, &zip&:&411057&,
&location&:[18..6821995] &fees&:500,
&tags&:[&Great Sports&], &rating&:&4&
&_index&:&schools&, &_type&:&school&, &_id&:&3&, &_score&:1.0,
&_source&:{
&name&:&Crescent School&, &description&:&State Board Affiliation&,
&street&:&Tonk Road&, &city&:&Jaipur&, &state&:&RJ&, &zip&:&176114&,
&location&:[26..7923988], &fees&:2500,
&tags&:[&Well equipped labs&], &rating&:&4.5&
其他类型的期限级查询是 -
存在的查询 - 如果某一个字段有非空值。缺失的查询 - 这与存在查询完全相反,此查询搜索没有特定字段的对象或有空值的字段。通配符或正则表达式查询 - 此查询使用正则表达式来查找对象中的模式。
类型查询 - 具有特定类型的文档。 例如,
POST http://localhost:9200/schools*/_search
&type& : {
&value& : &school&
响应存在于指定的索引中的所有学校的JSON对象。
复合查询这些查询是通过使用如和,或,非和或等,用于不同索引或具有函数调用等的布尔运算符彼此合并的不同查询的集合。例如,
POST http://localhost:9200/schools*/_search
&filtered&:{
&state&:&UP&
&filter&:{
&rating&:{
&took&:16, &timed_out&:false, &_shards&:{&total&:10, &successful&:10, &failed&:0},
&hits&:{&total&:0, &max_score&:null, &hits&:[]}
连接查询这些查询用于包含多个映射或文档的位置。 连接查询有两种类型 -
嵌套查询这些查询处理嵌套映射(将在下一章阅读了解更多内容)。
has_child和has_parent查询
这些查询用于检索在查询中匹配的文档的子文档或父文档。 例如,
POST http://localhost:9200/tutorials/_search
&has_child& : {
&type& : &article&, &query& : {
&match& : {
&Text& : &This is article 1 of chapter 1&
&took&:21, &timed_out&:false, &_shards&:{&total&:5, &successful&:5, &failed&:0},
&total&:1, &max_score&:1.0, &hits&:[{
&_index&:&tutorials&, &_type&:&chapter&, &_id&:&1&, &_score&:1.0,
&_source&:{
&Text&:&this is chapter one&
地理查询这些查询处理地理位置和地理点。这些查询有助于查找任何位置附近的学校或任何其他地理对象。需要使用地理位置数据类型。 例如,
POST http://localhost:9200/schools*/_search
&filtered&:{
&filter&:{
&geo_distance&:{
&distance&:&100km&,
&location&:[32..649294]
&took&:6, &timed_out&:false, &_shards&:{&total&:10, &successful&:10, &failed&:0},
&total&:2, &max_score&:1.0, &hits&:[
&_index&:&schools&, &_type&:&school&, &_id&:&2&, &_score&:1.0,
&_source&:{
&name&:&Saint Paul School&, &description&:&ICSE Affiliation&,
&street&:&Dawarka&, &city&:&Delhi&, &state&:&Delhi&, &zip&:&110075&,
&location&:[28..0122136], &fees&:5000,
&tags&:[&Good Faculty&, &Great Sports&], &rating&:&4.5&
&_index&:&schools&, &_type&:&school&, &_id&:&1&, &_score&:1.0,
&_source&:{
&name&:&Central School&, &description&:&CBSE Affiliation&,
&street&:&Nagan&, &city&:&paprola&, &state&:&HP&, &zip&:&176115&,
&location&:[31..8380405], &fees&:2000,
&tags&:[&Senior Secondary&, &beautiful campus&], &rating&:&3.5&
注意 - 如果在执行上述示例时遇到异常,请将以下映射添加到索引。
&mappings&:{
&school&:{
&enabled&:true
&properties&:{
&location&:{
&type&:&geo_point&
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
上一篇:下一篇:
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加3个群。
Java技术群:
(人数:2000,等级:LV5,免费:否)
MySQL/SQL群:
(人数:2000,等级:LV5,免费:否)
大数据开发群:
(人数:2000,等级:LV5,免费:否)
Python技术群:
(人数:2000,等级:LV5,免费:否)
人工智能深度学习:
(人数:2000,等级:LV5,免费:否)
测试工程师(新群):
(人数:1000,等级:LV1,免费:是)
前端技术群(新群):
(人数:1000,等级:LV1,免费:是)
C/C++技术(新群):
(人数:1000,等级:LV1,免费:是)
Node.js技术(新群):
(人数:1000,等级:LV1,免费:是)
PostgreSQL数据库(新群):
(人数:1000,等级:LV1,免费:否)
Linux技术:
(人数:2000,等级:LV5,免费:否)
PHP开发者:
(人数:2000,等级:LV5,免费:是)
Oracle数据库:
(人数:2000,等级:LV5,免费:是)
C#/ASP.Net开发者:
(人数:2000,等级:LV5,免费:是)
数据分析师:
(人数:1000,等级:LV1,免费:是)R语言,Matlab语言等技术没有更多推荐了,
不良信息举报
举报内容:
ElasticSearch位置搜索
举报原因:
原文地址:
原因补充:
最多只允许输入30个字
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!

我要回帖

更多关于 mysql 合并查询结果 的文章

 

随机推荐