代码工厂 - IT编程入门网 - 码工厂 - 码农教程旨在为码农们提供免费的IT编程入门教程和编程学习平台,本码农网涵盖php、java、mysql、html、css、javascript、jquery等流行的编程入门教程。

JavaScript从入门到精通 纯笔记(day04)--函数1

一、函数

1、函数概述
        实际上就是多行代码的抽取(多行代码会构成特定的功能)

2、、函数的优点
        减少冗余代码(重复的代码放在函数里来封装起来)

        提高了代码的可维护性以及可阅读性

3、函数分类
        系统函数    window里面的所有函数都属于系统函数(console.log(),alert())

        内置函数    所有的内置对象里面的函数都叫内置函数(Math.pow()...)

        自定义函数    自己定义的函数

注:内置和系统函数更关注使用,而自定义函数关注定义以及使用

4、自定义函数的定义以及调用
        1)、使用function关键词定义匿名函数(没有名字的函数)

            例:

                    function(){

                                consolo.log(' ')

                        }

        注:匿名函数直接调用,让其他事件去自动调用,声明比较少,没有复用价值

                    自执行函数:

                        (function(){

                                console.log(' ')

                            }()

                注:自执行函数不能传参,函数调用后面带(),自执行的匿名函数,没有复用价值,只能用一次

        2)、使用function关键词定义具名函数(有名字的函数)

                 第一种:   function 函数名(形参1,形参2....){

                                函数体(代码块)

                    }

                    变种的具名函数:

                  第二种:  var     函数名 = function(){

                                代码块

                    }

                    注:对于调用速度来说,第一种更快,因为function和var在预解析阶段就会声                                                                  明,而var关键词修饰的变量在预解析阶段是不会赋值的

        3)、使用new Function声明(new后面的Function首字母一定要大写)

                var    函数名    =    new    Function('    形参1,形参2....    '    '    函数内容题    ')

                预解析:
                        预解析会做的事情:1、它会在声明对应function和var关键词修饰的变量开辟内存

                                                         2、对应的function的内存空间开辟以后它会将对应的代码块放在里面,等待调用

                                                         3、var修饰的关键词,只会开辟一个空间,并不会进行复制(默认undefined)

return关键词
return返回对应的数据,调用return后,后面的内容将不再执行,如果没有return,默认返回undefined

函数执行过程
1、方法栈:也称执行栈,是吧对应开辟的function内存里面的代码块丢给他去执行

2、执行栈会自动去执行对应的方法,执行完返回对应结果

作用域
当前一个变量的作用范围,分为局部,全局作用域

1.局部变量:位于某一个代码里面,称为局部变量,不能被全局访问(解决:变量提升为全局变量)

2、作用域链:先往同级的地方找,找不到向上面找,一直找到为止

注:如果外面没有声明变量,找不到变量就会报错

3、局部作用域:在一个函数内声明的或者在一段代码块内声明的,作用范围是当前的代码块

4、全局作用域:在对应的全局声明的,作用范围是全局

JavaScript从入门到精通 纯笔记(day03)--循环语句

一、循环
        概念:重复执行一段代码(while、do while、for)

                注:1)、避免死循环    2)、提高效率(减少时间复杂度)

        循环三要素:初始值、迭代量、条件

1、while(条件表达式返回true或false){

            执行代码

    }

    var 初始值变量=值

    while(条件){

            迭代量

            执行代码

    }

自旋:while(true)生成一万个订单编号,不能重复,生成编号的代码无限去生成,直到有一万个不重复的订单编码才出去

2、do while(对应的while唯一的区别是先做,后判断,意味着最少走一次)

while与do while的区别

        1)、while是先判断后执行,do while先执行再判断

        2)、do while最少执行,while至少执行0次

        3)、常用while来写一些执行多次的内容(最少没规定) do while(规定必须要执行的时候)

3、for循环(常用)

    for (初始值;条件;迭代量){

            执行代码;

    }

面试题:for(;;)这样写错了吗?

            正确,意味着对应里面的内容可以被省略,死循环,省条件,迭代量必定是死循环

  4、时间复杂度

            概述:从传统意义上,对应的代码时间复杂度越低,对应执行速度越快(效率越高)

            1)一行只执行一次:O(1)

            2)O(logN)

            3)O(n)

            4)O(nlogN)由两个值决定

            5)O(n^2)循环嵌套

时间复杂度效率:由上至下,依次降低(不要看次数,要看由什么决定时间复杂度)

注:while时间复杂度低于for循环(算法优化可以使用while来替代for循环)

