Elasticsearch one-stop Fengshen


Download link: https://download.csdn.net/download/Michael_lcf/86263501

1 es stand-alone installation in linux

1) 解压软件
tar -zxvf elasticsearch-7.8.0-linux-x86_64.tar.gz -C /opt/module
改名
mv elasticsearch-7.8.0 es7

2) 创建用户
因为安全问题,Elasticsearch 不允许 root 用户直接运行,所以要创建新用户,在 root 用户中创建新用户
useradd es7 #新增 es7 用户
echo es7 | passwd --stdin es7 #为es7用户设置密码为es7
userdel -r es7 #如果错了,可以删除再加
chown -R es7:es7 /opt/module/es7 #文件夹所有者

3) 修改配置文件/opt/module/es7/config/elasticsearch.yml文件,加入如下配置:
cluster.name: elasticsearch
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["node-1"]

4) 修改/etc/security/limits.conf 
# 在文件末尾中增加下面内容
# 每个进程可以打开的文件数的限制
es7 soft nofile 65536
es7 hard nofile 65536
# 重新登录即可生效
# 可使用命令查看是否生效
ulimit  -H -n


5) 修改/etc/security/limits.d/20-nproc.conf
# 在文件末尾中增加下面内容
# 每个进程可以打开的文件数的限制
es7 soft nofile 65536
es7 hard nofile 65536
# 操作系统级别对每个用户创建的进程数的限制
* hard nproc 4096
# 注:* 带表 Linux 所有用户名称


6) 修改/etc/sysctl.conf
# 在文件中增加下面内容
# 一个进程可以拥有的 VMA(虚拟内存区域)的数量,默认值为 65536
vm.max_map_count=655360

重新加载
sysctl -p 
7) 使用 es7 用户启动软件
#前台启动
/opt/module/es7/bin/elasticsearch
#后台启动
/opt/module/es7/bin/elasticsearch -d


8) 防火墙相关操作
systemctl stop firewalld
systemctl enable firewalld.service
systemctl disable firewalld.service


9) 测试软件
浏览器中输入地址:http://192.168.168.101:9200
1) 解压安装kibana
tar -zxvf kibana-7.8.0-linux-x86_64.tar.gz -C /opt/kibana-7.8.0
chown -R myes7:myes7 /opt/kibana-7.8.0

2) 修改 config/kibana.yml 文件
# 设置访问ip
erver.host: "0.0.0.0"
# 默认端口
server.port: 5601
# ES 服务器的地址
elasticsearch.hosts: ["http://192.168.168.101:9200"]
# 索引名
kibana.index: ".kibana"
# 支持中文
i18n.locale: "zh-CN"

3) 启动kibana
nohup /opt/kibana-7.8.0/bin/kibana > /dev/null 2>&1 &

4) 通过浏览器访问:
http://192.168.168.101:5601

2 ES cluster installation in linux

3 elasticsearch stand-alone installation in windows

decompress

The directory structure after decompression

Start the elasticsearch
bin/elasticsearch.batfile and start the ES service

Note:
Port 9300 is the communication port of the elasticsearch inter-cluster components;
Port 9200 is the restfull port of the http protocol accessed by the browser;

Browser access: http://localhost:9200

4 kibana installation

1. Unzip the downloaded zip file
https://artifacts.elastic.co/downloads/kibana/kibana-7.8.0-windows-x86_64.zip

2. Modify the config/kibana.yml file

# 默认端口
server.port: 5601
# ES 服务器的地址
elasticsearch.hosts: ["http://localhost:9200"]
# 索引名
kibana.index: ".kibana"
# 支持中文
i18n.locale: "zh-CN"

3. Execute the bin/kibana.bat file in Windows environment
4. Access via browser: http://localhost:5601

3 Basic Operation

3.1 Index operation

1) Create an index

PUT:http://127.0.0.1:9200/shopping

============
{
    
    
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "shopping"
}

Indexes cannot be created repeatedly;

2) View all indexes

GET:http://127.0.0.1:9200/_cat/indices?v

===========
health status index      uuid                   pri rep  docs.count docs.deleted   store.size    pri.store.size
yellow open   student    rfT7DVzyToSklO3j6FRRRg   1   1          0            0       208b           208b
yellow open   goods      sriB1EQqS5iAvxziJOU9iQ   1   1          0            0       208b           208b

3) View a single index

