Linux build elasticsearch 7.x version

Table of contents

installation method

Pre-boot configuration

Environment configuration

configuration file

Start Elasticsearch

Install the visual interface kibana

install word breaker

index operation

document manipulation


Official website address: Download Elasticsearch | Elastic

installation method

  • traditional way

According to the platform system Windows, linux, mac --> download the installation package

Take linux as an example, go to the directory where you want to install, download the installation package and decompress it

# 进入安装目录
cd /home

# 下载安装包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.3-linux-x86_64.tar.gz

# 解压
tar -zxvf elasticsearch-7.17.3-linux-x86_64.tar.gz elasticsearch-7.17.3

  • docker containerized installation

The current mainstream containerization technology does not need to consider the configuration environment, which is very convenient, but requires a certain docker foundation

Pre-boot configuration

  • Environment configuration

The versions before elasticsearch 7.x need to install jdk, but after the 7.x version, the jdk environment is built in, so you can install the jdk environment without installing the environment, you can refer to the bin/elasticsearch-env file

if [ ! -z "$ES_JAVA_HOME" ]; then
  JAVA="$ES_JAVA_HOME/bin/java"
  JAVA_TYPE="ES_JAVA_HOME"
elif [ ! -z "$JAVA_HOME" ]; then
  # fallback to JAVA_HOME
  echo "warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME" >&2
  JAVA="$JAVA_HOME/bin/java"
  JAVA_TYPE="JAVA_HOME"
else
  # use the bundled JDK (default)
  if [ "$(uname -s)" = "Darwin" ]; then
    # macOS has a different structure
    JAVA="$ES_HOME/jdk.app/Contents/Home/bin/java"
  else
    JAVA="$ES_HOME/jdk/bin/java"
  fi
  JAVA_TYPE="bundled JDK"
fi

It means that you will first look for the ES_JAVA_HOME environment variable. If it exists, use the jdk under this environment variable. If it does not exist, look for the JAVA_HOME environment variable. Generally, you will have this environment. , ES_HOME points to the jdk directory in our installation package.

Priority: ES_JAVA_HOME>JAVA_HOME>ES_HOME

  • configuration file

Important configuration changes | Elasticsearch: The Definitive Guide | Elastic

The official website here provides us with some important parameters for us to modify selectively.

# Elasticsearch 默认启动的集群名字叫 elasticsearch 。你最好给你的生产环境的集群改个名字,改名字的目的很简单, 就是防止某人的笔记本电脑加入了集群这种意外
cluster.name: elasticsearch_production

# Elasticsearch 会在你的节点启动的时候随机给它指定一个名字,这些名字是在启动的时候产生的,每次启动节点, 它都会得到一个新的名字。这会使日志变得很混乱,因为所有节点的名称都是不断变化的。
node.name: elasticsearch_005_data

# Elasticsearch 会把你最重要的数据放在以下目录下
path.data: /path/to/data1,/path/to/data2 

# Elasticsearch 会把日志放在以下目录下
path.logs: /path/to/logs

# Elasticsearch 会把插件放在以下目录下
path.plugins: /path/to/plugins

# 设定对你的集群的稳定 极其 重要。 当你的集群中有两个 masters(注:主节点)的时候,这个配置有助于防止 脑裂 ,一种两个主节点同时存在于一个集群的现象。此设置应该始终被配置为 master 候选节点的法定个数(大多数个)。法定个数就是 ( master 候选节点个数 / 2) + 1
discovery.zen.minimum_master_nodes: 1

# 检查内存,当内存过小时,会启动失败,建议开发环境暂时关闭,生产环境内存足够再开启
bootstrap.memory_lock: false

# 配置能够访问当前节点的主机,0.0.0.0所有主机都可访问
network.host: 0.0.0.0

The most important thing is to enable remote access:

# elasticsearch.yml 中配置
network.host: 0.0.0.0

Set memory size:

# 在config目录下修改jvm配置
vim jvm.options

# jvm堆内存大小
-Xms1g
-Xmx1g

Suggestion: xms and xmx settings are the same, xmx should not exceed 50% of the system memory, and should not exceed 30G

Start Elasticsearch

If it is started by windows, just run elasticsearch.bat directly

