# 基本查询
PUT /lib3
{
"settings":{
"number_of_shards": 3,
"number_of_replicas": 0
},
"mappings": {
"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"}
}
}
}
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": 23,
"birthday": "1998-10-12",
"interests": "喜欢喝酒,锻炼,说相声"
}
PUT /lib3/user/4
{
"name":"王五",
"address":"北京海淀清河",
"age": 26,
"birthday": "1995-10-12",
"interests": "喜欢编程,听音乐,旅游"
}
PUT /lib3/user/5
{
"name":"张三",
"address":"北京海淀清河",
"age": 29,
"birthday": "1988-10-12",
"interests": "喜欢摄影,听音乐,跳舞"
}
#------------------start term-------------------------
#查询name含有赵关键词
GET /lib3/user/_search/
{
"from": 0,
"size": 1,
"query":{
"term": {"name": "赵"}
}
}
#查询interests含有"喝酒","唱歌"关键词, 差不出来!!!
GET /lib3/_search/
{
"query":{
"terms":
{"interests": ["喝酒","唱歌"]}
}
}
#from size
GET /lib3/_search/
{
"from": 0,
"size": 1,
"query":{
"term": {"name": "赵"}
}
}
#from size
GET /lib3/_search/
{
"from": 0,
"size": 1,
"version":true,
"query":{
"term": {"name": "赵"}
}
}
#------------------start match-------------------------
#match 知道分词器的存在
GET /lib3/_search/
{
"query":{
"match": {"name": "赵六"}
}
}
GET /lib3/_search/
{
"query":{
"match_all": {}
}
}
GET /lib3/_search/
{
"query":{
"multi_match":
{
"query": "唱歌",
"fields": ["interests", "name"]
}
}
}
#match_phrase 短语匹配查询
GET /lib3/_search/
{
"query":{
"match_phrase":
{
"interests": "锻炼,说相声"
}
}
}
#_source 控制返回字段
GET /lib3/_search/
{
"query":{
"match_all": {}
},
"_source": {
"includes":["name","address"],
"excludes":["age","birthday"]
}
}
GET /lib3/user/_search/
{
"query":{
"match_all": {}
},
"_source": {
"includes":["add**"],
"excludes":["age","birthday"]
}
}
# 排序
GET /lib3/_search/
{
"query":{
"match_all": {}
},
"sort":[
{
"age": {"order":"asc"}
}
]
}
# 前缀匹配 match_phrase_prefix
GET /lib3/_search/
{
"query":{
"match_phrase_prefix":
{
"name": {
"query": "赵"
}
}
}
}
# 范围查询 range
GET /lib3/_search/
{
"query":{
"range":{
"age": {
"gte": 20,
"lte": 25
}
}
}
}
# whildcard 查询
#?一个
GET /lib3/_search/
{
"query":{
"wildcard":{
"name": "li?i"
}
}
}
# *多个
GET /lib3/_search/
{
"query":{
"wildcard":{
"name": "l*i"
}
}
}
# fuzzy实现模糊查询
GET /lib3/_search/
{
"query":{
"fuzzy":{
"name": "赵"
}
}
}