5、break
        break跳出代码块(switch以及循环)只会跳出当前break所在层,break后面的内容不会走

6、continue

        continue跳过本次循环,只能在循环内使用,循环还在继续

JavaScript从入门到精通 纯笔记(day02)--运算符与逻辑分支

一、一元运算符
注:()不能与++一起使用,因为优先级一样,控制台会报错,如果写一行,要分开写

二、比较
1、字符与数值进行比较-->会自动转换成number,若转换不成功,就是NaN,结果就是false

2、只要看到NaN,就是false(NaN==NaN,结果就是false)

3、字符串和字符串之间的比较(转成ASCII码进行比较,A:65,a:65,比较第一位)

       eg:

            ' a ' > ' b '    结果为false

            ' bAA ' < ' azz '    结果为false

4、undefined和null进行比较(undefined是null的派生子类)

        undefined==null        结果为true

        undefined===null        结果为false

5、boolean类型的值与null进行比较(自动转数值)

        eg:

                true>null    结果为true

                true>undefined    结果为false(undefined->NaN)

6、null和0 的比较(null是空对象的引用)

        eg:

                null==0    结果为false(null对应的是一个引用类型,引用类型比较的是地址)

                注:null在进行>和<等会转成number类型,但是在==时不会转成number类型

7、在同时有转换和比较的时候,会攸县转换再进行比较

三、逻辑运算符
&&短路与

&断路与

    &&所有的同时满足才为真,只要前面有一个假,后面就不会看了

    &无论真假,他都要看完全部

     所以比较二者而言,第一种效率更加高一点

||短路或

|断路或

    ||有一个true就是true,前面只要出现true,就不用看后面了

    |一样要看完全部

    第一种效率高

!取反    自动转成布尔类型

' 短路或 '具体操作

        1、如果两个都是false,取最后一个值

        2、如果两个都是true情况下,取前面那个为true的结果

 三目运算符(三目运算符的优先级要低于逻辑运算符的优先级)

四、程序控制语句

    1、逻辑分支if

        结构:

        顺序:从上朝下执行的代码就是顺序

        分支(选择):根据不同的情况,执行对应代码

        循环:重复做一件事情

            逻辑if-单分支

             if条件判断语句的写法:

                               if (表达式){

                                                执行语句

                                }

                注:

                    1、if后面的()不能省略。

                    2、一条执行语句可以省略{}, 多条时不能省略{}, 建议不管是一条还是多条都写上{}

                    3、当括号内的表达式结果成立(为true时),则执行大括号内的语句,否则不执行。

                if条件多分支语句的写法:

                          if(表达式){

                                    执行语句1

                          }else if(表达式2){

                                    执行语句2

                         }

                        ..........

                        else{

                                    执行语句n

                         }

                    注:从上往下,满足哪个条件就执行其相对应的语句,都不满足时,执行最后的else的语句,只能

                            进入其中之一。

            if 的嵌套(嵌套if时, 内层的每一对if-else代码要缩进且对齐;编写代码时,else要与最近的if配对。)

2、逻辑分支switch

          语法:

                    switch(表达式) {

                                     case 常量1: 语句; break;

                                     case 常量2: 语句; break;

                                     …

                                     case 常量n: 语句; break;

                                     default:语句; break;

                                     }

            注:break的作用:是跳出switch结构,如果没有break,则继续执行下面分支的的语句(而不进行

                    判  断)。注意case穿透,要加break

JavaScript从入门到精通 纯笔记(day01)--JavaScript的初了解

一、诞生时间
           1995年

二、JavaScript包含内容
            ECMAScript:基本语法及相关对象es(包括我们了解的es3,es5,es6.es7...)

            DOM:文档对象模型,用来操作HTML

            BOM:浏览器对象模型,用来操作浏览器

三、书写方式
            1、书写在script标签里(比较常用)

            2、js文件里

            3、对应时间属性里面(比较少用,针对少代码单一模块)

四、变量定义(使用var关键词声明,区分大小写)
            1、见名知意

            2、只能是对应的字母或数字或下划线等字符(注:不能用数字开头)

            3、一般采用驼峰命名法(第一个单词首字母小写,其他单词首字母大写)

            4、不能用关键字(占用之前的关键词的引用空间)

            5、普通变量首字母小写,常量全大写(const)

            注:函数-->fn

                   正则-->re

五、数据类型(检验值的类型)
            1、number(所有数值)

            2、string(单引号,双引号均表示字符串)

            3、boolean(true or false)

            4、null(是null的情况下)

            5、undefined