Running es in the linux system cannot be the root user, so we need to create an es user first

# 创建es用户
adduser es

# 修改密码
passwd es

# 分配权限
chown -R es:es ./elasticsearch-7.17.3

# 切换用户
su es

# 运行 -d 后台
bin/elasticsearch -d

 Possible errors:

  • max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

The maximum virtual memory is too small, increase the virtual memory of the system

# 切换到root
su root
vim /etc/sysctl.conf

# 追加内容
vm.max_map_count=262144

# 保存之后执行
sysctl -p
  • max number of threads [1024] for user (es] is too low, increase to at least [4096]

Unable to create a local thread problem, the maximum number of threads that users can create

vim /etc/security/limits.d/20-nproc.conf 
# 改为以下配置
*    soft    nproc    4096
  • the default discovery settings are unsuitable for production use; at least one of (discovery.seed hostsdiscovery.seed providers, custer.initial master nodes] must be configured

Missing default configuration

# elasticsearch.yml 文件中放开以下注释
discovery.seed_hosts: ["本机ip"]

cluster.initial_master_nodes: ["本机节点名称"]

Browser access: http://ip:9200

 

Turn on self-starting es

  • Create a service startup file for es
# 进入系统启动目录
cd /etc/init.d

# 新建es文件
vim elasticsearch
  • Fill in the configuration content
#!/bin/bash
#chkconfig: 345 63 37
#description: elasticsearch
#processname: elasticsearch-7.17.3

# 这个目录是你Es所在文件夹的目录
export ES_HOME=/usr/local/elasticsearch-7.17.3
case $1 in
start)
    su es<<!
    cd $ES_HOME
    ./bin/elasticsearch -d -p pid
    exit
!
    echo "elasticsearch is started"
    ;;
stop)
    pid=`cat $ES_HOME/pid`
    kill -9 $pid
    echo "elasticsearch is stopped"
    ;;
restart)
    pid=`cat $ES_HOME/pid`
    kill -9 $pid
    echo "elasticsearch is stopped"
    sleep 1
    su es<<!
    cd $ES_HOME
    ./bin/elasticsearch -d -p pid
    exit
!
    echo "elasticsearch is started"
    ;;
*)
    echo "start|stop|restart"
    ;;
esac
exit 0

Note : The following configurations cannot be deleted, and the previous # comments remain

#!/bin/bash
#chkconfig: 345 63 37
#description: elasticsearch
#processname: elasticsearch-7.17.3 

  • grant permission
chmod 777 elasticsearch
  • add/remove service
# 添加服务
chkconfig --add elasticsearch

# 删除服务,无需执行
chkconfig --del elasticsearch
  • start/stop/restart service
# 启动
service elasticsearch start

# 停止
service elasticsearch stop

# 重启
service elasticsearch restart
  • Enable/disable self-starting service
# 开启开机自启服务
chkconfig elasticsearch on

# 关闭开机自启服务
chkconfig elasticsearch off

Install the visual interface kibana

  • download

Official website download address: https://artifacts.elastic.co/downloads/kibana/kibana-7.17.3-linux-x86_64.tar.gz

# 下载
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.17.3-linux-x86_64.tar.gz

# 解压
tar -zxvf kibana-7.17.3-linux-x86_64.tar.gz

  • Modify the configuration file

Official website configuration file reference: Configure Kibana | Kibana User Manual | Elastic

# 编辑配置文件
vim config/kibana.yml

# 以下配置放开
server.port: 5601

server.host: "0.0.0.0"

elasticsearch.hosts: ["http://es主机ip:9200"]

i18n.locale: "zh-CN"

  • start kibana
nohup bin/kibana &

install word breaker

  • Install online

The default tokenizer of es is standard, which will segment the word with the smallest granularity

# 查看已安装的插件
bin/elasticsearch-plugin list

# 安装 analysis-icu 分词器
bin/elasticsearch-plugin install analysis-icu

# 删除 analysis-icu 分词器
bin/elasticsearch-plugin remove analysis-icu

Note: After installing or deleting the plug-in, you need to restart the es service to take effect

  • Install offline

To download the ik tokenizer, you need to download it offline. The download address is: Tags medcl/elasticsearch-analysis-ik GitHub

