赞
踩
GET /bank/_search?q=*&sort=account_number:asc
说明:
(1) q=* 查询所有;
(2) sort=account_number:asc 按照account_number升序
GET /bank/_search { "query": { "match_all": {} }, "sort": [ { "account_number": "asc" }, { "balance": "desc" } ], "from": 1, "size": 5, "_source": [ "balance", "firstname" ] }
说明:
(1)先按照account_number升序,再按照balance进行降序
(2)from:从哪一个文档开始
(3)size:需要的个数
GET /bank/_search
{
"query":{
"match":{
"address": "mill lane"
}
}
}
说明:
(1)当match检索的字段是数值型的,会进行精确匹配;当时字符串类型的,会进行模糊匹配;
(2)match会对检索的字段进行分词,如上述例子中,会将 "mill lane"分词,进而分别进行模糊匹配检索
GET /bank/_search
{
"query":{
"match_phrase":{
"address": "mill lane"
}
}
}
说明:
(1)match_phrase不会对要检索的mill lane进行分词,会查询address字段中含有 "mill lane"的文档
GET /bank/_search
{
"query":{
"multi_match": {
"query": "mill movico",
"fields": ["address","city"]
}
}
}
说明:
(1)multi_match用来对多个字段执行相同的查询
(2)multi_match中,fields支持使用通配符
(3)multi_match内部执行查询的方式主要取决于type参数,以下为type类型说明:
类型 | 说 明 |
---|---|
best_fields | (默认)查找与任何字段匹配但使用_score最佳字段中的文档 |
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html
GET /bank/_search { "query": { "bool": { "must": [ { "match": { "address": "Mill" } }, { "match": { "gender": "M" } } ], "must_not": [ { "match": { "firstname": "Forbes" } } ], "should": [ { "match": { "state": "KY" } } ], "filter": [ { "range": { "age": { "gte": 35, "lte": 38 } } } ] } } }
说明:
(1)bool复合查询。用于构造复杂的查询
(2)must: 必须匹配,查询上下文,加分
(3)must_not: 必须不匹配,过滤上下文,过滤
(4)should: 应该匹配,查询上下文,加分
(5)filter: 必须匹配,过滤上下文,过滤
GET /bank/_search
{
"query": {
"term": {
"balance": "45801"
}
}
}
说明:
(1)使用term查询去精确匹配类似price、productID、或者username的非text类型数据;
(2)使用match查询去匹配text类型数据
(3)term:查询某个字段里含有某个关键词的文档
(4)terms:查询某个字段里含有多个关键词的文档
(5).keyword:完全精确匹配字段值
完全精确匹配address="198 Mill Lane"的文档
GET /bank/_search
{
"query":{
"match": {
"address.keyword": "198 Mill Lane"
}
}
}
(6)match_phrase:匹配某个字段中含有该短语的文档
PUT /my_index { "mappings": { "properties": { "age": { "type": "integer" }, "email": { "type": "keyword" }, "name": { "type": "text" } } } }
PUT /my_index/_mapping
{
"properties": {
"employee-id": {
"type": "keyword",
"index": false
}
}
}
此时如果根据employee-id进行索引查询,会报如下错误:
{
...
"reason" : "failed to create query: Cannot search on field [employee-id] since it is not indexed.",
...
}
说明:
(1)employee-id:要新添加的字段名;
(2)type:字段类型;
(3)index:是否可以被查询被索引(默认是true)
(1)我们不可以修改一个已经存在的映射规则或者映射的字段类型(但可以添加新的映射关系,如上);
(2)我们可以通过创建一个新的索引index,并指定好新的映射mapping,然后将旧索引中的数据通过reindex迁移到新索引中。
如果老索引中的文档数据指定了type,则需要指定type类型;如果没有则不需要指定。
POST _reindex
{
"source": {
"index": "bank",
"type": "account"
},
"dest": {
"index": "newbank"
}
}
(1)在https://github.com/medcl/elasticsearch-analysis-ik/releases这个地址下载对应ElasticSearch版本的elasticsearch-analysis-ik-7.9.2.zip(版本一定要对应)
(2)将压缩包中的文件解压,并将所有解压文件拷贝到ES安装目录的plugins目录下。
例如:D:\Software\ElasticSearch7.9.2\elasticsearch-7.9.2\plugins\analysis-ik\目录下(此处需要创建analysis-ik目录)
(3)重启ElasticSearch
现在支持ik_smart(智能分词)与ik_max_word(最大分词组合)两种分词
ik_max_word请求:
POST _analyze
{
"analyzer": "ik_max_word",
"text": "我是中国人"
}
ik_max_word返回:
{ "tokens" : [ { "token" : "我", "start_offset" : 0, "end_offset" : 1, "type" : "CN_CHAR", "position" : 0 }, { "token" : "是", "start_offset" : 1, "end_offset" : 2, "type" : "CN_CHAR", "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" : 3, "end_offset" : 5, "type" : "CN_WORD", "position" : 4 } ] }
ik_smart请求:
POST _analyze
{
"analyzer": "ik_smart",
"text": "我是中国人"
}
ik_smart返回:
{ "tokens" : [ { "token" : "我", "start_offset" : 0, "end_offset" : 1, "type" : "CN_CHAR", "position" : 0 }, { "token" : "是", "start_offset" : 1, "end_offset" : 2, "type" : "CN_CHAR", "position" : 1 }, { "token" : "中国人", "start_offset" : 2, "end_offset" : 5, "type" : "CN_WORD", "position" : 2 } ] }
在ik分词器的config目录下,找到IKAnalyzer.cfg.xml配置文件,我们在此处配置要拓展的词汇的文件的未知,该文件内容如下:
(我的目录:D:\Software\ElasticSearch7.9.2\elasticsearch-7.9.2\plugins\analysis-ik\config\IKAnalyzer.cfg.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict"></entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
说明:
(1)如果要配置远程拓展词典,可以在服务器中搭建nginx服务器,并在nginx服务器中放入拓展词库的txt文件,文件中的内容按照如下换行格式:
测试分词1
测试分词2
(2)此处以学习为目的,在windows本地下(即D:\Software\ElasticSearch7.9.2\elasticsearch-7.9.2\plugins\analysis-ik\config)配置了需要自定义拓展的词汇文件
在config目录下创建extend.dic文件:
王二麻
分词效果
KAnalyzer.cfg.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">extend.dic</entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
先看下如下请求如果未拓展自定义词库的效果
POST _analyze
{
"analyzer": "ik_smart",
"text": "王二麻测试分词效果!"
}
{ "tokens" : [ { "token" : "王", "start_offset" : 0, "end_offset" : 1, "type" : "CN_CHAR", "position" : 0 }, { "token" : "二", "start_offset" : 1, "end_offset" : 2, "type" : "TYPE_CNUM", "position" : 1 }, { "token" : "麻", "start_offset" : 2, "end_offset" : 3, "type" : "CN_CHAR", "position" : 2 }, { "token" : "测试", "start_offset" : 3, "end_offset" : 5, "type" : "CN_WORD", "position" : 3 }, { "token" : "分词", "start_offset" : 5, "end_offset" : 7, "type" : "CN_WORD", "position" : 4 }, { "token" : "效果", "start_offset" : 7, "end_offset" : 9, "type" : "CN_WORD", "position" : 5 } ] }
采用自定义分词词库后
{ "tokens" : [ { "token" : "王二麻", "start_offset" : 0, "end_offset" : 3, "type" : "CN_WORD", "position" : 0 }, { "token" : "测试", "start_offset" : 3, "end_offset" : 5, "type" : "CN_WORD", "position" : 1 }, { "token" : "分词效果", "start_offset" : 5, "end_offset" : 9, "type" : "CN_WORD", "position" : 2 } ] }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。