注:number型在控制台的颜色为蓝色,字符串颜色为黑色,null和undefined为灰色

       typeof可以查看对应类型(null显示的是object,而undefined是null的对象扩展,属于父子关系)

六、常见类型转换
            1、number

                    1)number-->string

                          number.toString------隐式转换

                    2)number-->boolean

                           Boolean(number)-----非0为true,NaN和0为false

            2、string

                    1)string-->number

                          Number(string)--------直接转换成number,当数值无法解析,结果为NaN

                          parseInt()

                          parseFloat()--->从前到后解析,无数值变NaN

                    2)string-->boolean

                          Boolean(string)--------非空为true(空格或换行也占位置),空字符串为false

           3、undefined,null

                    1)转boolean

                             结果都是false

                    2)转number

                             前者为NaN,后者为0

           4、boolean转number

                            false-->0

                            true-->1

七、运算符和表达式
 1、算术运算符

                  +    -    *    /    %

                  1) ' + '

                        --两个number相加    正常运算(1+1结果2)

                        --一个string加一个number    拼接(‘123’+1结果1231)

                        注:true+1结果为2,boolean类型和null和undefined小于number,则自动转成number

                  2)' - '

                          --一个string减一个number(‘123’-1结果为122)

                          注:true-1结果为0,undefined-1结果为NaN,null-1结果为-1,null为0

                    3)' * '

                          true * 3 结果为3

                    4)' % '

                          注:大数取余小数,取余结果得到的是余数;小数取余大数,结果得到的是小数。

2、++/--前置,后置

        自增和自减的前置和后置的区别

        前置先执行对应的++(--) 再执行对应的代码

        后置先执行对应的代码 再执行++(--)

注:计算顺序(先算括号里面的 ++或-- 再进行乘除取余 再进行+- 再进行比较运算 再逻辑运费 再进行赋值)

3、逻辑运算

        1、与&&

                同为true才true

        2、或||

                有true就true

        3、非!

                做取反操作

4、比较

            >    ,    <    ,    >=    ,    <=    ,    ==    ,    !=    ,    ===(全等,一个东西)

5、赋值

            =    ,    +=    ,    -=    ,    /=    ,*/    ,    %=

6、位运算(转为二进制再进行位的变化运算)

        左移<<  

        右移>>    

        ~去小数

       谷歌面试题:

                怎样快速从2得到8?

                        方案:

                                2转换成二进制为    10

                                8转换成二进制为    1000

                                则进行(2<<2)即左移2位,补0操作,既可实现快速转换

7、三目运算符

                表达式(判断条件)?1:2

                                条件正确,则为1,反之,则为2

8、number扩展:保留小数方法

                number.toFixed(2)即为number数值保留2位小数

八、进制的转换
1、十进制二进制的相互转换

                十进制-->二进制    (除2取余法)

                二进制-->十进制    (对应位数乘2的多少次方相加)

2、十进制转八进制    (除8取余)

3、十进制转16进制    (除16取余)

注:1)将对应8进制,16进制字符串转换为十进制数值:parseInt或parseFloat方法

        2)将10进制数值转换成对应8进制或16进制字符串:toString方法

elasticsearch 8.2.3 安装及springboot简单使用

一、下载安装
官网下载地址
https://www.elastic.co/cn/downloads/elasticsearch

解压
elasticsearch-8.2.3-windows-x86_64

修改配置
elasticsearch-8.2.3\config\elasticsearch.yml

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
#network.host: 192.168.0.1
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# --------------------------------- Readiness ----------------------------------
#
# Enable an unauthenticated TCP readiness endpoint on localhost
#
#readiness.port: 9399
#
# ---------------------------------- Various -----------------------------------
#
# Allow wildcard deletion of indices:
#
#action.destructive_requires_name: false

#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically      
# generated to configure Elasticsearch security features on 21-06-2022 06:14:38
#
# --------------------------------------------------------------------------------

# Enable security features
#xpack.security.enabled: true

#xpack.security.enrollment.enabled: true

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
#xpack.security.http.ssl:
#  enabled: true
#  keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
#xpack.security.transport.ssl:
  #enabled: true
  #verification_mode: certificate
  #keystore.path: certs/transport.p12
  #truststore.path: certs/transport.p12
# Create a new cluster with the current node only
# Additional nodes can still join the cluster later
#cluster.initial_master_nodes: ["DESKTOP-22BJ4SG"]

# Allow HTTP API connections from anywhere
# Connections are encrypted and require user authentication
http.host: 0.0.0.0