Download the ik tokenizer corresponding to the es version from gitHub, and upload it to the plugins directory on the linux server

# 切换es用户
su es

# 进入到插件目录中
cd elasticsearch-7.17.3/plugins

# 下载压缩包
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.3/elasticsearch-analysis-ik-7.17.3.zip

# 解压
unzip -d elasticsearch-analysis-ik elasticsearch-analysis-ik-7.17.3.zip

# 删除安装包
rm -f elasticsearch-analysis-ik-7.17.3.zip

After decompression, delete the installation package and restart the es service to take effect

# 切换root用户
su root

# 找到es服务
netstat -tunlp |grep 9200
tcp6   0    0 :::9200        :::*            LISTEN      1734/java

# 杀掉进程
kill 1734

# 切换es用户
su es

# 重启es
./bin/elasticsearch -d

Test the ik tokenizer

# ik最粗力度
POST _analyze
{
    "analyzer":"ik_smart",
    "text":"十月初七"
}

# 结果
{
    "tokens": [
        {
            "token": "十月",
            "start_offset": 0,
            "end_offset": 2,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "初七",
            "start_offset": 2,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 1
        }
    ]
}


# ik最细力度
POST _analyze
{
    "analyzer":"ik_max_word",
    "text":"十月初七"
}

# 结果
{
    "tokens": [
        {
            "token": "十月",
            "start_offset": 0,
            "end_offset": 2,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "十",
            "start_offset": 0,
            "end_offset": 1,
            "type": "TYPE_CNUM",
            "position": 1
        },
        {
            "token": "月初",
            "start_offset": 1,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 2
        },
        {
            "token": "月",
            "start_offset": 1,
            "end_offset": 2,
            "type": "COUNT",
            "position": 3
        },
        {
            "token": "初七",
            "start_offset": 2,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 4
        },
        {
            "token": "七",
            "start_offset": 3,
            "end_offset": 4,
            "type": "TYPE_CNUM",
            "position": 5
        }
    ]
}

index operation

  • understand the index

 Note: Index names must be lowercase and cannot start with an underscore

  • create

put /index name

# 简单创建索引
PUT /index_test

# 创建索引并配置分片数和副本数
PUT /index_test
{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 2
    }
}

# 返回数据
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "index_test"
}

  • modify, delete

put /index name/_settings

delete /index name

# 修改索引配置
PUT /index_test/_settings
{
    "index": {
        "number_of_replicas": 1
    }
}

# 删除索引
DELETE /index_test

# 返回
{
    "acknowledged": true
}
  • Inquire

get /index name

# 查询索引信息
GET /index_test

# 返回
{
    "index_test": {
        "aliases": {},    # 别名
        "mappings": {},   # 映射,有数据后映射的字段类型
        "settings": {
            "index": {
                "routing": {
                    "allocation": {
                        "include": {
                            "_tier_preference": "data_content"
                        }
                    }
                },
                "number_of_shards": "3",              # 分片
                "provided_name": "index_test",        # 索引名称
                "creation_date": "1679481012424",
                "number_of_replicas": "1",            # 副本
                "uuid": "2TTTjETOQnK4V53XmGEC8Q",     # uuid
                "version": {                          # 版本
                    "created": "7170399"
                }
            }
        }
    }
}

document manipulation

1. Elasticsearch is document-oriented, and a document is the smallest unit of all searchable data

2. Documents will be serialized into json format and stored in elasticsearch. Each json field corresponds to a data type (string, value, Boolean, date, binary, range type)

3. There is a unique id in the document, and a document contains multiple fields, similar to a record in mysql 

4. The document does not need to define the field data type in advance, es will automatically deduce it according to the data, support arrays, and support nesting

  • new document

Modify the word breaker of es to ik word breaker, and add the index again after deleting

PUT /index_test
{
    "settings": {
        "index":{
            "analysis.analyzer.default.type":"ik_max_word"            
        }
    }
}

[put | post] /index name/[_doc | _create]/id

# 指定id,id存在则修改否则新增,会删除内容后重新新增
PUT /index_test/_doc/1
{
    "name": "十月初七",
    "age": 18,
    "phone": "13100000000",
    "hobbyList": [
        {
            "hobbyId": 1,
            "hobbyName": "唱歌"
        },{
            "hobbyId": 2,
            "hobbyName": "羽毛球"
        },{
            "hobbyId": 3,
            "hobbyName": "狼人杀"
        }
    ]
}

