面试题答案
一键面试1. 设置映射数据类型
在创建索引时,合理设置字段的数据类型。示例如下:
PUT product_index
{
"mappings": {
"properties": {
"product_description": {
"type": "text"
},
"price": {
"type": "number"
}
}
}
}
2. 编写查询语句
使用bool
查询来组合关键词搜索和价格过滤,使用sort
来按照价格排序。示例如下:
GET product_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"product_description": "关键词"
}
}
],
"filter": [
{
"range": {
"price": {
"gte": 0, // 价格下限
"lte": 100 // 价格上限
}
}
}
]
}
},
"sort": [
{
"price": {
"order": "asc" // 升序排序,可改为 "desc" 降序
}
}
]
}