# Allow other nodes to join the cluster from anywhere
# Connections are encrypted and mutually authenticated
#transport.host: 0.0.0.0


# 集群的名字
cluster.name: es-cluster

# 节点名字
node.name: es_node1

# ES的监听地址
network.host: 0.0.0.0

#设置对外服务的http端口,默认为9200
http.port: 9200

#设置索引数据的存储路径
path.data: D:/99-tools/elasticsearch-8.2.3-windows-x86_64/elasticsearch-8.2.3/data    
#设置日志文件的存储路径
path.logs: D:/99-tools/elasticsearch-8.2.3-windows-x86_64/elasticsearch-8.2.3/logs    

# 关闭http访问限制
xpack.security.enabled: false

# 增加新的参数,head插件可以访问es
http.cors.enabled: true
http.cors.allow-origin: "*"

# Enable security features
#xpack.security.enabled: false
xpack.security.enrollment.enabled: true

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
  enabled: false
  keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
  enabled: true
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
# Create a new cluster with the current node only
# Additional nodes can still join the cluster later
cluster.initial_master_nodes: ["es_node1"]  #注意,这个要与node.name填写一致

#屏蔽自动下载geopip
ingest.geoip.downloader.enabled: false

启动服务
双击elasticsearch-8.2.3\bin\elasticsearch.bat 运行
9300:Java程序访问的端口
9200:浏览器、HTTP访问的端口

http://localhost:9200/

二、客户端elasticsearch-head安装
elasticsearch-head是一款专门针对于Elasticsearch的客户端工具

下载源码
https://github.com/mobz/elasticsearch-head](https://github.com/mobz/elasticsearch-head

加压后,在elasticsearch-head目录下执行命令
需要先安装nodejs,下载地址:http://nodejs.cn/download/

npm install
npm run start
安装成功
http://localhost:9100/

三、springboot 集成
依赖

  <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.13.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>jakarta.json</artifactId>
        <version>2.0.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/co.elastic.clients/elasticsearch-java -->
    <dependency>
        <groupId>co.elastic.clients</groupId>
        <artifactId>elasticsearch-java</artifactId>
        <version>8.2.3</version>
    </dependency>

配置

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticSearchConfig {

    @Value("${elasticsearch.host-name}")
    private String hostName;

    @Value("${elasticsearch.port}")
    private int port;


    @Bean
    public ElasticsearchClient elasticsearchClient(){
        RestClient client = RestClient.builder(new HttpHost(hostName, port,"http")).build();
        ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper());
        return new ElasticsearchClient(transport);
    }

}

