数据检索
CRUD
创建与更新
在http://localhost:9200/<index>/<type>/[<id>]
。其中索引名称可以是任意字符,如果
- 每个类型有自己独立的
ID 空间 - 不同的类型有不同的映射
(Mappings) ,即不同的属性/ 域的建立索引的方案 - 尽可能地在一起搜索请求中只对某个类型或者特定的类型进行搜索
典型的某个
curl -XPUT "http://localhost:9200/movies/movie/1" -d'
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972
}'
POST /website/blog/
{
"title": "My second blog entry",
"text": "Still trying this out...",
"date": "2014/01/01"
}
Search | 搜索
_bulk
端点来在单请求中完成多文档创建操作,不过这里为了简单起见还是分为了多个请求进行执行。_search
这个端点进行的,其标准请求格式为<index>/<type>/_search
,其中
http://localhost:9200/_search - 搜索所有的Index 与Type http://localhost:9200/movies/_search - 搜索Movies 索引下的所有类型http://localhost:9200/movies/movie/_search - 仅搜索包含在Movies 索引Movie 类型下的文档
全文搜索
kill
curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"query_string": {
"query": "kill"
}
}
}'
指定域搜索
在上文简单的全文检索中,我们会搜索每个文档中的所有域。而很多时候我们仅需要对指定的部分域中文档进行搜索操作,譬如我们要搜索仅在标题中出现ford
字段的文档
curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"query_string": {
"query": "ford",
"fields": ["title"]
}
}
}'
Geo
PUT /my_locations
{
"mappings": {
"location": {
"properties": {
"pin": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
}
}
}
PUT /my_locations/location/1
{
"pin" : {
"location" : {
"lat" : 40.12,
"lon" : -71.34
}
}
}
GET /my_locations/location/_search
{
"query": {
"bool" : {
"must" : {
"match_all" : {}
},
"filter" : {
"geo_distance" : {
"distance" : "200km",
"pin.location" : {
"lat" : 40,
"lon" : -70
}
}
}
}
}
}
MySQL => Databases => Tables => Columns/Rows
Elasticsearch => Indices => Types => Documents with Properties
基本的术语名词解释如下:
- 索引
/ Index: 类似于MySQL 中的数据库;
-
类型
/ Type: 类似于MySQL 中的表,我们可以为Type 定义相对应的 映射/ Mappings ( 类似于MySQL 中的Schema ) 以优化索引性能; -
文档
/ Document: 类似于MySQl 中的记录,是信息聚合的最小单元。
与集群相关的名词还包括:
- 集群
/ Cluster: ElastichSearch 可以作为一个独立的搜索服务器工作,也可以在多台协同工作的服务器上运行,统称为一个集群,其中有一台被作为Master ,其他为Slave ;
-
节点
/ Node: 一般来说一个机器部署一个Node 。 -
分片
/ Shard: 指的是一个Index 分成多少份,这些Shards 会分散到各个Node 上面,类似于HDFS 的文件块。 -
副本
/ Replica: 副本是针对每个分片的,可以为一个分片设置多个副本,分布在不同的节点上,即是容错,也可以提高查询任务的性能,原理同HDFS 的文件块副本机制。
鉴于
单机配置
# ElasticSearch 不允许以 root 用户运行,需要创建并且切换用户
useradd elastic
su elastic
# 以 root 用户设置文件最大描述符
# max file descriptors [8192] for elasticsearch process is too low, increase to at least [65536]
$ sudo ulimit -n 65536
# 修改 MMap
$ sudo sysctl -w vm.max_map_count=262144
# max size virtual memory [10018979840] for user [elastic] is too low, increase to [unlimited]
$ vim /etc/security/limits.conf
* hard memlock unlimited
* soft memlock unlimited
* hard nofile 65536
* soft nofile 65536
* - as unlimited
CRUD
Query: 搜索
# 简单查询
GET _search
{
"query": {
"match": {
"${FIELD}": "${TEXT}"
}
}
}
# 复杂查询
GET _search
{
"query": {
"match": {
"${FIELD}": {
"query": "${TEXT}",
"${OPTION}": "${VALUE}"
}
}
}
}
# 多重匹配
"multi_match": {
"query": "Elastic",
"fields": ["user.*", "title^3"],
"type": "best_fields"
}
# 布尔值计算
"bool": {
"must": [],
"must_not": [],
"filter": [],
"should": [],
"minimum_should_match" : 1
}
# 范围查询
"range": {
"age": {
"gte": 10,
"lte": 20,
"boost": 2
}
}
针对于复杂查询,
# 默认查询全部属性
GET /_search?q=pony
# 操作符
GET /_search?q=title:(joli OR code) AND author:"Damien Alexandre"^2
# 通配符或者特殊字符
GET /_search?q=_exists_:title OR title:singl? noneOrAnyChar*cter
# 模糊搜索与范围搜索
GET /_search?q=title:elastichurch~3 AND date:[2016-01-01 TO 2018-12-31]
# Query DSL