# id又有可无
POST /index_test/_doc
{
    "name": "青玖",
    "age": 18,
    "phone": "13100000000",
    "hobbyList": [
        {
            "hobbyId": 1,
            "hobbyName": "密室逃脱"
        },{
            "hobbyId": 2,
            "hobbyName": "看电影"
        }
    ]
}

# 自动生成id
POST /index_test/_doc
{
    "name": "青玖",
    "age": 18,
    "phone": "13100000000",
    "hobbyList": [
        {
            "hobbyId": 1,
            "hobbyName": "密室逃脱"
        },{
            "hobbyId": 2,
            "hobbyName": "看电影"
        }
    ]
}

# id存在则报错
POST | PUT /index_test/_create/4
{
    "name": "test",
    "age": 3,
    "phone": "13100000000",
    "hobbyList": [
        {
            "hobbyId": 1,
            "hobbyName": "打游戏"
        }
    ]
}


# 返回
{
    "_index": "index_test",    # 索引名称
    "_type": "_doc",           # 文档类型,默认为_doc,8.x之后被删除
    "_id": "1",                # 唯一id
    "_version": 1,             # 版本。每次修改+1
    "result": "created",       # 新增 修改为:updated
    "_shards": {               # 分片
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,            # 类似修改版本,每次修改递增,用于并发场景的乐观锁
    "_primary_term": 1       # 恢复数据时_seq_no一样的冲突,避免写入被覆盖,每次恢复递增
}

The difference between PUT and POST to add new documents: PUT can only create and modify documents according to id, and POST will automatically create id to create and update documents according to whether there is an id, and both update documents will be updated in full. create can only create documents but not update them.

  • Partial Update Documentation

POST /indexname/_update/id

# 根据id局部更新文档
POST /index_test/_update/1
{
    "doc": {
        "age": 19
    }
}

# 返回
{
    "_index": "index_test",
    "_type": "_doc",
    "_id": "1",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 3,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

Modify according to the content of the query (atomic)

# 查询出的文档进行修改(对id为1的文档修改年龄为20)
POST /index_test/_update_by_query
{
    "query": {
        "match": {
            "_id":1
        }
    },
    "script":{
        "source":"ctx._source.age=20"
    }
}

# 返回
{
    "took": 185,
    "timed_out": false,
    "total": 1,
    "updated": 1,
    "deleted": 0,
    "batches": 1,
    "version_conflicts": 0,
    "noops": 0,
    "retries": {
        "bulk": 0,
        "search": 0
    },
    "throttled_millis": 0,
    "requests_per_second": -1.0,
    "throttled_until_millis": 0,
    "failures": []
}

  • batch write

The request parameter has at least four lines and must be an even number of lines

The first line of parameters is the operation type, index, document type and id, and the second line of parameters is the new data, as follows:

POST  _builk 

{"actionName":{"_index":"indexName","_type":"typeName","_id":"id"}}
{"field1":"value1","field2":"value2","field3":"value3"}

actionName: Indicates the type of operation, mainly create, update, delete, index

# 批量修改
POST /_bulk
{"update":{"_index":"index_test","_id":3}}
{"doc":{"name":"青玖","age":18,"phone":"13100000000","hobbyList":[{"hobbyId":4,"hobbyName":"密室逃脱"},{"hobbyId":5,"hobbyName":"看电影"}]}}
{"update":{"_index":"index_test","_id":4}}
{"doc":{"name":"test","age":23,"phone":"13111111111","hobbyList":[{"hobbyId":6,"hobbyName":"打游戏"}]}}
{"create":{"_index":"index_test","_id":5}}
{"doc":{"name":"张三","age":3,"phone":"13333333333","hobbyList":[{"hobbyId":6,"hobbyName":"打游戏"}]}}


# 返回
{
    "took": 71,
    "errors": false,
    "items": [
        {
            "update": {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "3",
                "_version": 3,
                "result": "noop",
                "_shards": {
                    "total": 3,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 3,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
            "update": {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "4",
                "_version": 2,
                "result": "noop",
                "_shards": {
                    "total": 3,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 4,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
            "create": {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "5",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 3,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 0,
                "_primary_term": 1,
                "status": 201
            }
        }
    ]
}

  • query document

There are mainly two types of queries, the first one is not recommended, and the second one is mostly used, which is easier to read json

1. REST style request url, carrying parameters, kv format

2. Conditional encapsulation to requestBoby (official recommendation)

Query by document id

# 根据id查询文档
GET /index_test/_doc/1

# 返回
{
    "_index": "index_test",
    "_type": "_doc",
    "_id": "1",
    "_version": 4,
    "_seq_no": 4,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "phone": "13355490600",
        "name": "十月初七",
        "hobbyList": [
            {
                "hobbyName": "唱歌",
                "hobbyId": 1
            },
            {
                "hobbyName": "羽毛球",
                "hobbyId": 2
            },
            {
                "hobbyName": "狼人杀",
                "hobbyId": 3
            }
        ],
        "age": 20
    }
}

  • batch query

_mget

# 跨索引批量查询
GET _mget
{
    "docs": [
        {
            "_index": "index_dev",
            "_id": 1
        },
        {
            "_index": "index_test",
            "_id": 1
        }
    ]
}

# 返回
{
    "docs": [
        {
            "_index": "index_dev",
            "_type": "_doc",
            "_id": "1",
            "_version": 1,
            "_seq_no": 0,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "doc": {
                    "name": "张三",
                    "age": 3,
                    "hobbyList": [
                        1,
                        2
                    ]
                }
            }
        },
        {
            "_index": "index_test",
            "_type": "_doc",
            "_id": "1",
            "_version": 4,
            "_seq_no": 4,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "phone": "13355490600",
                "name": "十月初七",
                "hobbyList": [
                    {
                        "hobbyName": "唱歌",
                        "hobbyId": 1
                    },
                    {
                        "hobbyName": "羽毛球",
                        "hobbyId": 2
                    },
                    {
                        "hobbyName": "狼人杀",
                        "hobbyId": 3
                    }
                ],
                "age": 20
            }
        }
    ]
}

# 单个索引内
GET /index_test/_mget
{
    "ids": [
        1,
        2
    ]
}

# 返回
{
    "docs": [
        {
            "_index": "index_test",
            "_type": "_doc",
            "_id": "1",
            "_version": 4,
            "_seq_no": 4,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "phone": "13355490600",
                "name": "十月初七",
                "hobbyList": [
                    {
                        "hobbyName": "唱歌",
                        "hobbyId": 1
                    },
                    {
                        "hobbyName": "羽毛球",
                        "hobbyId": 2
                    },
                    {
                        "hobbyName": "狼人杀",
                        "hobbyId": 3
                    }
                ],
                "age": 20
            }
        },
        {
            "_index": "index_test",
            "_type": "_doc",
            "_id": "2",
            "found": false
        }
    ]
}

_msearch

# 夸索引查询
GET _msearch
{"index":"index_dev"}
{"query": {"match_all": {}},"from":0,"size":2}
{"index": "index_test"}
{"query": {"match": {"name":"十月初七"}}}


# 返回
{
    "took": 28,
    "responses": [
        {
            "took": 10,
            "timed_out": false,
            "_shards": {
                "total": 3,
                "successful": 3,
                "skipped": 0,
                "failed": 0
            },
            "hits": {
                "total": {
                    "value": 3,
                    "relation": "eq"
                },
                "max_score": 1.0,
                "hits": [
                    {
                        "_index": "index_dev",
                        "_type": "_doc",
                        "_id": "2",
                        "_score": 1.0,
                        "_source": {
                            "doc": {
                                "name": "李四",
                                "age": 4,
                                "hobbyList": [
                                    1,
                                    3
                                ]
                            }
                        }
                    },
                    {
                        "_index": "index_dev",
                        "_type": "_doc",
                        "_id": "3",
                        "_score": 1.0,
                        "_source": {
                            "doc": {
                                "name": "王五",
                                "age": 5,
                                "hobbyList": [
                                    1,
                                    4
                                ]
                            }
                        }
                    }
                ]
            },
            "status": 200
        },
        {
            "took": 25,
            "timed_out": false,
            "_shards": {
                "total": 3,
                "successful": 3,
                "skipped": 0,
                "failed": 0
            },
            "hits": {
                "total": {
                    "value": 1,
                    "relation": "eq"
                },
                "max_score": 3.4526575,
                "hits": [
                    {
                        "_index": "index_test",
                        "_type": "_doc",
                        "_id": "1",
                        "_score": 3.4526575,
                        "_source": {
                            "phone": "13355490600",
                            "name": "十月初七",
                            "hobbyList": [
                                {
                                    "hobbyName": "唱歌",
                                    "hobbyId": 1
                                },
                                {
                                    "hobbyName": "羽毛球",
                                    "hobbyId": 2
                                },
                                {
                                    "hobbyName": "狼人杀",
                                    "hobbyId": 3
                                }
                            ],
                            "age": 20
                        }
                    }
                ]
            },
            "status": 200
        }
    ]
}

  • Query DSL query

1、match_all

Query all, return 10 pieces of data by default, because _search adopts pagination query, take 10 pieces by default, the default maximum pagination returns 10,000 pieces of data, 10,000 records are loaded into the memory, the memory consumption is too large, it is not recommended to modify.

# 查询所有 
GET /index_test/_search
{
    "query": {
        "match_all": {}
    },
    "from":2,    # 位置
    "size": 2    # 返回数据数量,最大为10000,可在setting中修改index.max_result_window
    "sort":[     # 排序
        {
            "age":"desc"
        }
    ],
    "_source":["name","age"]    # 返回指定字段
}


# 返回
{
    "_scroll_id": "FGluY2x1ZGVfY29udGV4dF91dWlkDnF1ZXJ5VGhlbkZldGNoAxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAANAWa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAM8Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAANEWa1U0djZXOWxRNE9wbWZoXzVwQ0F4Zw==",
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "4",
                "_score": null,
                "_source": {
                    "name": "test",
                    "age": 23
                },
                "sort": [
                    23
                ]
            },
            {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "1",
                "_score": null,
                "_source": {
                    "name": "十月初七",
                    "age": 20
                },
                "sort": [
                    20
                ]
            }
        ]
    }
}

