ElasticSearch进阶_查询

1.前期总结

Mappings详解

1.1 在ElasticSearch集群中创建索引

(1)没有mappings属性的索引库

语法:
PUT /索引名称  (不需要任何参数)

(2)创建有mappings属性的索引库
需要在raw中添加json数据

语法:
PUT /索引名称
{
	"mappings":{
		"article":{
			"properties":{
				"id":{
					"type":"long",
					"store":true
				},
				"title":{
					"type":"text",
					"store":true,
					"index":true,
					"analyzer":"ik_smart"
				},
				"content":{
					"type":"text",
					"store":true,
					"index":true,
					"analyzer":"ik_smart"
				}
			}
		}
	}
}

两者最大的差异在于

  • 没有mappings属性的索引库中往type中插入的文档字段的类型是任意类型的
  • 有mappings属性的索引库中往type中插入的文档字段的类型是限定的

如果某个索引一开始并没有创建mappings属性,也可以后续添加

语法:
POST /索引名称/类型/_mappings
{
  "properties": {
    "id": {
      "type": "long",
      "store": true
    },
    "title": {
      "type": "text",
      "store": true,
      "index": true,
      "analyzer": "standard"
    },
    "content": {
      "type": "text",
      "store": true,
      "index": true,
      "analyzer": "standard"
    }
  }
}

1.2 向索引库添加文档
语法:
POST  /索引名称/类型/文档id
{
	"field":"value"
}

如果不设置文档id,那么最终添加的文档,其id为一个随机数

注意:如果添加的文档,在索引库中没有对应的type,那么当我们第一次把文档添加到索引库时,会创建对应的mapping
例如:我们现在有一个wxf3的索引库,其mappings是空的,现在我们需要往这个索引库中添加一个文档
我们选择添加到wxf3下的info类型中(当然这个info类型事先也是不存在的)

POST /wxf3/info
{
  "id": 1,
  "title": 17,
  "content": "祝贺祖国七十周岁"
}

当我们第一次添加后,在wxf3中就会生成对应的mappings,之后再往info中添加文档,就要遵守这个mappings
在这里插入图片描述

2.基本查询

2.1 指定index以及type的搜索
GET http://127.0.0.1:9201/index_hello/article/_search?q=title:八荣八耻
其中q表示搜索的意思,后面加上要搜的字段和值

也可以直接在head中搜索
在这里插入图片描述

2.2 指定index,没有type的搜索
GET http://127.0.0.1:9201/index_hello/_search?q=title:八荣八耻
2.3 既没有index也没有type的搜索
GET http://127.0.0.1:9201/_search?q=title:八荣八耻

3.term查询

term查询:查询某个字段里有某个关键词的文档

语法:

GET /索引/类型/_search
{
	"query":{
		"term":{
			"field":"value"
		}
	}
}

举例:
我们在两个索引下分别插入一条记录
在这里插入图片描述
(1)如果我们想要在wxf2中查询title中有chinese的

POST  /wxf1/_search
{
	"query":{
		"term":{
			"title":"chinese"
		}
	}
}

term:虽然title是chinese peopel,但是只要其中有一个关键词符合,就能搜索到,但是如果搜的是"chines"就搜索不到

(2)查询所有索引库中title中有chinese的

POST  /wxf1,wxf2/_search
{
	"query":{
		"term":{
			"title":"chinese"
		}
	}
}

查询到的就是这两个索引库中title中有chinese的记录

3.1 terms查询

terms:查询某个字段中有多个关键词的文档
minimun_match:最小匹配集,1时说明两个关键词最少有一个,2时说明这两个关键词都得存在

3.2 对查询结果进行分页

from和size,相当于mysql中的limit
from:从哪个结果开始返回
size:返回的结果数

POST  http://localhost:9202/index_hello/_search
{	
	"from":1,
	"size":10,
	"query":{
		"term":{
			"title":"谁是"
		}
	}
}

3.match查询