index操作类

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class EsGpsIndexService {

    @Autowired
    private ElasticsearchClient client;

    public final static String INDEX_NAME = "gps";

    /**
     * 增加index
     * @throws IOException
     */
    public boolean create() throws IOException {
        CreateIndexResponse indexResponse = client.indices().create(c -> c.index(INDEX_NAME));
        System.out.println(indexResponse.toString());
        return indexResponse.acknowledged();
    }


    /**
     * 查询Index
     * @throws IOException
     */
    public void query() throws IOException {
        GetIndexResponse getIndexResponse = client.indices().get(i -> i.index(INDEX_NAME));
        System.out.println(getIndexResponse.toString());
    }

    /**
     * 判断index是否存在
     * @throws IOException
     */
    public boolean exists() throws IOException {
        BooleanResponse booleanResponse = client.indices().exists(e -> e.index(INDEX_NAME));
        System.out.println(booleanResponse.value());
        return booleanResponse.value();
    }


    /**
     * 删除index
     * @throws IOException
     */
    public void delete() throws IOException {
        DeleteIndexResponse deleteIndexResponse = client.indices().delete(d -> d.index(INDEX_NAME));
        System.out.println(deleteIndexResponse.toString());
    }

}
document 操作类

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
import co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.json.JsonData;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class EsGpsDocumentService {

    @Autowired
    private ElasticsearchClient client;


    /**
     * 插入document
     * @throws IOException
     */
    public void add(GpsInfo gps) throws IOException {
        IndexResponse indexResponse = client.index(i -> i
                .index(EsGpsIndexService.INDEX_NAME)
                //设置id
                .id(gps.getId())
                //
                .document(gps));

    }

    /**
     * 更新Document
     * @throws IOException
     */
    public void update(GpsInfo gps) throws IOException {
        UpdateResponse<GpsInfo> updateResponse = client.update(u -> u
                        .index(EsGpsIndexService.INDEX_NAME)
                        .id(gps.getId())
                        .doc(gps)
                , GpsInfo.class);
    }

    /**
     * 判断Document是否存在
     * @throws IOException
     */
    public void exist(String id) throws IOException {
        BooleanResponse indexResponse = client.exists(e -> e.index(EsGpsIndexService.INDEX_NAME).id(id));
        System.out.println(indexResponse.value());
    }

    /**
     * 查询Document
     * @throws IOException
     */
    public void get(String id) throws IOException {
        GetResponse<GpsInfo> getResponse = client.get(g -> g
                        .index(EsGpsIndexService.INDEX_NAME)
                        .id("1")
                , GpsInfo.class
        );
        System.out.println(getResponse.source());


    }

    /**
     * 删除Document
     * @throws IOException
     */
    public void delete(String id) throws IOException {
        DeleteResponse deleteResponse = client.delete(d -> d
                .index(EsGpsIndexService.INDEX_NAME)
                .id("1")
        );
        System.out.println(deleteResponse.id());
    }

    /**
     * 批量插入Document
     * @throws IOException
     */
    public void bulk(List<GpsInfo> gpsList) throws IOException {

        List<BulkOperation> bulkOperationArrayList = new ArrayList<>();
        //遍历添加到bulk中
        for(GpsInfo gps : gpsList){
            bulkOperationArrayList.add(BulkOperation.of(o->o.index(i->i.document(gps).id(gps.getId()))));
            //bulkOperationArrayList.add(BulkOperation.of(x->x.create(d->d.document(gps).id(gps.getId()).index(EsGpsIndexService.INDEX_NAME))));
        }

        BulkResponse bulkResponse = client.bulk(b -> b.index(EsGpsIndexService.INDEX_NAME).operations(bulkOperationArrayList));

    }

    /**
     * 查询
     * @throws IOException
     * https://wenku.baidu.com/view/a8954a68862458fb770bf78a6529647d27283439.html
     */
    public void search(String vehicleId,long startTime,long endTime,int page,int pageSize) throws IOException {

        SearchResponse<GpsInfo> search = client.search(s -> s
                .index(EsGpsIndexService.INDEX_NAME)
                .query(q -> q
                        .bool(b -> b
                                .filter(m -> m.term(t -> t.field("vehicleId").value(vehicleId)))
                                .filter(f -> f.range(x-> x.field("gpsTime").gte(JsonData.of(startTime)).lte(JsonData.of(endTime))))
                         ))

                //分页查询,从第0页开始查询20个document
                .from(page)
                .size(pageSize)
                //按时间降序排序
                //.sort(f->f.field(o->o.field("gpsTime").order(SortOrder.Desc)))
                , GpsInfo.class
        );
        for (Hit<GpsInfo> hit : search.hits().hits()) {
            System.out.println(hit.source());
        }
    }
}

实体

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class GpsInfo {

    private String id;
    private String vehicleId;
    private String deviceId;
    private String location;
    private Long gpsTime;
    private Long createTime;

}

测试例子

@RestController
@RequestMapping("/gps")
public class GpsController {

    @Autowired
    EsGpsIndexService esGpsIndexService;

    @Autowired
    EsGpsDocumentService esGpsDocumentService;

    @GetMapping("/test")
    public String test() throws IOException {

        boolean exist = esGpsIndexService.exists();
        if(!exist){
            esGpsIndexService.create();
        }

        esGpsIndexService.query();

        //
        /*GpsInfo gps = new GpsInfo();
        gps.setId("1");
        gps.setDeviceId("1111");
        gps.setVehicleId("123");
        gps.setLocation("12,33");
        gps.setCreateTime(System.currentTimeMillis());
        gps.setGpsTime(System.currentTimeMillis());
        esGpsDocumentService.add(gps);*/

        /*List<GpsInfo> gpsList =  new ArrayList<>();
        int i= 0;
        GpsInfo gps = null;
        long time = 1656041715000L;
        while (i < 10000){
            gps = new GpsInfo();
            gps.setId(String.valueOf(i*10000));
            gps.setDeviceId("1111");
            gps.setVehicleId("123456");
            gps.setLocation("12233.2232,33.2512235");
            gps.setCreateTime(time);
            gps.setGpsTime(time);
            gpsList.add(gps);
            //
            time += 1000;
            i += 1;
        }
        esGpsDocumentService.bulk(gpsList);*/


        long start = 1656041715000L;
        long end   = 1656041725000L;
        String v = "123456";
        esGpsDocumentService.search(v,start,end,0,20);


        return "elasticSearch test!!!!";
    }

}