列出所有索引(数据库)
列出所有索引(列出所有的数据库)
GET /_cat/indices?v
删除索引(数据库)
DELETE /索引名称
添加索引(数据库)
PUT /es
{
“settings”: {
“number_of_replicas”: 1,
“number_of_shards”: 1
}
}
插入数据(插入数据的同时建表)
post /es/user
{
“name”:“张晓东”,
“age”:18
}
es:索引名(数据库)
user:类型名(表名)
name:字段名(列)
“张晓东”、"18"表示值
查看所有数据
get /es/user/_search
took:本次操作花费的时间,单位为毫秒
timed_out:请求是否超时
_shards:说明本次操作共搜索了哪些分片
hits:搜索命中的记录
hits.total : 符合条件的文档总数
hits.hits :匹配度较高的前N个文档
hits.max_score:文档匹配得分,这里为最高分
_score:每个文档都有一个匹配度得分,按照降序排列
_source:显示了文档的原始内容
删除数据
delete /es/user/UoupxHgBnRHM7AQTqhk9
根据id删除
修改数据
POST /es/user/U4utxHgBnRHM7AQTIhmT/_update
{
“doc”:{
“name”:“李朋”
}
}
查询(搜索)
GET /es/user/_search
{
“query”: {
“match”: {
“name”: “张三”
}
},
“size”:2
}
根据name字段进行查询
还可以排序,增加参数order即可
size:取出的记录数量
测试ES中文搜索结果
POST _analyze
{
“text”: “张晓东”
}
从结果可以看出,这种分词把每个汉字都独立分开来了,这对中文分词就没有意义了,所以ES默认的分词器对中文处理是有问题的。好在有很多不错的第三方的中文分词器,可以很好地和ES结合起来使用。在ES中,每种分词器(包括内置的、第三方的)都会有个名称。上面默认的操作,其实用的分词器的名称是standard。下面的请求与前面介绍的请求是等价的,如:
POST _analyze
{
“analyzer”: “standard”,
“text”: “世界如此之大”
}
当我们换一个分词器处理分词时,只需将"analyzer"字段设置相应的分词器名称即可。
ES通过安装插件的方式来支持第三方分词器,对于第三方的中文分词器,比较常用的是中科院ICTCLAS的smartcn和IKAnanlyzer分词器。在本文中,我们介绍IKAnanlyzer分词器(下面简称ik)的使用。
安装ik
git地址下载:https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v6.5.3
下载的版本要和ES的安装版本一致
切换到ES安装的bin目录中运行:(注意别复制,看看版本)
elasticsearch-plugin.bat install file:///c:/elasticsearch-analysis-ik-6.5.3.zip
安装完毕后,发现在ES的安装目录下的plugins目录下多了一个analysis-ik目录
(内容是ik的zip包解压后根目录下的所有文件,一共是5个jar文件和1个properties配置文件)
另外ES的安装目录下的config目录下多了一个analysis-ik目录(内容是ik的zip包解压后根目录下的config目录下所有文件,用于放置ik的自定义词库)
如果没找到config目录不要慌,因为版本不同,config肯定在
ik中文分词器的使用
上面提到,ik提供了两个分词器,分别是ik_max_word 和ik_smart,下面我们分别测试下。
先测试ik_max_word,输入命令如下:
POST _analyze
{
“analyzer”: “ik_max_word”,
“text”: “世界如此之大值的去看看”
}
{
“tokens” : [
{
“token” : “世界”,
“start_offset” : 0,
“end_offset” : 2,
“type” : “CN_WORD”,
“position” : 0
},
{
“token” : “如此之”,
“start_offset” : 2,
“end_offset” : 5,
“type” : “CN_WORD”,
“position” : 1
},
{
“token” : “如此”,
“start_offset” : 2,
“end_offset” : 4,
“type” : “CN_WORD”,
“position” : 2
},
{
“token” : “之大”,
“start_offset” : 4,
“end_offset” : 6,
“type” : “CN_WORD”,
“position” : 3
},
{
“token” : “值”,
“start_offset” : 6,
“end_offset” : 7,
“type” : “CN_CHAR”,
“position” : 4
},
{
“token” : “的”,
“start_offset” : 7,
“end_offset” : 8,
“type” : “CN_CHAR”,
“position” : 5
},
{
“token” : “去看”,
“start_offset” : 8,
“end_offset” : 10,
“type” : “CN_WORD”,
“position” : 6
},
{
“token” : “看看”,
“start_offset” : 9,
“end_offset” : 11,
“type” : “CN_WORD”,
“position” : 7
}
]
}
再测试ik_smart,输入命令如下:
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
},
{
“token” : “之大”,
“start_offset” : 4,
“end_offset” : 6,
“type” : “CN_WORD”,
“position” : 2
},
{
“token” : “值”,
“start_offset” : 6,
“end_offset” : 7,
“type” : “CN_CHAR”,
“position” : 3
},
{
“token” : “的”,
“start_offset” : 7,
“end_offset” : 8,
“type” : “CN_CHAR”,
“position” : 4
},
{
“token” : “去”,
“start_offset” : 8,
“end_offset” : 9,
“type” : “CN_CHAR”,
“position” : 5
},
{
“token” : “看看”,
“start_offset” : 9,
“end_offset” : 11,
“type” : “CN_WORD”,
“position” : 6
}
]
}
比较两个分词器对同一句中文的分词结果,ik_max_word比ik_smart得到的中文词更多(从两者的英文名含义就可看出来),但这样也带来一个问题,使用ik_max_word会占用更多的存储空间。
ik的自定义词典
有时,可能ik自身提供的分词词典无法满足特定的一些需求(如专用名词等),ik提供了自定义词典的功能,也就是用户可以自己定义一些词汇,这样ik就会把它们当作词典中的内容来处理。
举个例子,对于上面例子中的“世界如此之大”这个中文语句,ik词库中不会有“界如此”这样一个单词,假设“界如此”就是一个专用名词,我们希望ik能识别出来。这时就可自定义ik的词典。具体方法是:
1、新建扩展名为dic的文本文件,文件中写入想增加的词条,每个词条单独一行,如文件名是test.dic,文件内容如下:
界如此
高潜
上面例子中有两个自定义词条。
2、将上面的dic文件保存到ES安装目录的config目录下的analysis-ik目录(安装ik插件时产生的目录)下,可以建立子目录,放在子目录下。比如文件的路径如: ** config/analysis-ik/mydic/test.dic**
3、修改ik的配置文件IKAnalyzer.cfg.xml(位于config/analysis-ik目录下),在配置文件中增加如下条目:
mydict/test.dic
这样就将自定义的字典文件加到ik的字典中了。
4、重启ES让生效。
这时我们发起如下的REST请求:
POST _analyze
{
“analyzer”: “ik_max_word”,
“text”: “世界如此之大”
}
响应结果如下:
{
“tokens”: [
{
“token”: “世界”,
“start_offset”: 0,
“end_offset”: 2,
“type”: “CN_WORD”,
“position”: 0
},
{
“token”: “界如此”,
“start_offset”: 1,
“end_offset”: 4,
“type”: “CN_WORD”,
“position”: 1
},
{
“token”: “如此之”,
“start_offset”: 2,
“end_offset”: 5,
“type”: “CN_WORD”,
“position”: 2
},
{
“token”: “如此”,
“start_offset”: 2,
“end_offset”: 4,
“type”: “CN_WORD”,
“position”: 3
},
{
“token”: “之大”,
“start_offset”: 4,
“end_offset”: 6,
“type”: “CN_WORD”,
“position”: 4
}
]
}
可以看出,自定义的“界如此”词条被分词出来了。不过如果我们将analyzer改为ik_smart却发现“界如此”词条没能被识别出来。
PHP操作ES
安装
composer require elasticsearch/elasticsearch
创建索引
$hosts = [
‘127.0.0.1:9200’
];
KaTeX parse error: Undefined control sequence: \Elasticsearch at position 10: client = \̲E̲l̲a̲s̲t̲i̲c̲s̲e̲a̲r̲c̲h̲\ClientBuilder:…hosts)->build();
// 创建索引
$params = [
‘index’ => ‘goods’,
‘body’ => [
‘settings’ => [
‘number_of_shards’ => 5,
‘number_of_replicas’ => 1
],
‘mappings’ => [
‘_doc’ => [
‘_source’ => [
‘enabled’ => true
],
‘properties’ => [
‘title’ => [
‘type’ => ‘keyword’
],
‘desn’ => [
‘type’ => ‘text’,
‘analyzer’ => ‘ik_max_word’,
‘search_analyzer’ => ‘ik_max_word’
]
]
]
]
]
];
$response =
c
l
i
e
n
t
−
>
i
n
d
i
c
e
s
(
)
−
>
c
r
e
a
t
e
(
client->indices()->create(
client−>indices()−>create(params);
更新文档
$hosts = [
‘127.0.0.1:9200’,
];
KaTeX parse error: Undefined control sequence: \Elasticsearch at position 10: client = \̲E̲l̲a̲s̲t̲i̲c̲s̲e̲a̲r̲c̲h̲\ClientBuilder:…hosts)->build();
// 写文档
$params = [
‘index’ => ‘goods’,
‘type’ => ‘_doc’,
‘id’ => $model->id,
‘body’ => [
‘title’ => $model->title,
‘desn’ => $model->desn,
],
];
$response =
c
l
i
e
n
t
−
>
i
n
d
e
x
(
client->index(
client−>index(params);
搜索
$hosts = [
‘127.0.0.1:9200’,
];
KaTeX parse error: Undefined control sequence: \Elasticsearch at position 10: client = \̲E̲l̲a̲s̲t̲i̲c̲s̲e̲a̲r̲c̲h̲\ClientBuilder:…hosts)->build();
$params = [
‘index’ => ‘goods’,
‘type’ => ‘_doc’,
‘body’ => [
‘query’ => [
‘match’ => [
‘title’=>[
‘query’ => ‘手机’
]
]
]
]
];
$results =
c
l
i
e
n
t
−
>
s
e
a
r
c
h
(
client->search(
client−>search(params);
dump($results);