GET: http://127.0.0.1:9200/shopping

============
{
    
    
  "shopping" : {
    
    
    "aliases" : {
    
     },
    "mappings" : {
    
     },
    "settings" : {
    
    
      "index" : {
    
    
        "creation_date" : "1652014034548",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "_jzlUBikRAiqCrewEi4HJQ",
        "version" : {
    
    
          "created" : "7080099"
        },
        "provided_name" : "shopping"
      }
    }
  }
}

4) Delete the index

DELETE /shopping

==============
{
    
    
  "acknowledged" : true
}

3.2 Document Operation

1) Create a document

In Postman, send a POST request to the ES server
http://127.0.0.1:9200/shopping/_doc
If you want to customize the unique identifier, you need to specify
http://127.0.0.1:9200/shopping/_doc when creating /1
Note here: If the data primary key is specified when adding data, the request method can also be PUT

# 指定 _id
POST /shopping/_doc/1
# 默认 _id
POST /shopping/_doc
{
    
    
	"title":"小米手机",
	"category": "小米",
	"images":"http://www.baidu.com/xm.jpg",
	"price":3999.00
}
====================
{
    
    
  "_index" : "shopping",
  "_type" : "_doc",
  "_id" : "_eG3o4AB0LSa-GvBMnym",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    
    
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

2) View documentation

When viewing a document, it is necessary to specify the unique identifier of the document, which is similar to
the primary key query of data in MySQL

GET /shopping/_doc/1

=============
{
    
    
  "_index" : "shopping",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 10,
  "_seq_no" : 10,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    
    
    "title" : "小米手机",
    "category" : "小米",
    "images" : "http://www.baidu.com/xm.jpg",
    "price" : 3999.0
  }
}

3) Modify the document as a whole

Just like adding a new document, enter the same
URL address request, if the request body changes, the original data content will be overwritten
in
Postman, and a POST request will be sent to the ES server http://127.0.0.1:9200/shopping/_doc/ 1

POST /shopping/_doc/1
{
    
    
	"title":"VIVO手机",
	"category": "VIVO",
	"images":"http://www.baidu.com/vivo.jpg",
	"price":6999.00
}

==============
{
    
    
  "_index" : "shopping",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 11,
  "result" : "updated",
  "_shards" : {
    
    
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 11,
  "_primary_term" : 1
}

4) Partially modify the document (modify the field value)

When modifying data, you can also only modify the local information of a given piece of data

POST /shopping/_update/1
{
    
    
	"doc":{
    
    
		"price":5999.00
	}
}

======================
{
    
    
  "_index" : "shopping",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 12,
  "result" : "updated",
  "_shards" : {
    
    
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 12,
  "_primary_term" : 1
}

5) Delete document

Deleting a document is not immediately removed from disk, it is just marked as deleted (tombstone).

DELETE /shopping/_doc/1

=================
{
    
    
  "_index" : "shopping",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 22,
  "result" : "deleted",
  "_shards" : {
    
    
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 22,
  "_primary_term" : 1
}

6) Conditionally delete documents

先添加数据
POST /shopping/_doc/11
{
    
    
	"title":"小米手机",
	"category": "小米",
	"images":"http://www.baidu.com/xm.jpg",
	"price":3999.00
}
POST /shopping/_doc/12
{
    
    
	"title":"华为手机",
	"category": "华为",
	"images":"http://www.baidu.com/huawei.jpg",
	"price":6999.00
}
POST /shopping/_doc/13
{
    
    
	"title":"VIVO手机",
	"category": "VIVO",
	"images":"http://www.baidu.com/vivo.jpg",
	"price":7999.00
}
==================================
==================================
POST /shopping/_delete_by_query
{
    
    
	"query":{
    
    
		"match":{
    
    
			"price":6999.00
		}
	}
}
=====================
{
    
    
  "took" : 39,
  "timed_out" : false,
  "total" : 1,
  "deleted" : 1,
  "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" : [ ]
}

7) Delete all documents

POST /shopping/_doc/_delete_by_query
{
    
    
  "query": {
    
    
    "match_all": {
    
    }
  }
}

3.3 Mapping operation

1) Create a mapping

删除索引
DELETE /student
创建索引
PUT /student
创建文档
POST /student/_doc/11
{
    
    
	"name":"李太白",
	"sex": "男",
	"age":"22"
}

