本章内容概述
1、基本查询
1、基本查询
1.1 数据准备
创建索引
PUT /lib3
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0
},
"mappings": {
"user":{
"properties":{
"name":{
"type":"text",
"analyzer":"ik_max_word"
},
"address":{
"type":"text",
"analyzer":"ik_max_word"
},
"age":{
"type":"integer"
},
"interests":{
"type":"text",
"analyzer":"ik_max_word"
},
"birthday":{
"type":"date"
}
}
}
}
}
ik带有两个分词器
ik_max_word : 会将文本做最细粒度的拆分;尽可能多的拆分出词语
ik_smart : 会将文本做最粗粒度的拆分;已被分出的词语将不会再次被其他词语占有
添加文档数据
PUT /lib3/user/1
{
"name":"赵六",
"address":"黑龙江省铁岭市",
"age":50,
"birthday":"1970-12-12",
"interests":"喜欢喝酒,锻炼,说相声"
}
PUT /lib3/user/2
{
"name":"赵明",
"address":"北京海淀区清河",
"age":20,
"birthday":"1998-10-12",
"interests":"喜欢喝酒,锻炼,唱歌"
}
PUT /lib3/user/3
{
"name":"lisi",
"address":"北京市朝阳区",
"age":28,
"birthday":"1990-01-11",
"interests":"喜欢喝酒,锻炼,唱歌"
}
PUT /lib3/user/4
{
"name":"王五",
"address":"北京市房山区",
"age":26,
"birthday":"1993-01-11",
"interests":"喜欢编程,听音乐,旅游"
}
PUT /lib3/user/5
{
"name":"张三",
"address":"郑州市二七区",
"age":32,
"birthday":"1988-05-11",
"interests":"喜欢摄影,跳舞,听音乐"
}
PUT /lib3/user/6
{
"name":"小明",
"address":"郑州市二七区",
"age":22,
"birthday":"1988-05-11",
"interests":"喜欢摄影,跳舞,听音乐"
}
1.2 term查询
term query 会去倒排索引中寻找确切的term,不进行分词器分析,文档中必须包含整个搜索的词汇,这种查询适合keyword,mumeric
date等。
1、查询姓名中有“赵”的
GET /lib3/user/_search
{
"query": {
"term": {
"name": "赵"
}
}
}
1.3 terms查询
terms: 查询某个字段中含有多个关键词的文档
1、查询爱好包含喝酒 或 唱歌的
GET /lib3/user/_search
{
"query":{
"terms":{
"interests":["唱歌","喝酒"]
}
}
}
只要包含关键词的一个,都会被查询出来
1.4 match模糊查询
match query 知道分词器的存在,会对字段进行分词操作,然后再查询
1、根据名称模糊查询
GET /lib3/user/_search
{
"query": {
"match": {
"name": "赵明 赵六"
}
}
}
"match":{} 定义模糊查询,类似于like
match查询相当于模糊匹配,只包含其中一部分关键词就行
2、多条件模糊查询 multi_match
GET /lib3/user/_search
{
"query": {
"multi_match": {
"query": "唱歌",
"fields": ["interests","name"]
}
}
}
"query" 后面是跟要查询的内容
"fields" 要查询的字段
1.5 结构化分页查询
1、分页查询
GET /lib3/user/_search
{
"from": 0,
"size": 2,
"query": {
"match": {
"interests": "唱歌"
}
}
}
"from" 开始下标
"size" 数量
注:类似于limit限定查询
1.6 控制显示字段
我们之前的查询是把所有的字段都显示出来,我们可以通过_source来控制要显示的字段:
GET /lib3/user/_search
{
"_source":["name","interests"],
"query":{
"match":{
"interests":"唱歌"
}
}
}
也可以通过includes和excludes来实现:
GET /lib3/user/_search
{
"_source":{
"includes": ["name","interests"],
"excludes": ["birthday","age"]
},
"query":{
"match":{
"interests":"唱歌"
}
}
}
也可以使用通配符来匹配指定的字段:
GET /lib3/user/_search
{
"_source":{
"includes": "a*",
"excludes": ["name","birthday"]
},
"query":{
"match":{
"interests":"唱歌"
}
}
}
1.7 排序
使用sort实现排序, desc 降序 asc 升序
GET /lib3/user/_search
{
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
1.8 前缀匹配
匹配以指定前缀开始的字段
GET /lib3/user/_search
{
"query": {
"match_phrase_prefix": {
"name": "赵"
}
}
}
1.9 范围查询
根据生日范围查询
GET /lib3/user/_search
{
"query": {
"range": {
"birthday": {
"gte": "1990-10-10",
"lte": "2018-05-01"
}
}
}
}
当然,也有人喜欢使用from to
GET /lib3/user/_search
{
"query": {
"range": {
"birthday": {
"from": "1990-10-10",
"to": "2018-05-01"
}
}
}
}
我们可以设置是否包含最大或最小
GET /lib3/user/_search
{
"query": {
"range": {
"age": {
"gte": 20,
"lte": 32,
"include_lower":true,
"include_upper":false
}
}
}
}
1.10 wildcard查询
允许使用通配符*和?来进行查询
* 代表0个或多个字符
? 代表任意一个字符
查询姓名以zhao开头后面任意的
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "赵*"
}
}
}
查询前面任意一个字符,第二个字是“明”的
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "?明*"
}
}
}
1.11 fuzzy实现模糊查询
当我们对于字段值不太记得时,我们可以通过fuzzy进行大致查询,找到对应匹配的值
GET /lib3/user/_search
{
"query": {
"fuzzy": {
"interests": "唱歌"
}
}
}
GET /lib3/user/_search
{
"query": {
"fuzzy": {
"interests": {
"value":"喝酒"
}
}
}
}
1.12 高亮搜索结果
GET /lib3/user/_search
{
"query":{
"match":{
"interests":"唱歌"
}
},
"highlight": {
"fields": {
"interests": {}
}
}
}
ET /lib3/user/_search
{
"query": {
"fuzzy": {
"interests": "唱歌"
}
}
}
GET /lib3/user/_search
{
"query": {
"fuzzy": {
"interests": {
"value":"喝酒"
}
}
}
}