6.1 rest查询
shop/user/1 //查询id =1
shop/user/_search //查询全部
shop/user/1?_source=user_name,area_code //返回指定字段
shop/user/_search?q=user_name:张三 //搜索条件
6.2 term、terms、match等基本查询语法
term:查询某个字段里含有某个关键词的文档
GET /customer/doc/_search/
{
"query": {
"term": {
"title": "blog"
}
}
}
terms:查询某个字段里含有多个关键词的文档
GET /customer/doc/_search/
{
"query": {
"terms": {
"title": [ "blog","first"]
}
}
}
match query 知道分词器的存在,会对field进行分词操作,然后再查询
GET /customer/doc/_search/
{
"query": {
"match": {
"title": "my ss" #它和term区别可以理解为term是精确查询,这边match模糊查询;match会对my ss分词为两个单词,然后term对认为这是一个单词
}
}
}
6.3DSl查询
//查询全部
http://localhost:9200/yuyu/user/_search
{
"query":{
"match_all":{}
}
}
或GET http://localhost:9200/yuyu/user/_search
//分页(以term为例)
GET index/type/_search
{
"from":0,
"size":100,
"query":{
"term":{
"area":"GuangZhou"
}
}
}
//包含指定字段
GET index/type/_search
{
"_source":["hobby", "name"],
"query":{
"term":{
"area":"GuangZhou"
}
}
}
//排序(以term为例)单个字段排序:
GET index/type/_search
{
"query":{
"term":{
"area":"GuangZhou"
}
},
"sort":[
{"user_id":{"order":"asc"}},
{"salary":{"order":"desc"}}
]
}
//增加过滤条件
http://localhost:9200/yuyu/user/_search
{
"query" : {
"bool" : {
"filter" : {
"range" : {
"age" : { "gt" : 10 }
}
},
"must" : {
"match" : {
"user_name" : "我是中国人"
}
}
}
}
}
//高亮展示
http://localhost:9200/yuyu/user/_search
{
"query" : {
"match_phrase" : {
"user_name" : "我是点"
}
},
"highlight": {
"fields" : {
"user_name" : {}
}
}
}