FanKingWang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

// 自动补全查询
GET /test3/_search
{
"suggest": {
"title_suggest": {
"text": "s",
"completion": {
"field": "title",
"skip_duplicates": true,
"size": 10
}
}
}
}

// 示例数据
POST test3/_doc
{
"title": ["Sony", "WH-1000XM3"]
}
POST test3/_doc
{
"title": ["SK-II", "PITERA"]
}
POST test3/_doc
{
"title": ["Nintendo", "switch"]
}


#自动补全 创建索引库
PUT test3
{
"mappings": {
"properties": {
"title":{
"type": "completion"
}
}
}
}
#自定义分词器
PUT /test1
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "ik_max_word",
"filter": "py"
}
},
"filter": {
"py": {
"type": "pinyin",
"keep_full_pinyin": false,
"keep_joined_full_pinyin": true,
"keep_original": true,
"limit_first_letter_length": 16,
"remove_duplicated_term": true,
"none_chinese_pinyin_tokenize": false
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "my_analyzer",
"search_analyzer": "ik_smart"
}
}
}
}


#拼音分词器测试
POST /_analyze
{
"text": "如家酒店还不错",
"analyzer": "pinyin"
}


# Metric聚合语法 我们对酒店按照品牌分组,形成了一个个桶。现在我们需要对桶内的酒店做运算,获取每个品牌的用户评分的min、max、avg等值。
GET /hotel/_search
{
"size": 0,
"aggs": {
"brandAgg": {
"terms": {
"field": "brand",
"size": 20
},
"aggs": {
"scoreAgg": {
"stats": {
"field": "score"
}
}
}
}
}
}

#限定聚合范围
GET /hotel/_search
{
"query": {
"range": {
"price": {
"gte": 10
}
}
},
"size": 0,
"aggs": {
"brandAgg" :{
"terms": {
"field": "brand",
"size": 20
}
}
}
}

 


#单字段查询
GET /hotel/_search
{
"query": {
"match": {
"all": "上海"
}
}
}

#查询全部
GET /hotel/_search
{
"query": {
"match_all": {}
}
}

#精准查询
GET /hotel/_search
{
"query": {
"term": {
"city": {
"value": "上海"
}
}
}
}

#多字段查询
GET /hotel/_search
{
"query": {
"multi_match": {
"query": "上海",
"fields": ["name","brand", "city"]
}
}
}


#范围查询---价格
GET /hotel/_search
{
"query": {
"range": {
"price": {
"gte": 1000,
"lte": 2000
}
}
}
}


#矩形范围查询
# geo_bounding_box查询
GET /hotel/_search
{
"query": {
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 31.1,
"lon": 121.5
},
"bottom_right": {
"lat": 30.9,
"lon": 121.7
}
}
}
}
}


# geo_distance 查询
GET /hotel/_search
{
"query": {
"geo_distance": {
"distance": "15km",
"location": "31.21,121.5"
}
}
}

#算分函数
GET /hotel/_search
{
"query": {
"function_score": {
"query": {
"match": {
"all": "外滩"
}
},
"functions": [
{
"filter": {
"term": {
"brand": "如家"
}
},
"weight": 10
}
],
"boost_mode": "multiply"
}
}
}

#布尔查询
GET /hotel/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"city": {
"value": "上海"
}
}
}
],
"should": [
{
"term": {
"brand": {
"value": "皇冠假日"
}
}
},
{
"term": {
"brand": {
"value": "华美达"
}
}
}
],
"must_not": [
{
"range": {
"price": {
"lte": 500
}
}
}
],
"filter": [
{
"range": {
"score": {
"gte": 45
}
}
}
]

}
}
}


#经纬度查询
GET /hotel/_search
{
"query": {
"bool": {
"must": [
{"term": {"city": "上海" }}
],
"should": [
{"term": {"brand": "皇冠假日" }},
{"term": {"brand": "华美达" }}
],
"must_not": [
{ "range": { "price": { "lte": 500 } }}
],
"filter": [
{ "range": {"score": { "gte": 45 } }}
]
}
}
}

#酒店数据按照用户评价(score)降序排序,
#评价相同的按照价格(price)升序排序
#根据坐标进行升序
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"score": {
"order": "desc"
}
},
{
"price": {
"order": "asc"
}
},
{
"_geo_distance": {
"location": {
"lat": 31.034661,
"lon": 121.612282
},
"order": "asc",
"unit": "km"
}
}
],
"from": 20,
"size": 10

}


#基本分页
GET /hotel/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 20,
"sort": [
{
"price": {
"order": "desc"
}
}
]
}

#深度分页
GET /hotel/_search
{
"query": {
"match_all": {}
},
"from": 990,
"size": 10,
"sort": [
{
"price": {
"order": "asc"
}
}
]
}

#搜索名称中包含如家的酒店,且对名称进行高亮
GET /hotel/_search
{
"query": {
"match": {
"name": "如家"
}
},
"highlight": {
"fields": {
"name": {
"pre_tags": "<em>",
"post_tags": "</em>"
}
}
}
}

#搜索all中有如家的数据
GET /hotel/_search
{
"query": {
"match": {
"all": "如家"
}
},
"highlight": {
"fields": {
"name": {
"require_field_match": "false"
}
}
}
}


GET /hotel

 

GET _search
{
"query": {
"match_all": {}
}
}

#查询索引库
GET /heima

#修改索引库
PUT /heima/_mapping
{
"properties":{
"age":{
"type":"integer"
}
}
}

#新增文档
POST /heima/_doc/1
{
"info": "黑马练习生",
"email": "18105899096@163.com",
"name":{
"fristName": "文范",
"lastName": "王"
}
}

#查询文档
GET /heima/_doc/1

 

DELETE /hotel


GET /hotel/_doc/36934

 

# 商品数据索引库
PUT /item
{
"settings": {
"analysis": {
"analyzer": {
"text_anlyzer": {
"tokenizer": "ik_max_word",
"filter": "py"
},
"completion_analyzer": {
"tokenizer": "keyword",
"filter": "py"
}
},
"filter": {
"py": {
"type": "pinyin",
"keep_full_pinyin": false,
"keep_joined_full_pinyin": true,
"keep_original": true,
"limit_first_letter_length": 16,
"remove_duplicated_term": true,
"none_chinese_pinyin_tokenize": false
}
}
}
},
"mappings": {
"properties": {
"id":{
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "text_anlyzer",
"search_analyzer": "ik_smart",
"copy_to": "all"
},
"category":{
"type": "keyword",
"copy_to": "all"
},
"brand":{
"type": "keyword",
"copy_to": "all"
},
"price":{
"type": "long"
},
"sold":{
"type": "integer"
},
"image":{
"type": "keyword",
"index": false
},
"commentCount":{
"type": "integer",
"index": false
},
"isAD":{
"type": "boolean"
},
"all":{
"type": "text",
"analyzer": "text_anlyzer",
"search_analyzer": "ik_smart"
},
"suggestion":{
"type": "completion",
"analyzer": "completion_analyzer"
}
}
}
}

posted on 2022-08-06 15:31  FanKingWang  阅读(21)  评论(0)    收藏  举报