学习elasticsearch查询用法的时候,发现这篇文章写得很详细,为以后方便查看,就直接搬过来了,原文链接在下面。
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u014646662/article/details/89010759
1、数据准备
put lib3
{
"settings":{
"number_of_shards":3,
"number_of_replicas":0
},
"mappings":{
"user":{
"properties":{
"name":{"type":"text"},
"address":{"type":"text"},
"age":{"type":"integer"},
"interests":{"type":"text"},
"birthday":{"type":"date"}
}
}
}
}
post /lib3/user
{
"name":"lisi",
"address":"shandong",
"age":18,
"interests":"youyong shufa changge tiaowu",
"birthday":"2001-01-19"
}
post /lib3/user
{
"name":"wangwu",
"address":"zhejiang",
"age":22,
"interests":"youyong shufa",
"birthday":"1997-01-19"
}
post /lib3/user
{
"name":"zhangsan",
"address":"zhejiang",
"age":20,
"interests":"youyong shufa changge changpao",
"birthday":"1999-08-29"
}
post /lib3/user
{
"name":"youyong",
"address":"zhejiang",
"age":20,
"interests":"youyong shufa changge changpao",
"birthday":"1999-08-29"
}
测试一下
1 get /lib3/user/_search?q=name:lisi 2 get /lib3/user/_search?q=name:wangwu&sort=age:desc
2、term查询和terms查询
- term query会去倒排索引中寻找确切的term,它并不知道分词器的存在。这种查询适合keyword 、numeric、date。
- term:查询某个字段里含有某个关键词的文档
- terms:查询某个字段里含有多个关键词的文档
get /lib3/user/_search/
{
"query":{"term":{ "interests":"youyong"}}
}
get lib3/user/_search/
{
"query":{"terms":{"interests":["shufa","youyong"]}}
}
3、控制查询返回的数量
get lib3/user/_search
{
"from":0,
"size":2,
"query":{
"terms":{
"interests": ["changge","tiaowu"]
}
}
}
4、返回版本号
get lib3/user/_search
{
"version":true,
"query":{
"term":{"interests": "changge"}
}
}
5、match查询
- match query知道分词器的存在,会对filed进行分词操作,然后再查询
- match_all:查询所有文档
- multi_match:可以指定多个字段
- match_phrase:短语匹配查询,ElasticSearch引擎首先分析(analyze)查询字符串,从分析后的文本中构建短语查询,这意味着必须匹配短语中的所有分词,并且保证各个分词的相对位置不变
GET lib3/user/_search
{
"query":{"match":{"age": 20}}
}
GET lib3/user/_search
{
"query":{"match":{"interests": "changge"}}
}
GET lib3/user/_search
{
"query":{
"match_all": {}
}
}
GET lib3/user/_search
{
"query":{
"multi_match": {
"query": "youyong",
"fields":["interests","name"]
}
}
}
get lib3/user/_search
{
"query":{
"match_phrase":{"interests": "youyong shufa"}
}
}
6、指定返回的字段、显示字段
get lib3/user/_search
{
"_source":["name","age"],
"query":{
"match": {
"interests": "changge"
}
}
}
7、显示要的字段、去除不需要的字段
get lib3/user/_search
{
"query":{
"match_all": {}
},
"_source":{
"includes": "addr*",
"excludes": ["name","bir*"]
}
}
8、排序
- 前缀匹配查询"match_phrase_prefix",
- 使用sort实现排序:desc:降序,asc升序
GET /lib3/user/_search
{
"query": {
"match_phrase_prefix": {
"interests": "you"
}
},
"sort":[
{
"age":{"order": "desc"}
}
]
}
9、 范围查询
- range:实现范围查询
- 参数:from,to,include_lower,include_upper,boost
- include_lower:是否包含范围的左边界,默认是true
- include_upper:是否包含范围的右边界,默认是true
GET /lib3/user/_search
{
"query": {
"range": {
"birthday": {
"from": "1990-10-10",
"to": "2000-05-01",
"include_lower": true,
"include_upper": false
}
}
}
}
GET /lib3/user/_search
{
"query": {
"range": {
"age": {
"from": 18,
"to": 25,
"include_lower": true,
"include_upper": false
}
}
}
}
10、wildcard查询
- 允许使用通配符* 和 ?来进行查询
- *代表0个或多个字符
- ?代表任意一个字符
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "wang*"
}
}
}
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "li?i"
}
}
}
11、fuzzy实现模糊查询
- value:查询的关键字
- boost:查询的权值,默认值是1.0
- min_similarity:设置匹配的最小相似度,默认值为0.5,对于字符串,取值为0-1(包括0和1);对于数值,取值可能大于1;对于日期型取值为1d,1m等,1d就代表1天
- prefix_length:指明区分词项的共同前缀长度,默认是0
- max_expansions:查询中的词项可以扩展的数目,默认可以无限大
GET /lib3/user/_search
{
"query": {
"fuzzy": {
"interests": "chagge"
}
}
}
GET /lib3/user/_search
{
"query": {
"fuzzy": {
"interests": {
"value": "chagge"
}
}
}
}
12、高亮搜索结果
GET /lib3/user/_search
{
"query":{
"match":{
"interests": "changge"
}
},
"highlight": {
"fields": {
"interests": {}
}
}
}