创建映射
PUT /student/_mapping

=====================
{
    
    
	"properties":{
    
    
		"name":{
    
    
			"type": "text",
			"index": true
		},
		"sex":{
    
    
			"type": "text",
			"index": false
		},
		"age":{
    
    
			"type": "long",
			"index": false
		}
	}
}

字段名: Fill in at will;

type:
  String type :
     text: separable word;
     keyword: non-segmentable, the data will be matched as a complete field;
  Numerical type :
     basic data type: long, integer, short, byte, double, float, half_float;
     high precision type of floating point number: scaled_float;
  Date date type :
  Array array type :
  Object object :

index: Whether to index, the default is true (that is, all fields will be indexed).
  true : the field will be indexed and can be used for searching
  false : the field will not be indexed and cannot be used for searching

store: Whether to store data independently, the default is false,
the original text will be stored in _source, by default, other extracted fields are not stored independently, but extracted from _source. Of course, you can also store a certain field independently, as long as you set "store": true. Obtaining an independently stored field is much faster than parsing from _source, but it will also take up more space, so it should be based on the actual situation business needs to set.

analyzer: Token breaker, the ik_max_word here is to use the ik word breaker;
     1. ik_max_word: The text will be split in the most detailed way, and the index will cover everything.
     2. ik_smart: It will split the text at the coarsest granularity and match it accurately when searching.
  When inserting a document, segment the field of text type and then insert it into the inverted index:
     "analyzer": "ik_max_word",
  when querying, first segment the input of the text type to be queried, and then search in the inverted index:
     "search_analyzer": "ik_smart",

2) View mapping

GET /student/_mapping

========================
{
    
    
  "student" : {
    
    
    "mappings" : {
    
    
      "properties" : {
    
    
        "age" : {
    
    
          "type" : "long",
          "index" : false
        },
        "name" : {
    
    
          "type" : "text"
        },
        "sex" : {
    
    
          "type" : "text",
          "index" : false
        }
      }
    }
  }
}

3) Create an index mapping association at the same time

PUT /my_student
{
    
    
	"settings":{
    
    },
	"mappings":{
    
    
		"properties":{
    
    
			"name":{
    
    
				"type": "keyword",
				"index": true
			},
			nickname:{
    
    
				"type": "text",
				"index": false
			}
			"sex":{
    
    
				"type": "text",
				"index": false
			},
			"age":{
    
    
				"type": "long",
				"index": false
			}
		}
	}
}

3.4 Advanced query

Elasticsearch provides a complete query DSL based on JSON to define queries;

// 创建测试数据:
POST /student/_doc/1001
{
    
    
	"name":"李太白",
	"nickname":"青莲居士",
	"sex":"男",
	"age":33
}

POST /student/_doc/1002
{
    
    
	"name":"李清照",
	"nickname":"易安居士",
	"sex":"女",
	"age":22
}

POST /student/_doc/1003
{
    
    
	"name":"白居易",
	"nickname":"香山居士",
	"sex":"男",
	"age":28
}

POST /student/_doc/1004
{
    
    
	"name":"李贺",
	"nickname":"诗鬼",
	"sex":"男",
	"age":25
}

POST /student/_doc/1005
{
    
    
	"name":"DuFu",
	"nickname":"诗圣",
	"sex":"男",
	"age":36
}

1) Query all documents

GET /student/_search

===================
{
    
    
  "took" : 857,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "11",
        "_score" : 1.0,
        "_source" : {
    
    
          "name" : "李太白",
          "sex" : "男",
          "age" : "22"
        }
      },
      。。。。。。
      。。。。。。
    ]
  }
}

2) Single field matching query match

GET /student/_search
{
    
    
  "query":{
    
    
    "match":{
    
    
      "name": "贺"
    }
  }
}

=========================
{
    
    
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.5697745,
    "hits" : [
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1004",
        "_score" : 1.5697745,
        "_source" : {
    
    
          "name" : "李贺",
          "nickname" : "诗鬼",
          "sex" : "男",
          "age" : 25
        }
      }
    ]
  }
}

3) Multi-field matching query multi_match

GET /student/_search
{
    
    
  "query":{
    
    
    "multi_match":{
    
    
      "query": "贺",
      "fields": ["name","nickname"]
    }
  }
}

