zoukankan      html  css  js  c++  java
  • elasticsearch java demo(新手)

    https://www.elastic.co/cn/downloads/?elektra=home&storm=hero  下载地址

     

     浏览器查看启动情况,出现一下内容说明服务已启动

     

    elasticsearchs是什么

    Elasticsearch 是一个分布式的免费开源搜索和分析引擎,适用于包括文本、数字、地理空间、结构化和非结构化数据等在内的所有类型的数据。Elasticsearch 在 Apache Lucene 的基础上开发而成,由 Elasticsearch N.V.(即现在的 Elastic)于 2010 年首次发布。Elasticsearch 以其简单的 REST 风格 API、分布式特性、速度和可扩展性而闻名,是 Elastic Stack 的核心组件;Elastic Stack 是一套适用于数据采集、扩充、存储、分析和可视化的免费开源工具。人们通常将 Elastic Stack 称为 ELK Stack(代指 Elasticsearch、Logstash 和 Kibana),目前 Elastic Stack 包括一系列丰富的轻量型数据采集代理,这些代理统称为 Beats,可用来向 Elasticsearch 发送数据。https://www.elastic.co/cn/what-is/elasticsearch

    elasticsearchs能帮我们干什么

    https://blog.csdn.net/ctwctw/article/details/105972029

    依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>7.6.1</version>
            </dependency>

    好了,先写个demo体验一下

    import lombok.Data;
    import org.springframework.data.elasticsearch.core.geo.GeoPoint;
    
    @Data
    public class Org {
        private Integer id;
        private String name;
        private GeoPoint location;
    
        public static final String LOCATION = "location";
    }

    ###注意引用的org.springframework.data.elasticsearch.core.geo.GeoPoint包,别引错了

    import com.example.es.entity.Org;
    import com.google.gson.Gson;
    import org.elasticsearch.action.index.IndexRequest;
    import org.elasticsearch.action.index.IndexResponse;
    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.client.indices.CreateIndexRequest;
    import org.elasticsearch.client.indices.CreateIndexResponse;
    import org.elasticsearch.common.geo.GeoDistance;
    import org.elasticsearch.common.unit.DistanceUnit;
    import org.elasticsearch.common.unit.TimeValue;
    import org.elasticsearch.common.xcontent.XContentType;
    import org.elasticsearch.index.query.QueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.search.builder.SearchSourceBuilder;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.data.elasticsearch.core.geo.GeoPoint;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.io.IOException;
    
    /**
     * @ClassName EsOrgController
     * @Description TODO
     * @Author DELL
     * @Date 2021/4/23 16:20
     **/
    @RestController
    public class EsOrgController {
        private Logger logger = LoggerFactory.getLogger(getClass());
        @Autowired
        @Qualifier("restHighLevelClient")
        private RestHighLevelClient highLevelClient;
    
    
        @GetMapping(value = "/es/create/index1/{name}")
        public String createIndex(@PathVariable(value = "name") String name) throws IOException {
            CreateIndexRequest request = new CreateIndexRequest(name);
            CreateIndexResponse response = highLevelClient.indices().create(request, RequestOptions.DEFAULT);
            logger.info(response.toString());
            return "success";
        }
    
        @PostMapping(value = "/es/document/create/Org")
        public String createUserDocument(@RequestBody Org org) throws IOException {
            IndexRequest indexRequest = new IndexRequest("org");
            indexRequest.timeout(TimeValue.timeValueSeconds(1));
            /**
             * 使用Gson --
             * GeoPoint传入时传经纬度 --- 使用fastjson会解析错误
             * by https://agentd.cn/archives/es-geopoint
             */
            indexRequest.source(new Gson().toJson(org), XContentType.JSON);
            IndexResponse indexResponse = highLevelClient.index(indexRequest, RequestOptions.DEFAULT);
            return indexResponse.toString();
        }
    
        /**
         * 根据经纬度查询
         * @param location
         * @return
         * @throws IOException
         */
        @GetMapping(value = "/es/document/OrgBylocation")
        public String getOrgByLocation(@RequestBody GeoPoint location) throws IOException {
            SearchRequest searchRequest = new SearchRequest("org");
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            double lat = location.getLat();
            double lon = location.getLon();
            QueryBuilder queryBuilder = QueryBuilders.geoDistanceQuery(Org.LOCATION)
                    .distance(3, DistanceUnit.KILOMETERS).point(lat, lon).geoDistance(GeoDistance.ARC);
            sourceBuilder.query(queryBuilder);
            SearchResponse search = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            System.out.println(search);
            return search.toString();
        }
    
    
    }

    先看一下GeoPoint  

    public class GeoPoint {
        //纬度
        private double lat;
        //经度
        private double lon;
    
        private GeoPoint() {
        }
    
        public GeoPoint(double latitude, double longitude) {
            this.lat = latitude;
            this.lon = longitude;
        }
    
        public double getLat() {
            return this.lat;
        }
    
        public double getLon() {
            return this.lon;
        }
    
        public static GeoPoint fromPoint(Point point) {
            return new GeoPoint(point.getX(), point.getY());
        }
    
        public static Point toPoint(GeoPoint point) {
            return new Point(point.getLat(), point.getLon());
        }
    
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            } else if (o != null && this.getClass() == o.getClass()) {
                GeoPoint geoPoint = (GeoPoint)o;
                return Double.compare(geoPoint.lat, this.lat) == 0 && Double.compare(geoPoint.lon, this.lon) == 0;
            } else {
                return false;
            }
        }
    
        public int hashCode() {
            return Objects.hash(new Object[]{this.lat, this.lon});
        }
    
        public String toString() {
            return "GeoPoint{lat=" + this.lat + ", lon=" + this.lon + '}';
        }
    }

     

     浏览器elasticsearch-head插件

  • 相关阅读:
    Spring 源码解析之 XML 解析 到 BeanDefinition
    Mybatis 流程解析 之 mapper映射
    Mybatis 流程解析 之 配置加载
    (二) mybatis 源码分析之日志
    CNC 1320
    CNC 1815
    linux中jdk安装,配置环境变量
    ssm配置文件,方便以后使用
    01 标识符,基本数据类型,自动类型转换与强制类型转换,自增与自减,三元运算符,键盘录入
    N46期第二十一周作业
  • 原文地址:https://www.cnblogs.com/Iron-1995/p/14694884.html
Copyright © 2011-2022 走看看