#--------------------------高级查询 查询操作--------------------------#
#1、match_all关键字: 返回索引中的全部文档
GET /products/_search
{
“query”: {
“match_all”: {}
}
}
#2、term 关键字: 用来使用关键词查询
GET /products/_search
{
“query”: {
“term”: {
“price”: {
“value”: 4999
}
}
}
}
#NOTE1: 通过使用term查询得知ES中默认使用分词器为标准分词器(StandardAnalyzer),标准分词器对于英文单词分词,对于中文单字分词。
#NOTE2: 通过使用term查询得知,在ES的Mapping Type 中 keyword , date ,integer, long , double , boolean or ip 这些类型不分词,只有text类型分词。
#3、range 关键字: 用来指定查询指定范围内的文档
GET /products/_search
{
“query”: {
“range”: {
“price”: {
“gte”: 1400,
“lte”: 9999
}
}
}
}
#4、prefix 关键字: 用来检索含有指定前缀的关键词的相关文档
GET /products/_search
{
“query”: {
“prefix”: {
“title”: {
“value”: “ipho”
}
}
}
}
#5、wildcard 关键字: 通配符查询 ? 用来匹配一个任意字符 * 用来匹配多个任意字符
GET /products/_search
{
“query”: {
“wildcard”: {
“description”: {
“value”: “iphon*”
}
}
}
}
#6、ids 关键字 : 值为数组类型,用来根据一组id获取多个对应的文档
GET /products/_search
{
“query”: {
“ids”: {
“values”: [“E7MbhY8BTmd2OsNDpakT”,“FLMbhY8BTmd2OsNDpaka”]
}
}
}
#这里查询条件id值需要换成自己创建索引后的id
#7、fuzzy 关键字: 用来模糊查询含有指定关键字的文档
GET /products/_search
{
“query”: {
“fuzzy”: {
“description”: “iphooone”
}
}
}
#注意: fuzzy 模糊查询 最大模糊错误 必须在0-2之间
#搜索关键词长度为 2 不允许存在模糊
#搜索关键词长度为3-5 允许一次模糊
#搜索关键词长度大于5 允许最大2模糊
#8、布尔查询[bool]
#bool 关键字: 用来组合多个条件实现复杂查询
#must: 相当于&& 同时成立
#should: 相当于|| 成立一个就行
#must_not: 相当于! 不能满足任何一个
GET /products/_search
{
“query”: {
“bool”: {
“must”: [
{“term”: {
“price”: {
“value”: 4999
}
}}
]
}
}
}
#9、多字段查询[multi_match]
GET /products/_search
{
“query”: {
“multi_match”: {
“query”: “iphone13 毫”,
“fields”: [“title”,“description”]
}
}
}
#注意: 字段类型分词,将查询条件分词之后进行查询改字段 如果该字段不分词就会将查询条件作为整体进行查询
#10默认字段分词查询[query_string]
GET /products/_search
{
“query”: {
“query_string”: {
“default_field”: “description”,
“query”: “屏幕真的非常不错”
}
}
}
#注意: 查询字段分词就将查询条件分词查询 查询字段不分词将查询条件不分词查询
#11、高亮查询[highlight]
#(1)highlight 关键字: 可以让符合条件的文档中的关键词高亮
GET /products/_search
{
“query”: {
“term”: {
“description”: {
“value”: “iphone”
}
}
},
“highlight”: {
“fields”: {
““:{}
}
}
}
#(2)自定义高亮html标签: 可以在highlight中使用pre_tags和post_tags
GET /products/_search
{
“query”: {
“term”: {
“description”: {
“value”: “iphone”
}
}
},
“highlight”: {
“post_tags”: [”“],
“pre_tags”: [”"],
“fields”: {
"”:{}
}
}
}
#(3)多字段高亮 使用require_field_match开启多个字段高亮
GET /products/_search
{
“query”: {
“term”: {
“description”: {
“value”: “iphone”
}
}
},
“highlight”: {
“require_field_match”: “false”,
“post_tags”: [“”],
“pre_tags”: [“”],
“fields”: {
“*”:{}
}
}
}
#12、size 关键字: 指定查询结果中返回指定条数。 默认返回值10条
GET /products/_search
{
“query”: {
“match_all”: {}
},
“size”: 2
}
#13、from 关键字: 用来指定起始返回位置,和size关键字连用可实现分页效果
GET /products/_search
{
“query”: {
“match_all”: {}
},
“size”: 2,
“from”: 0
}
#14、指定字段排序[sort]
GET /products/_search
{
“query”: {
“match_all”: {}
},
“sort”: [
{
“price”: {
“order”: “desc”
}
}
]
}
#15、_source 关键字: 是一个数组,在数组中用来指定展示那些字段
GET /products/_search
{
“query”: {
“match_all”: {}
},
“_source”: [“title”,“description”]
}