=======================
{
    
    
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.5697745,
    "hits" : [
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1004",
        "_score" : 1.5697745,
        "_source" : {
    
    
          "name" : "李贺",
          "nickname" : "诗鬼",
          "sex" : "男",
          "age" : 25
        }
      }
    ]
  }
}

4) Keyword exact query

term
query, precise keyword matching query, without word segmentation for query conditions.
In
Postman, send a GET request to the ES server http://127.0.0.1:9200/student/_search

GET /student/_search
{
    
    
  "query":{
    
    
    "term":{
    
    
      "name": {
    
    
        "value": "DuFu"
      }
    }
  }
}

=======================
试验没有查到数据,好奇怪?????????
{
    
    
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

5) Multi-keyword precise query

termstermSame as query , but it allows you to specify multiple values ​​to match against. Similar to mysql's in.

GET /student/_search
{
    
    
  "query":{
    
    
    "terms":{
    
    
      "name": ["DuFu","李太白"]
    }
  }
}

=======================
试验没有查到数据,好奇怪?????????

6) Specify query field 1

By default, Elasticsearch will return all the fields in the document stored in _source in the search results.
Add _source filtering to get some of the fields.

GET /student/_search
{
    
    
  "_source": ["name","nickname"],
  "query":{
    
    
    "match":{
    
    
      "name": "白居易"
    }
  }
}

=====================
{
    
    
  "took" : 450,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 4.4009094,
    "hits" : [
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1003",
        "_score" : 4.4009094,
        "_source" : {
    
    
          "name" : "白居易",
          "nickname" : "香山居士"
        }
      },
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1001",
        "_score" : 1.2052968,
        "_source" : {
    
    
          "name" : "李太白",
          "nickname" : "青莲居士"
        }
      }
    ]
  }
}

7) Specify query field 2

includes: to specify the fields you want to display;
excludes: to specify the fields you don’t want to display;

GET /student/_search
{
    
    
  "_source":{
    
    
    "excludes": ["name","nickname"]
  },
  "query":{
    
    
    "match":{
    
    
      "name": "白居易"
    }
  }
}

==============================
{
    
    
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 3.309578,
    "hits" : [
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1003",
        "_score" : 3.309578,
        "_source" : {
    
    
          "sex" : "男",
          "age" : 28
        }
      },
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1001",
        "_score" : 0.79423964,
        "_source" : {
    
    
          "sex" : "男",
          "age" : 33
        }
      }
    ]
  }
}

8) Combined query

boolCombine various other queries by means of must, must_ not, should

GET /student/_search
{
    
    
	"query": {
    
    
		"bool": {
    
    
			"must": [
				{
    
    
					"match": {
    
    
						"name":"白居易"
					}
				}
			],
			"must_not": [
				{
    
    
					"match": {
    
    
						"age":"28"
					}
				}
			],
			"should": [
				{
    
    
					"match": {
    
    
						"sex": "女"
					}
				}
			]
		}
	}
}

==================
{
    
    
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.79423964,
    "hits" : [
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1001",
        "_score" : 0.79423964,
        "_source" : {
    
    
          "name" : "李太白",
          "nickname" : "青莲居士",
          "sex" : "男",
          "age" : 33
        }
      }
    ]
  }
}

9) range query

GET /student/_search
{
    
    
  "query":{
    
    
    "range":{
    
    
      "age":{
    
    
        "gte": 33,
        "lte": 36
      }
    }
  }
}

===================
{
    
    
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1001",
        "_score" : 1.0,
        "_source" : {
    
    
          "name" : "李太白",
          "nickname" : "青莲居士",
          "sex" : "男",
          "age" : 33
        }
      },
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1005",
        "_score" : 1.0,
        "_source" : {
    
    
          "name" : "DuFu",
          "nickname" : "诗圣",
          "sex" : "男",
          "age" : 36
        }
      }
    ]
  }
}

10) Fuzzy query

Returns documents that contain terms similar to the search term.
Edit distance is the number of one character changes required to convert one term into another. These changes can include:

 Change character ( box -> fox)
 Delete character ( black -> lack)
 Insert character ( sic -> sick)
 Transpose two adjacent characters ( act -> cat)

To find similar terms,
a fuzzy query creates a set of all possible variations or expansions of a search term within a specified edit distance. The query then returns an exact match for each extension.
Modify edit distance by fuzziness. The default value is generally used AUTO, and the edit distance is generated according to the length of the term.