# 修改最大返回数量, _all修改所有索引,可指定索引
PUT /_all/_settings
{
    "index.max_result_window": 20000
}

2. scroll page

# 拿到分页id
GET /index_test/_search?scroll=1m
{
    "query": {
        "match_all": {}
    },
    "size": 2,
    "sort":[
        {
            "age":"asc"
        }
    ]
}

# 返回
{
    "_scroll_id": "FGluY2x1ZGVfY29udGV4dF91dWlkDnF1ZXJ5VGhlbkZldGNoAxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL0Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL8Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL4Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4Zw==",
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "5",
                "_score": 1.0,
                "_source": {
                    "doc": {
                        "name": "张三",
                        "age": 3,
                        "phone": "13333333333",
                        "hobbyList": [
                            {
                                "hobbyId": 6,
                                "hobbyName": "打游戏"
                            }
                        ]
                    }
                }
            },
            {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "name": "青玖",
                    "age": 18,
                    "phone": "13100000000",
                    "hobbyList": [
                        {
                            "hobbyName": "密室逃脱",
                            "hobbyId": 4
                        },
                        {
                            "hobbyName": "看电影",
                            "hobbyId": 5
                        }
                    ]
                }
            }
        ]
    }
}


# 根据 scroll_id 分页
GET /_search/scroll
{
    "scroll": "1m",
    "scroll_id": "FGluY2x1ZGVfY29udGV4dF91dWlkDnF1ZXJ5VGhlbkZldGNoAxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL0Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL8Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL4Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4Zw=="
}