match查询可以接收文字,数字日期等数据类型
match跟term的区别是,match查询时,ES会根据你给定的字段提供合适的分词器,而term不会有分词器分析的过程
用法和term一致
es中的term和match的区别

match和term最大的差别在于:match会对查找的词进行分词后,再去匹配,只要有一个符合就命中
例如:

POST  http://localhost:9202/wxf1,wxf2/_search
{	
	"query":{
		"match":{
			"title":"chinese people"
		}
	}
}

首先将chinese people分词成"chinese"和"people",只要符合其中一个词就算命中
在这里插入图片描述

3.1 match_all

查询指定索引下所有的文档)

POST  http://localhost:9202/wxf1,wxf2/_search
{	
	"query":{
		"match_all":{}
	}
}

查询结果是wxf1和wxf2下所有的文档信息

3.2 match_phrase

match_phrase查询
短语查询,slop定义是关键词中隔多少未知单词

如果我们想要查找"chinese people"这个短语,用match肯定是不行的,此时就要用match_phrase

POST  http://localhost:9202/wxf1,wxf2/_search
{	
	"query":{
		"match_phrase":{
			"title":"chinese people"
		}
	}
}

此时就只会命中title中包含"chinese people"这个短语的文档了

再比如:
在这里插入图片描述

POST  http://localhost:9202/wxf1/_search
{	
	"query":{
		"match_phrase":{
			"content": {
				"query":"we,hero",
				"slop":1
			}
		}
	}
}
3.3 multi_match

可以指定多个字段进行查询

elasticsearch term match multi_match区别

4.排序

通过sort将结果进行排序
desc:降序
asc:升序

一般我们分页获取时,都会使用排序功能,即排序后,再分页获取

POST  http://localhost:9201/_search
{	
	"from":1,
	"size":10,
	"query":{
		"match":{
			"title":"谁是"
		}
	},
	"sort":{
		"id":{
			"order":"desc"
		}
	}
}

5.控制范围range

range:查询,范围查询
有from,to,include_lower,include_upper,boost这些参数
include_lower:是否包含左边的边界,默认为true
include_upper:是否包含右边的边界,默认为true

例如:wxf1中有date这个字段(它也是date类型的)
在这里插入图片描述
我们想要搜索时间在"2015/01/01 12:10:30"到"2015/01/03 10:10:30",但不包括前者

POST  http://localhost:9202/_search/
{
  "query": {
    "range": {
      "date": {
        "from": "2015/01/01 12:10:30",
        "to": "2015/01/03 10:10:30",
        "include_lower": false
      }
    }
  }
}

其中没有精确时间的"2015/01/02"默认为是"2015/01/02 00:00:00"

6.ES中的时间类型总结

ES中的时间类型总结
当我们前期在mapping中并没有设置时间类型的字段,而直接插入时间类型的数据时
例如:

POST  http://localhost:9202/wxf1/article/
{
  "id": 2,
  "title": "japn peple",
  "content": "japn is sun",
  "date": "2015/01/01 12:10:30"
}

而后在mapping中显示的类型如下
在这里插入图片描述

7.wildcard模糊查询

wildcard查询,即模糊查询,可以使用通配符*和?进行查询
*代表一个或多个字符
?仅代表一个字符
注意点:这个查询比较影响性能
在这里插入图片描述

POST  http://localhost:9202/wxf2/article/_search/
{
  "query": {
    "wildcard": {
      "title": "ch*"
    }
  }
}

这两条记录都能查到

8.fuzzy模糊查询

误拼写时的fuzzy模糊搜索技术
ElasticSearch误拼写时的fuzzy模糊搜索技术

value:查询的关键字
boost:设置查询的权值,默认是1.0
min_similarity:设置匹配的最小相似度,默认为0.5
prefix_length:指明区分词项的共同前缀长度,默认是0
max_expansions:指明查询中的词项可扩展数目,默认无限大

2.filter查询

3.组合查询

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值