GET /student/_search
{
    
    
  "query":{
    
    
    "fuzzy":{
    
    
      "name":{
    
    
        "value": "白"
      }
    }
  }
}

=====================
{
    
    
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.79423964,
    "hits" : [
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1001",
        "_score" : 0.79423964,
        "_source" : {
    
    
          "name" : "李太白",
          "nickname" : "青莲居士",
          "sex" : "男",
          "age" : 33
        }
      },
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1003",
        "_score" : 0.79423964,
        "_source" : {
    
    
          "name" : "白居易",
          "nickname" : "香山居士",
          "sex" : "男",
          "age" : 28
        }
      }
    ]
  }
}

11) Single field sorting

sort allows us to sort by different fields, and specify the sorting method through order. desc descending order, asc ascending order.

GET /student/_search
{
    
    
  "_source": {
    
    
    "excludes": [
      "nickname",
      "sex"
    ]
  },
  "query": {
    
    
    "match": {
    
    
      "name": "李太白"
    }
  },
  "sort": [
    {
    
    
      "age": {
    
    
        "order": "desc"
      }
    }
  ]
}

==============================
{
    
    
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    
    "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 },
  "hits" : {
    
    
    "total" : {
    
    "value" : 4, "relation" : "eq" },
    "max_score" : null,
    "hits" : [
      {
    
    "_index" : "student", "_type" : "_doc", "_id" : "1001", "_score" : null, 
	   "_source" : {
    
    "name" : "李太白", "age" : 33},
        "sort" : [ 33 ]
      },
      {
    
    "_index" : "student", "_type" : "_doc", "_id" : "1003", "_score" : null,
       "_source" : {
    
    "name" : "白居易", "age" : 28},
        "sort" : [ 28 ]
      },
      {
    
    "_index" : "student", "_type" : "_doc", "_id" : "1004", "_score" : null,
       "_source" : {
    
    "name" : "李贺", "age" : 25},
        "sort" : [ 25 ]
      },
      {
    
    "_index" : "student", "_type" : "_doc", "_id" : "1002", "_score" : null,
       "_source" : {
    
    "name" : "李清照", "age" : 22},
        "sort" : [ 22 ]
      }
    ]
  }
}

12) Multi-field sorting

GET /student/_search
{
    
    
  "_source": {
    
    
    "excludes": [
      "nickname",
      "sex"
    ]
  },
  "query": {
    
    
    "match_all": {
    
    
      
    }
  },
  "sort": [
    {
    
    
      "age": {
    
    
        "order": "desc"
      }
    },
    {
    
    
      "score": {
    
    
        "order": "asc"
      }
    }
  ]
}

=========================

13) Highlight query

When performing a keyword search, the keywords in the searched content will be displayed in different colors, which is called highlighting.
When using
match query, add a highlight attribute:
 pre_tags: pre-tags
 post_tags: post-tags
 fields: fields that need to be highlighted
 title: declare that the title field needs to be highlighted, and you can set this field later Unique configuration, can also be empty

GET /student/_search
{
    
    
	"query":{
    
    
		"match":{
    
    
			"name":"贺"
		}
	},
	"highlight":{
    
    
		"pre_tags": "<font color='red'>",
		"post_tags": "</font>",
		"fields":{
    
    
			"name":{
    
    }
		}
	}
}

======================
{
    
    
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.4877305,
    "hits" : [
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1004",
        "_score" : 1.4877305,
        "_source" : {
    
    
          "name" : "李贺",
          "nickname" : "诗鬼",
          "sex" : "男",
          "age" : 25
        },
        "highlight" : {
    
    
          "name" : [
            "李<font color='red'>贺</font>"
          ]
        }
      }
    ]
  }
}

14) Paging query

from: The starting index of the current page, starting from 0 by default. from = ( page N um − 1 ) ∗ size from = (pageNum-1) * sizefrom=( p a g e N u m1)s i ze
size: how many items are displayed on each page

GET /student/_search
{
    
    
  "from": 0,
  "size": 2,
  "_source": {
    
    
    "excludes": [
      "nickname",
      "sex"
    ]
  },
  "query": {
    
    
    "match_all": {
    
    
      
    }
  },
  "sort": [
    {
    
    
      "age": {
    
    
        "order": "desc"
      }
    }
  ]
}

