建设工程合同通用条款范本2017seo是什么字
web/
2025/9/28 1:40:52/
文章来源:
建设工程合同通用条款范本2017,seo是什么字,服装网站建设的技术可行性,支付网站建设费的会计分录1.Redis,MongoDB,Elasticsearch实现地理位置查询比较
1.1 Redis:
优点#xff1a;Redis提供了地理空间索引功能#xff0c;可以通过Geo数据类型进行地理位置查询。这使得Redis在处理地理位置查询时非常高效。
缺点#xff1a; Redis的地理空间索引功能相对简单#xff0…1.Redis,MongoDB,Elasticsearch实现地理位置查询比较
1.1 Redis:
优点Redis提供了地理空间索引功能可以通过Geo数据类型进行地理位置查询。这使得Redis在处理地理位置查询时非常高效。
缺点 Redis的地理空间索引功能相对简单只能支持二维平面坐标系经纬度的查询对于三维坐标系或者不规则地理区域的查询支持不够好。 功能有限Redis的地理位置查询功能相对简单仅支持基本的距离计算、范围查询等操作无法满足复杂的空间查询需求。 存储容量限制由于Redis数据存储在内存中其存储容量受限于物理内存大小对于大规模地理位置数据可能需要进行分片或其他优化策略。 扩展性受限Redis对于数据的扩展能力有限不如Elasticsearch那样容易水平扩展以适应规模的增长。
使用场景适用于需要快速查询地理位置信息的场景小型应用并且对于快速插入和查询地理位置数据有较高的实时性要求可以考虑使用Redis geo。
1.2 MongoDB
优点 灵活性好MongoDB支持多种地理位置查询操作包括点查询、范围查询和多边形查询等。 数据结构简单MongoDB的文档型结构非常适合存储地理位置数据容易理解和使用。 高可用性MongoDB提供了复制集和分片等机制来确保数据的高可用性和扩展性。 然而MongoDB 2d索引实现地理位置查询也存在一些缺点
性能相对较差相比Elasticsearch在处理大规模的地理位置查询时MongoDB的性能可能会受到限制。 功能相对简单MongoDB的地理位置查询功能较为基础相比Elasticsearch可能缺乏某些高级查询功能。 不支持部分地理位置操作例如MongoDB不支持直接计算两个地理位置之间的距离。
1.3 Elasticsearch geo
使用Elasticsearch geo实现地理位置查询的优点
高性能Elasticsearch是一种搜索引擎使用geo点的经纬度数据可以快速进行空间查询和过滤具有较高的查询效率。 灵活性Elasticsearch提供了丰富的地理位置查询功能例如可以根据距离、范围及其他条件进行查询和排序。 可扩展性Elasticsearch可以通过分片和副本来实现水平扩展以应对大规模的地理位置数据查询需求。
使用Elasticsearch geo实现地理位置查询的缺点 学习成本学习和配置Elasticsearch需要花费一定的时间和精力。 依赖性使用Elasticsearch需要安装和维护Elasticsearch服务这可能增加系统依赖和部署复杂性。 数据存储限制Elasticsearch适用于小到中等大小的数据集对于大量地理位置数据可能需要额外的硬件资源和优化工作。
Elasticsearch为用户提供了基于地理位置的搜索功能。它主要支持两种类型的地理查询一种是地理点(geo_point),即经纬度查询另一种是地理形状查询(geo_shape),即支持点、线、圆形和多边形查询等
2.Elasticsearch geo地理位置数据类型
在Elasticsearch中存在两种地理位置数据类型geo_point和geo_shape。
geo_point这是最基本的地理位置类型通常用于表示一个二维坐标点经度和纬度。可以计算落在某个矩形内的点、以某个点为半径(圆)的点、某个多边形内的点等。此外geo_point还可以用于排序、聚合等操作。 geo_shape这种数据类型表示一个复杂的图形使用的是GeoJSON的格式。它可以表达一块地理区域区域的形状可以是任意多边形也可以是点、线、面、多点、多线、多面等几何类型。然而这种数据类型不能进行排序操作。
2.基于geo_point类型实现查询加油站案例
elasticsearch 版本7.12.1
2.1 springboot集成elasticsearch dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-elasticsearch/artifactIdversion2.5.11/version/dependencydependencygroupIdorg.locationtech.jts/groupIdartifactIdjts-core/artifactIdversion1.18.1/version/dependencydependencygroupIdorg.locationtech.spatial4j/groupIdartifactIdspatial4j/artifactIdversion0.8/version/dependency配置文件
# es 服务地址
elasticsearch.host127.0.0.1
# es 服务端口
elasticsearch.port9200
# 配置日志级别
logging.level.org.springframework.data.elasticsearch.coredebug
logging.level.org.springframework.data.elasticsearch.client.WIREtrace配置类
ConfigurationProperties(prefix elasticsearch)
Configuration
Data
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {private String host ;private Integer port ;//重写父类方法Overridepublic RestHighLevelClient elasticsearchClient() {RestClientBuilder builder RestClient.builder(new HttpHost(host, port));RestHighLevelClient restHighLevelClient newRestHighLevelClient(builder);return restHighLevelClient;}
}
测试实体类
ApiModel
Data
Builder
AllArgsConstructor
NoArgsConstructor
ToString
Document(indexName stationcc, shards 3, replicas 1)
public class ChargingStationDTO {//必须有 id,这里的 id 是全局唯一的标识等同于 es 中的_idIdApiModelProperty(value id, example 111111111111)private Long baseId;/*** type : 字段数据类型* analyzer : 分词器类型* index : 是否索引(默认:true)* Keyword : 短语,不进行分词*/ApiModelProperty(value 加油站ID, example 111111111111)Field(type FieldType.Keyword)private String stationId;ApiModelProperty(value 运营商ID, example 395815801)Field(type FieldType.Keyword)private String operatorId;ApiModelProperty(value 加油站名称, example 测试加油站)Field(type FieldType.Keyword)private String stationName;ApiModelProperty(value 运营商名称, example 测试)Field(type FieldType.Keyword)private String operatorName;GeoPointFieldApiModelProperty(value 经纬度)private GeoPoint location; Field(type FieldType.Keyword)ApiModelProperty(value 详细地址, example 山东路154号)private String address;ApiModelProperty(value 距离, example 1.0)private double distance;
}
初始化数据 Testpublic void saveAll() {//起点 111.000,31.000//终点 121.000,31.000//( 121 , 31 ) - ( 111 , 31 ) 之间的距离为 952.8062737420901 km//96-121,23-40ListChargingStationDTO chargingStationDTOList new ArrayList();ListString stringList CollUtil.newArrayList(招式, 王五, 基于, 好好, 电动, 反复, 第三十, 十三点, 但是);for (int i 2000; i 450000; i) {ChargingStationDTO chargingStationDTO new ChargingStationDTO();chargingStationDTO.setBaseId(Long.valueOf(i));chargingStationDTO.setStationId(Long.valueOf(i).toString());chargingStationDTO.setOperatorId(Long.valueOf(i).toString());chargingStationDTO.setStationName(RandomUtil.randomEleList(stringList, 1).get(0));chargingStationDTO.setAddress(地址 i);//经度范围是0-180°,纬度范围是0-90°//纬度double lat RandomUtil.randomDouble(23.000, 40.000, 3, RoundingMode.DOWN);//经度double lon RandomUtil.randomDouble(96.000, 121.000, 3, RoundingMode.DOWN);chargingStationDTO.setLocation(new GeoPoint(lat, lon));chargingStationDTOList.add(chargingStationDTO);if (chargingStationDTOList.size() 1000) {chargingStationDao.saveAll(chargingStationDTOList);chargingStationDTOList.clear();System.out.println(插入1000,ii);}}}
2.2 查询附近加油站圆形查询
请求参数
ApiModel
Data
Builder
AllArgsConstructor
NoArgsConstructor
public class ChargingStationNearbySearchDTO {ApiModelProperty(value id, example 1111111111)private Long baseId;ApiModelProperty(value 加油站名称, example 测试加油站)private String stationName;ApiModelProperty(value 经度)NotNull(message 经度不能为空)private Double lon;ApiModelProperty(value 纬度)NotNull(message 纬度不能为空)private Double lat;ApiModelProperty(value 查找半径)private int radius;ApiModelProperty(value page, example 1)private Integer page;ApiModelProperty(value pageSize, example 100)private Integer pageSize;
} PostMapping(/nearby)ApiOperation(value 查询附近加油站)public ResponseChargingStationVO nearbySearch(RequestBody Valid Validated ChargingStationNearbySearchDTO searchDTO) {String fieldName location;// NativeSearchQuery实现了SearchQuery接口NativeSearchQueryBuilder nativeSearchQueryBuilder new NativeSearchQueryBuilder();// 分页PageRequest pageRequest PageRequest.of(searchDTO.getPage() - 1, searchDTO.getPageSize());nativeSearchQueryBuilder.withPageable(pageRequest);// 定义bool查询BoolQueryBuilder boolQueryBuilder new BoolQueryBuilder();//https://blog.csdn.net/icanlove/article/details/126425788?spm1001.2101.3001.6661.1utm_mediumdistribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-126425788-blog-120678401.235%5Ev38%5Epc_relevant_default_basedepth_1-utm_sourcedistribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-126425788-blog-120678401.235%5Ev38%5Epc_relevant_default_baseutm_relevant_index1//使用 minimum_should_match 选项至少匹配一项should子句。if (StringUtils.isNotBlank(searchDTO.getStationName()) || ObjectUtils.isNotEmpty(searchDTO.getBaseId())) {if (StringUtils.isNotBlank(searchDTO.getStationName())) {// //左右模糊查询,其中fuzziness的参数作用是在查询时es动态的将查询关键词前后增加或者删除一个词然后进行匹配QueryBuilder queryBuilder QueryBuilders.fuzzyQuery(stationName, searchDTO.getStationName()).fuzziness(Fuzziness.ONE);boolQueryBuilder.must(queryBuilder);}if (ObjectUtils.isNotEmpty(searchDTO.getBaseId())) {// //关键字不支持分词QueryBuilder queryBuilder QueryBuilders.termQuery(baseId, searchDTO.getBaseId());boolQueryBuilder.must(queryBuilder);}}// geo查询定义中心点指定查询范围GeoDistanceQueryBuilder geoDistanceQueryBuilder new GeoDistanceQueryBuilder(fieldName);geoDistanceQueryBuilder.point(searchDTO.getLat(), searchDTO.getLon());geoDistanceQueryBuilder.distance(searchDTO.getRadius(), DistanceUnit.METERS);boolQueryBuilder.must(geoDistanceQueryBuilder);// 外部 bool 过滤器BoolQueryBuilder queryBuilder new BoolQueryBuilder();queryBuilder.filter(boolQueryBuilder);nativeSearchQueryBuilder.withQuery(queryBuilder);// 按照距离升序GeoDistanceSortBuilder geoDistanceSortBuilder new GeoDistanceSortBuilder(fieldName, searchDTO.getLat(), searchDTO.getLon());geoDistanceSortBuilder.unit(DistanceUnit.METERS); //距离单位geoDistanceSortBuilder.order(SortOrder.ASC); //升序nativeSearchQueryBuilder.withSort(geoDistanceSortBuilder);NativeSearchQuery nativeSearchQuery nativeSearchQueryBuilder.build();DslLogUtil.log(elasticsearchOperations, nativeSearchQuery);SearchHitsChargingStationDTO searchHits elasticsearchOperations.search(nativeSearchQuery, ChargingStationDTO.class);log.info(响应数据:{}, LogUtil.getLogJson(searchHits));ListChargingStationDTO chargingStationDTOList null;if (CollectionUtil.isNotEmpty(searchHits.getSearchHits())) {chargingStationDTOList searchHits.getSearchHits().stream().map(o - {// 计算两点距离//关于GeoDistance.ARC和GeoDistance.PLANE前者比后者计算起来要慢但精确度要比后者高具体区别可以看。double distance GeoDistance.ARC.calculate(o.getContent().getLocation().getLat(), o.getContent().getLocation().getLon(), searchDTO.getLat(), searchDTO.getLon(), DistanceUnit.KILOMETERS);ChargingStationDTO chargingStationDTO o.getContent();chargingStationDTO.setDistance(distance);return chargingStationDTO;}).collect(Collectors.toList());}int count CollectionUtils.isEmpty(chargingStationDTOList) ? 0 : chargingStationDTOList.size();return Response.success(ChargingStationVO.builder().positions(chargingStationDTOList).count(count).build());}
2.3 查询附近加油站 geo_bounding_box 矩形查询
geo_bounding_box语法又称为地理坐标盒模型在当前语法中只需选择一个矩阵范围(输入矩阵的左上角的顶点地理坐标和矩阵的右上角的顶点地理坐标构建成为一个矩阵)即可计算出当前矩阵中符合条件的元素
/*** 给定两个坐标通过这两个坐标形成对角线* 平行于地球经纬度从而得到的一个矩阵。* 采用geo_bounding_box语法可以得到坐落于当前矩阵中的元素的信息** param searchDTO* return*/PostMapping(/box/query)ApiOperation(value 矩形查询附近加油站)public ResponseChargingStationVO boxQuery(RequestBody Valid Validated ChargingStationSearchDTO searchDTO) {// NativeSearchQuery实现了SearchQuery接口NativeSearchQueryBuilder nativeSearchQueryBuilder new NativeSearchQueryBuilder();// 分页PageRequest pageRequest PageRequest.of(searchDTO.getPage() - 1, searchDTO.getPageSize());nativeSearchQueryBuilder.withPageable(pageRequest);// 定义bool查询BoolQueryBuilder boolQueryBuilder new BoolQueryBuilder();//https://blog.csdn.net/icanlove/article/details/126425788?spm1001.2101.3001.6661.1utm_mediumdistribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-126425788-blog-120678401.235%5Ev38%5Epc_relevant_default_basedepth_1-utm_sourcedistribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-126425788-blog-120678401.235%5Ev38%5Epc_relevant_default_baseutm_relevant_index1//使用 minimum_should_match 选项至少匹配一项should子句。if (StringUtils.isNotBlank(searchDTO.getStationName()) || ObjectUtils.isNotEmpty(searchDTO.getBaseId())) {if (StringUtils.isNotBlank(searchDTO.getStationName())) {// //左右模糊查询,其中fuzziness的参数作用是在查询时es动态的将查询关键词前后增加或者删除一个词然后进行匹配QueryBuilder queryBuilder QueryBuilders.fuzzyQuery(stationName, searchDTO.getStationName()).fuzziness(Fuzziness.ONE);boolQueryBuilder.must(queryBuilder);}if (ObjectUtils.isNotEmpty(searchDTO.getBaseId())) {// //关键字不支持分词QueryBuilder queryBuilder QueryBuilders.termQuery(baseId, searchDTO.getBaseId());boolQueryBuilder.must(queryBuilder);}}//给定两个坐标通过这两个坐标形成对角线// 平行于地球经纬度从而得到的一个矩阵。// 采用geo_bounding_box语法可以得到坐落于当前矩阵中的元素的信息// 构造左上点坐标GeoPoint topLeft new GeoPoint(searchDTO.getPositions().get(0).getLat(), searchDTO.getPositions().get(0).getLon());// 构造右下点坐标GeoPoint bottomRight new GeoPoint(searchDTO.getPositions().get(1).getLat(), searchDTO.getPositions().get(1).getLon());GeoBoundingBoxQueryBuilder geoBoundingBoxQueryBuilder new GeoBoundingBoxQueryBuilder(location).setCorners(topLeft, bottomRight);boolQueryBuilder.must(geoBoundingBoxQueryBuilder);// 外部 bool 过滤器BoolQueryBuilder queryBuilder new BoolQueryBuilder();queryBuilder.filter(boolQueryBuilder);nativeSearchQueryBuilder.withQuery(queryBuilder);NativeSearchQuery nativeSearchQuery nativeSearchQueryBuilder.build();DslLogUtil.log(elasticsearchOperations, nativeSearchQuery);SearchHitsChargingStationDTO searchHits elasticsearchOperations.search(nativeSearchQuery, ChargingStationDTO.class);log.info(响应数据:{}, LogUtil.getLogJson(searchHits));ListChargingStationDTO chargingStationDTOList null;if (CollectionUtil.isNotEmpty(searchHits.getSearchHits())) {chargingStationDTOList searchHits.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());}int count CollectionUtils.isEmpty(chargingStationDTOList) ? 0 : chargingStationDTOList.size();return Response.success(ChargingStationVO.builder().positions(chargingStationDTOList).count(count).build());}DSL 请求体
{from: 0,size: 100,query: {bool: {filter: [{bool: {must: [{fuzzy: {stationName: {value: 第三十,fuzziness: 1,prefix_length: 0,max_expansions: 50,transpositions: true,boost: 1.0}}}, {geo_bounding_box: {location: {top_left: [120.91224, 30.84623],bottom_right: [120.93743, 30.8245]},validation_method: STRICT,type: MEMORY,ignore_unmapped: false,boost: 1.0}}],adjust_pure_negative: true,boost: 1.0}}],adjust_pure_negative: true,boost: 1.0}},version: true,explain: false
}响应体
{code: 200,message: 成功,data: {count: 2,positions: [{baseId: 431843,stationId: 431843,operatorId: 431843,stationName: 好好,operatorName: null,location: {lat: 30.833,lon: 120.934,geohash: wtmzruvrnry1,fragment: true},address: 地址431843,distance: 0},{baseId: 114960,stationId: 114960,operatorId: 114960,stationName: 第三十,operatorName: null,location: {lat: 30.84,lon: 120.919,geohash: wtmzrw680btm,fragment: true},address: 地址114960,distance: 0}]},extraData: {}
}2.4 多边形查询附近加油站geo-polygon-多边形查询
ES的geo_polygon语法可以通过指定多个坐标点从而构成一个多边形然后从当前多边形中召回坐落其中的元素进行召回在当前语法中最少需要3个坐标从而构成一个多边形 PostMapping(/polygon/query)ApiOperation(value 多边形查询附近加油站)public ResponseChargingStationVO polygonQuery(RequestBody Valid Validated ChargingStationSearchDTO searchDTO) throws IOException {// NativeSearchQuery实现了SearchQuery接口NativeSearchQueryBuilder nativeSearchQueryBuilder new NativeSearchQueryBuilder();// 分页PageRequest pageRequest PageRequest.of(searchDTO.getPage() - 1, searchDTO.getPageSize());nativeSearchQueryBuilder.withPageable(pageRequest);// 定义bool查询BoolQueryBuilder boolQueryBuilder new BoolQueryBuilder();//https://blog.csdn.net/icanlove/article/details/126425788?spm1001.2101.3001.6661.1utm_mediumdistribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-126425788-blog-120678401.235%5Ev38%5Epc_relevant_default_basedepth_1-utm_sourcedistribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-126425788-blog-120678401.235%5Ev38%5Epc_relevant_default_baseutm_relevant_index1//使用 minimum_should_match 选项至少匹配一项should子句。if (StringUtils.isNotBlank(searchDTO.getStationName()) || ObjectUtils.isNotEmpty(searchDTO.getBaseId())) {if (StringUtils.isNotBlank(searchDTO.getStationName())) {// //左右模糊查询,其中fuzziness的参数作用是在查询时es动态的将查询关键词前后增加或者删除一个词然后进行匹配QueryBuilder queryBuilder QueryBuilders.fuzzyQuery(stationName, searchDTO.getStationName()).fuzziness(Fuzziness.ONE);boolQueryBuilder.must(queryBuilder);}if (ObjectUtils.isNotEmpty(searchDTO.getBaseId())) {// //关键字不支持分词QueryBuilder queryBuilder QueryBuilders.termQuery(baseId, searchDTO.getBaseId());boolQueryBuilder.must(queryBuilder);}}//可以通过指定多个坐标点从而构成一个多边形//然后从当前多边形中召回坐落其中的元素进行召回//在当前语法中最少需要3个坐标从而构成一个多边形// 创建多边形几何对象CoordinatesBuilder coordinatesBuilder new CoordinatesBuilder();for (GpsListDTO gpsListDTO : searchDTO.getPositions()) {coordinatesBuilder.coordinate(gpsListDTO.getLon(), gpsListDTO.getLat());}PolygonBuilder pb new PolygonBuilder(coordinatesBuilder);GeoShapeQueryBuilder geoShapeQueryBuilder QueryBuilders.geoShapeQuery(location, pb.buildGeometry());// intersects - 查询的形状与索引的形状有重叠默认, 即图形有交集则匹配。//disjoint - 查询的形状与索引的形状完全不重叠。//within - 查询的形状包含索引的形状。//CONTAINS将返回其geo_shape字段包含查询中指定的几何形状的所有文档。//within与CONTAINS的区别// 它们是反比关系A包含B,B在A内.// A是查询中的形状而B是文档中的形状。//WITHIN表示A包含B A.contains(B) True// CONTAINS表示B包含A B.within(A) TruegeoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);boolQueryBuilder.must(geoShapeQueryBuilder);// 外部 bool 过滤器BoolQueryBuilder queryBuilder new BoolQueryBuilder();queryBuilder.filter(boolQueryBuilder);nativeSearchQueryBuilder.withQuery(queryBuilder);NativeSearchQuery nativeSearchQuery nativeSearchQueryBuilder.build();DslLogUtil.log(elasticsearchOperations, nativeSearchQuery);SearchHitsChargingStationDTO searchHits elasticsearchOperations.search(nativeSearchQuery, ChargingStationDTO.class);log.info(响应数据:{}, LogUtil.getLogJson(searchHits));ListChargingStationDTO chargingStationDTOList null;if (CollectionUtil.isNotEmpty(searchHits.getSearchHits())) {chargingStationDTOList searchHits.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());}int count CollectionUtils.isEmpty(chargingStationDTOList) ? 0 : chargingStationDTOList.size();return Response.success(ChargingStationVO.builder().positions(chargingStationDTOList).count(count).build());}DSL 请求体
{from: 0,size: 100,query: {bool: {filter: [{bool: {must: [{fuzzy: {stationName: {value: 好好,fuzziness: 1,prefix_length: 0,max_expansions: 50,transpositions: true,boost: 1.0}}}, {geo_shape: {location: {shape: {type: Polygon,coordinates: [[[120.92696, 30.83932],[120.91964, 30.82868],[120.95907, 30.81838],[120.96842, 30.83525],[120.94369, 30.84345],[120.92696, 30.83932]]]},relation: intersects},ignore_unmapped: false,boost: 1.0}}],adjust_pure_negative: true,boost: 1.0}}],adjust_pure_negative: true,boost: 1.0}},version: true,explain: false
}响应体
{empty: false,maxScore: 0.0,searchHits: [{content: {address: 地址431843,baseId: 431843,distance: 0.0,location: {fragment: true,geohash: wtmzruvrnry1,lat: 30.833,lon: 120.934},operatorId: 431843,stationId: 431843,stationName: 好好},highlightFields: {},id: 431843,index: stationcc,innerHits: {},matchedQueries: [],score: 0.0,sortValues: []}],totalHits: 1,totalHitsRelation: EQUAL_TO
}2.5 查询沿途加油站一次查询多个圆点 PostMapping(/route)ApiOperation(value 查询沿途加油站)public ResponseChargingStationVO routeSearch(RequestBody Valid Validated ChargingStationSearchDTO searchDTO) {String fieldName location;// NativeSearchQuery实现了SearchQuery接口NativeSearchQueryBuilder nativeSearchQueryBuilder new NativeSearchQueryBuilder();// 分页PageRequest pageRequest PageRequest.of(searchDTO.getPage() - 1, searchDTO.getPageSize());nativeSearchQueryBuilder.withPageable(pageRequest);// 定义bool查询BoolQueryBuilder boolQueryBuilder new BoolQueryBuilder();//https://blog.csdn.net/icanlove/article/details/126425788?spm1001.2101.3001.6661.1utm_mediumdistribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-126425788-blog-120678401.235%5Ev38%5Epc_relevant_default_basedepth_1-utm_sourcedistribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-126425788-blog-120678401.235%5Ev38%5Epc_relevant_default_baseutm_relevant_index1//使用 minimum_should_match 选项至少匹配一项should子句。boolQueryBuilder.minimumShouldMatch(1);if (StringUtils.isNotBlank(searchDTO.getStationName()) || ObjectUtils.isNotEmpty(searchDTO.getBaseId())) {if (StringUtils.isNotBlank(searchDTO.getStationName())) {// //左右模糊查询,其中fuzziness的参数作用是在查询时es动态的将查询关键词前后增加或者删除一个词然后进行匹配QueryBuilder queryBuilder QueryBuilders.fuzzyQuery(stationName, searchDTO.getStationName()).fuzziness(Fuzziness.ONE);boolQueryBuilder.must(queryBuilder);}if (ObjectUtils.isNotEmpty(searchDTO.getBaseId())) {// //关键字不支持分词QueryBuilder queryBuilder QueryBuilders.termQuery(baseId, searchDTO.getBaseId());boolQueryBuilder.must(queryBuilder);}}if (CollectionUtil.isNotEmpty(searchDTO.getPositions())) {boolQueryBuilder.minimumShouldMatch(1);for (GpsListDTO position : searchDTO.getPositions()) {// geo查询定义中心点指定查询范围GeoDistanceQueryBuilder geoDistanceQueryBuilder new GeoDistanceQueryBuilder(fieldName);geoDistanceQueryBuilder.point(position.getLat(), position.getLon());geoDistanceQueryBuilder.distance(searchDTO.getRadius(), DistanceUnit.METERS);boolQueryBuilder.should(geoDistanceQueryBuilder);}}// 外部 bool 过滤器
// Elasticsearch 查询条件和过滤条件的区别
// Elasticsearch中的查询条件和过滤条件都是用于搜索和过滤文档的条件但它们之间有一些区别。
// 查询条件是用于计算文档相关度得分的条件它会将所有符合条件的文档按照相关度得分从高到低排序并返回前N个文档。查询条件可以使用各种类型的查询如match、term、range、bool等。查询条件会计算每个文档的相关度得分因此查询条件可以用于搜索和排序。
// 过滤条件是用于过滤文档的条件它会将所有符合条件的文档返回但不会计算相关度得分。过滤条件可以使用各种类型的过滤器如term、range、bool、geo_distance等。过滤条件不会计算相关度得分因此过滤条件可以用于过滤和聚合。
// 查询条件和过滤条件的区别在于查询条件会计算每个文档的相关度得分而过滤条件不会计算得分。因此如果只需要过滤文档而不需要计算得分应该使用过滤条件。另外过滤条件可以缓存结果提高查询性能而查询条件不能缓存结果。
// 需要注意的是查询条件和过滤条件都可以使用bool查询和bool过滤器来组合多个条件。bool查询和bool过滤器都是用于组合多个查询或过滤器的逻辑运算符可以使用must、should、must_not三个子句来组合多个查询或过滤器。BoolQueryBuilder queryBuilder new BoolQueryBuilder();queryBuilder.filter(boolQueryBuilder);nativeSearchQueryBuilder.withQuery(queryBuilder);NativeSearchQuery nativeSearchQuery nativeSearchQueryBuilder.build();DslLogUtil.log(elasticsearchOperations, nativeSearchQuery);SearchHitsChargingStationDTO searchHits elasticsearchOperations.search(nativeSearchQuery, ChargingStationDTO.class);log.info(响应数据:{}, LogUtil.getLogJson(searchHits));ListChargingStationDTO chargingStationDTOList null;if (CollectionUtil.isNotEmpty(searchHits.getSearchHits())) {chargingStationDTOList searchHits.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());}int count CollectionUtils.isEmpty(chargingStationDTOList) ? 0 : chargingStationDTOList.size();return Response.success(ChargingStationVO.builder().positions(chargingStationDTOList).count(count).build());}请求DSL语句
{from: 0,size: 10000,query: {bool: {filter: [{bool: {must: [{fuzzy: {stationName: {value: 王五,fuzziness: 1,prefix_length: 0,max_expansions: 50,transpositions: true,boost: 1.0}}}],should: [{geo_distance: {location: [114.7, 31.0],distance: 10000.0,distance_type: arc,validation_method: STRICT,ignore_unmapped: false,boost: 1.0}}, {geo_distance: {location: [116.935, 31.0],distance: 10000.0,distance_type: arc,validation_method: STRICT,ignore_unmapped: false,boost: 1.0}}, {geo_distance: {location: [117.261, 31.0],distance: 10000.0,distance_type: arc,validation_method: STRICT,ignore_unmapped: false,boost: 1.0}}, {geo_distance: {location: [116.569, 31.0],distance: 10000.0,distance_type: arc,validation_method: STRICT,ignore_unmapped: false,boost: 1.0}}, {geo_distance: {location: [117.639, 31.0],distance: 10000.0,distance_type: arc,validation_method: STRICT,ignore_unmapped: false,boost: 1.0}}, {geo_distance: {location: [119.236, 31.0],distance: 10000.0,distance_type: arc,validation_method: STRICT,ignore_unmapped: false,boost: 1.0}}],adjust_pure_negative: true,minimum_should_match: 1,boost: 1.0}}],adjust_pure_negative: true,boost: 1.0}},version: true,explain: false
}响应数据:
{empty: false,maxScore: 0.0,searchHits: [{content: {address: 地址4031,baseId: 4031,distance: 0.0,location: {fragment: true,geohash: wtkzbygzuwxz,lat: 30.932,lon: 119.218},operatorId: 4031,stationId: 4031,stationName: 王五},highlightFields: {},id: 4031,index: stationcc,innerHits: {},matchedQueries: [],score: 0.0,sortValues: []}, {content: {address: 地址26708,baseId: 26708,distance: 0.0,location: {fragment: true,geohash: wte2df6z32vx,lat: 31.039,lon: 117.195},operatorId: 26708,stationId: 26708,stationName: 王五},highlightFields: {},id: 26708,index: stationcc,innerHits: {},matchedQueries: [],score: 0.0,sortValues: []}, {content: {address: 地址156487,baseId: 156487,distance: 0.0,location: {fragment: true,geohash: wt988d3zmbcx,lat: 31.039,lon: 114.634},operatorId: 156487,stationId: 156487,stationName: 王五},highlightFields: {},id: 156487,index: stationcc,innerHits: {},matchedQueries: [],score: 0.0,sortValues: []}, {content: {address: 地址131631,baseId: 131631,distance: 0.0,location: {fragment: true,geohash: wtdb78u6echc,lat: 30.986,lon: 116.527},operatorId: 131631,stationId: 131631,stationName: 王五},highlightFields: {},id: 131631,index: stationcc,innerHits: {},matchedQueries: [],score: 0.0,sortValues: []}, {content: {address: 地址265815,baseId: 265815,distance: 0.0,location: {fragment: true,geohash: wte8ks47qs3x,lat: 31.004,lon: 117.623},operatorId: 265815,stationId: 265815,stationName: 王五},highlightFields: {},id: 265815,index: stationcc,innerHits: {},matchedQueries: [],score: 0.0,sortValues: []}],totalHits: 16,totalHitsRelation: EQUAL_TO
}打印完整DSL语句工具类
Slf4j
public class DslLogUtil {public static void log(ElasticsearchOperations elasticsearchOperations, NativeSearchQuery nativeSearchQuery) {if (elasticsearchOperations instanceof ElasticsearchRestTemplate) {try {ElasticsearchRestTemplate elasticsearchRestTemplate (ElasticsearchRestTemplate) elasticsearchOperations;Method searchRequest ReflectionUtils.findMethod(Class.forName(org.springframework.data.elasticsearch.core.RequestFactory), searchRequest, Query.class, Class.class, IndexCoordinates.class);searchRequest.setAccessible(true);Object o ReflectionUtils.invokeMethod(searchRequest, elasticsearchRestTemplate.getRequestFactory(), nativeSearchQuery, ChargingStationDTO.class, elasticsearchRestTemplate.getIndexCoordinatesFor(ChargingStationDTO.class));Field source ReflectionUtils.findField(Class.forName(org.elasticsearch.action.search.SearchRequest), source);source.setAccessible(true);Object s ReflectionUtils.getField(source, o);log.info(请求DSL语句:{}, s);} catch (ClassNotFoundException e) {e.printStackTrace();}}}}
参考 https://www.kancloud.cn/yiyanan/elasticsearch_7_6/1670492
https://www.kancloud.cn/apachecn/elasticsearch-doc-zh/1945207
https://learnku.com/docs/elasticsearch73/7.3/5210-geo-distance-aggregation/8043
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/83071.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!