# 返回
{
    "_scroll_id": "FGluY2x1ZGVfY29udGV4dF91dWlkDnF1ZXJ5VGhlbkZldGNoAxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL0Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL8Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4ZxZ4aG52WkwtbFN1dS1mYWdEZXZqSnlRAAAAAAAAAL4Wa1U0djZXOWxRNE9wbWZoXzVwQ0F4Zw==",
    "took": 8,
    "timed_out": false,
    "terminated_early": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "4",
                "_score": 1.0,
                "_source": {
                    "name": "test",
                    "age": 23,
                    "phone": "13111111111",
                    "hobbyList": [
                        {
                            "hobbyName": "打游戏",
                            "hobbyId": 6
                        }
                    ]
                }
            },
            {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "bYRQE4cBQtOYu_2bG9Pp",
                "_score": 1.0,
                "_source": {
                    "name": "青玖",
                    "age": 18,
                    "phone": "13100000000",
                    "hobbyList": [
                        {
                            "hobbyId": 1,
                            "hobbyName": "密室逃脱"
                        },
                        {
                            "hobbyId": 2,
                            "hobbyName": "看电影"
                        }
                    ]
                }
            }
        ]
    }
}

3、match

# 名称中带有十月的,查询的数据取决于索引采用的分词器,这里是ik分词器
GET /index_test/_search
{
    "query":{
        "match":{
            "name":"十月"
        }
    }
}