================
{
    
    
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1005",
        "_score" : null,
        "_source" : {
    
    
          "name" : "DuFu",
          "age" : 36
        },
        "sort" : [
          36
        ]
      },
      {
    
    
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1001",
        "_score" : null,
        "_source" : {
    
    
          "name" : "李太白",
          "age" : 33
        },
        "sort" : [
          33
        ]
      }
    ]
  }
}

15) Aggregate query

Take the maximum value max for a field

GET /student/_search
{
    
    
  "aggs":{
    
    
    "max_age":{
    
    
      "max":{
    
    "field":"age"}
    }
  },
  "size":0
}

Take the minimum value min for a field

GET /student/_search
{
    
    
  "aggs":{
    
    
    "min_age":{
    
    
      "min":{
    
    "field":"age"}
    }
  },
  "size":0
}

sum a field

GET /student/_search
{
    
    
  "aggs":{
    
    
    "sum_age":{
    
    
      "sum":{
    
    "field":"age"}
    }
  },
  "size":0
}

Take the average avg of a field

GET /student/_search
{
    
    
  "aggs":{
    
    
    "avg_age":{
    
    
      "avg":{
    
    "field":"age"}
    }
  },
  "size":0
}

Deduplicate the value of a field and then take the total

GET /student/_search
{
    
    
  "aggs":{
    
    
    "distinct_age":{
    
    
      "cardinality":{
    
    "field":"age"}
    }
  },
  "size":0
}

State aggregation
stats aggregation, return count max min avg and sum five indicators for a field at one time.

GET /student/_search
{
    
    
  "aggs":{
    
    
    "stats_age":{
    
    
      "stats":{
    
    "field":"age"}
    }
  },
  "size":0
}

16) Bucket aggregation query

Bucket aggregation is equivalent to group bythe statement
Aggregation terms, group statistics

GET /student/_search
{
    
    
  "aggs":{
    
    
    "age_group_by":{
    
    
      "terms":{
    
    "field":"age"}
    }
  },
  "size":0
}

Aggregation is performed under the terms grouping

GET /student/_search
{
    
    
  "aggs":{
    
    
    "age_group_by":{
    
    
      "terms":{
    
    "field":"age"}
    }
  },
  "size":0
}

4 my test

GET /_cat/indices?v

PUT /mydrug

POST /mydrug/_mapping
{
    
    
	"properties":{
    
    
		"name":{
    
    
			"type": "text",
			"index": true
		},
		"jianpin":{
    
    
			"type": "text",
			"index": true
		},
		"quanpin":{
    
    
			"type": "text",
			"index": true
		}
	}
}

GET /mydrug/_mapping


POST /mydrug/_doc/1001
{
    
    
	"name":"北豆根",
	"jianpin":"bdg",
	"quanpin":"beidougen"
}

POST /mydrug/_doc/1002
{
    
    
	"name":"麸炒冬瓜子",
	"jianpin":"fcdgp",
	"quanpin":"fuchaodongguazi"
}

POST /mydrug/_doc/1003
{
    
    
	"name":"当归尾",
	"jianpin":"dgw",
	"quanpin":"dangguiwei"
}

POST /mydrug/_doc/1004
{
    
    
	"name":"山豆根",
	"jianpin":"sdg",
	"quanpin":"shandougen"
}

POST /mydrug/_doc/1005
{
    
    
	"name":"冬瓜皮",
	"jianpin":"dgp",
	"quanpin":"dongguapi"
}

POST /mydrug/_doc/1006
{
    
    
	"name":"当归",
	"jianpin":"dg",
	"quanpin":"danggui"
}

GET /mydrug/_search
{
    
    
  "query":{
    
    
    "match":{
    
    
      "jianpin": "dg"
    }
  }
}

GET /mydrug/_search
{
    
    
  "query":{
    
    
    "match_phrase":{
    
    
      "jianpin": "dg"
    }
  }
}

GET /mydrug/_search
{
    
    
  "query":{
    
    
    "match_phrase_prefix":{
    
    
      "jianpin": "dg"
    }
  }
}

GET /mydrug/_search
{
    
    
  "query":{
    
    
    "match":{
    
    
      "name": "当"
    }
  }
}

GET /mydrug/_search
{
    
    
  "query":{
    
    
    "match":{
    
    
      "name": "皮"
    }
  }
}















Guess you like

Origin blog.csdn.net/Michael_lcf/article/details/124601589