GET _search
{
"query": {
"match_all": {}
}
}
#模拟请求
GET /
# 测试分词器
POST /_analyze
{
"text": "黑马程序员学习java太棒了",
"analyzer": "ik_max_word"
}
# 创建索引库
PUT /heima
{
"mappings": {
"properties": {
"info": {
"type": "text",
"analyzer": "ik_smart"
},
"email": {
"type": "keyword",
"index": false
},
"name": {
"type": "object",
"properties": {
"firstName": {
"type": "keyword"
},
"lastName":{
"type":"keyword"
}
}
}
}
}
}
# 查询
GET /heima
# 修改索引库,添加新字段
PUT /heima/_mapping
{
"properties":{
"age":{
"type":"integer"
}
}
}
# 删除
DELETE /heima
# 插入一个文档
POST /heima/_doc/1
{
"info":"黑马程序员Java讲师",
"email":"[email protected]",
"name":{
"firstName":"云",
"lastName":"赵"
}
}
# 查询文档
GET /heima/_doc/1
# 删除文档
DELETE /heima/_doc/1
#全量修改文件
PUT /heima/_doc/1
{
"info":"黑马程序员Java讲师",
"email":"[email protected]",
"name":{
"firstName":"云",
"lastName":"赵"
}
}
# 局部修改文档字段
POST /heima/_update/1
{
"doc":{
"email":"[email protected]"
}
}
# 酒店的mapping
PUT /hotel
{
"mappings": {
"properties": {
"id":{
"type":"keyword"
},
"name":{
"type": "text",
"analyzer": "ik_max_word",
"copy_to": "all"
},
"address":{
"type": "keyword",
"index":false
},
"price":{
"type":"integer"
},
"score":{
"type":"integer"
},
"brand":{
"type":"keyword",
"copy_to": "all"
},
"city":{
"type":"keyword"
},
"starName":{
"type":"keyword"
},
"business":{
"type":"keyword",
"copy_to": "all"
},
"location":{
"type": "geo_point"
},
"pic":{
"type":"keyword",
"index": false
},
"all":{
"type":"text",
"analyzer": "ik_max_word"
}
}
}
}
GET /hotel/_doc/61083
# 查询所有
Get /hotel/_search
{
"query":{
"match_all": {
}
}
}
# match查询
GET /hotel/_search
{
"query": {
"match": {
"all": "外滩"
}
}
}
# multi_match查询
GET /hotel/_search
{
"query": {
"multi_match": {
"query": "如家外滩",
"fields": ["brand","name","business"]
}
}
}
# term查询
GET /hotel/_search
{
"query": {
"term": {
"city": {
"value": "杭州"
}
}
}
}
# range查询
GET /hotel/_search
{
"query": {
"range": {
"price": {
"gte": 100,
"lte": 300
}
}
}
}
# distance查询
GET /hotel/_search
{
"query": {
"geo_distance":{
"distance":"15km",
"location":"31.21,121.5"
}
}
}
# function score查询
GET /hotel/_search
{
"query": {
"function_score": {
"query": {
"match": {
"all": "外滩"
}
},
"functions": [
{
"filter": {
"term": {
"brand": "如家"
}
},
"weight": 10
}
],
"boost_mode": "sum"
}
}
}
# bool 查询
GET /hotel/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "如家"
}
}
],
"must_not": [
{
"range": {
"price": {
"gt": 400
}
}
}
],
"filter": [
{
"geo_distance": {
"distance": "10km",
"location": {
"lat": 31.21,
"lon": 121.5
}
}
}
]
}
}
}
# sort排序
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"FIELD": {
"order": "desc"
}
}
]
}
# sort排序
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"score": {
"order": "desc"
},
{
"price":"asc"
}
}
]
}
# 找到121.612282,31.034661周围的酒店,距离升序排序
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 31.034661,
"lon": 121.612282
},
"order": "asc",
"unit":"km"
}
}
]
}
# 分页查询
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "asc"
}
}
],
"from": 20,
"size": 10
}
# 高亮查询,默认情况下,ES搜索字段必须与高亮字段保持一致
GET /hotel/_search
{
"query": {
"match": {
"all": "如家"
}
},
"highlight": {
"fields": {
"name": {
"require_field_match": "false"
}
}
}
}