# 返回
{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.407595,
        "hits": [
            {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.407595,
                "_source": {
                    "phone": "13355490600",
                    "name": "十月初七",
                    "hobbyList": [
                        {
                            "hobbyName": "唱歌",
                            "hobbyId": 1
                        },
                        {
                            "hobbyName": "羽毛球",
                            "hobbyId": 2
                        },
                        {
                            "hobbyName": "狼人杀",
                            "hobbyId": 3
                        }
                    ],
                    "age": 20
                }
            },
            {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "dYRtF4cBQtOYu_2bjNNQ",
                "_score": 1.407595,
                "_source": {
                    "name": "十月初一",
                    "age": 18,
                    "phone": "13100000000",
                    "hobbyList": [
                        {
                            "hobbyId": 1,
                            "hobbyName": "密室逃脱"
                        }
                    ]
                }
            },
            {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "doRtF4cBQtOYu_2bsdOx",
                "_score": 1.407595,
                "_source": {
                    "name": "十月初二",
                    "age": 3,
                    "phone": "13100000000",
                    "hobbyList": [
                        {
                            "hobbyId": 2,
                            "hobbyName": "看电影"
                        }
                    ]
                }
            }
        ]
    }
}
# 查询名称带有十月初七中两个字符
GET /index_test/_search
{
    "query": {
        "match": {
            "name": {
                "query": "十月初七",
                "operator": "or",
                "minimum_should_match": 2  # 最低匹配度,在倒排索引中最低匹配度
            }
        }
    }
}

# 返回
{
    "took": 29,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 3.4009905,
        "hits": [
            {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "1",
                "_score": 3.4009905,
                "_source": {
                    "phone": "13355490600",
                    "name": "十月初七",
                    "hobbyList": [
                        {
                            "hobbyName": "唱歌",
                            "hobbyId": 1
                        },
                        {
                            "hobbyName": "羽毛球",
                            "hobbyId": 2
                        },
                        {
                            "hobbyName": "狼人杀",
                            "hobbyId": 3
                        }
                    ],
                    "age": 20
                }
            },
            {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "d4SUIYcBQtOYu_2batP6",
                "_score": 2.2538662,
                "_source": {
                    "name": "初七",
                    "age": 18,
                    "phone": "13100000000",
                    "hobbyList": [
                        {
                            "hobbyId": 1,
                            "hobbyName": "密室逃脱"
                        },
                        {
                            "hobbyId": 2,
                            "hobbyName": "看电影"
                        }
                    ]
                }
            },
            {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "dYRtF4cBQtOYu_2bjNNQ",
                "_score": 1.8767934,
                "_source": {
                    "name": "十月初一",
                    "age": 18,
                    "phone": "13100000000",
                    "hobbyList": [
                        {
                            "hobbyId": 1,
                            "hobbyName": "密室逃脱"
                        }
                    ]
                }
            },
            {
                "_index": "index_test",
                "_type": "_doc",
                "_id": "doRtF4cBQtOYu_2bsdOx",
                "_score": 1.8767934,
                "_source": {
                    "name": "十月初二",
                    "age": 3,
                    "phone": "13100000000",
                    "hobbyList": [
                        {
                            "hobbyId": 2,
                            "hobbyName": "看电影"
                        }
                    ]
                }
            }
        ]
    }
}

Guess you like

Origin blog.csdn.net/weixin_43820024/article/details/129546015