elasticsearch - 限制弹性搜索响应中的列文本长度
问题描述
以下是我的弹性搜索映射
{
"metadata" : {
"mappings" : {
"properties": {
"id": {"type":"keyword"},
"title": {"type":"keyword"},
"created": {"type":"date"},
"modified": {"type":"date"},
"keyword": {"type":"keyword"},
"description": {"type":"text"}
}
}
}
}
这是我的 ES 查询,用于根据用户对多个字段的查询检索数据,限制为前 5 个点击,按修改日期排序并仅显示标题和描述字段。该查询按我的预期完美运行。
GET /_search
{
"query": {
"multi_match" : {
"query" : "UK House Price Index",
"fields" : ["title", "keyword", "description"]
}
},
"size": 5,
"_source": ["title", "description"],
"sort": {"modified": "asc"}
}
我想将我的描述字段响应限制为最多 100 个字符。我是 ES 的新手。有人可以建议我如何有效地做到这一点吗?
解决方案
您可以使用highlighting
来获取匹配的相关部分description
。它的fragment_size
设置本质上是你所追求的字符串连接,有趣的是,默认为 100。
POST long_desc/_doc
{"title":"UK House Price Index","keyword":"UK House Price Index","description":"APM automatically collects in-depth performance metrics and errors from inside your applications. Ingest logs UK House Price Index from popular data sources and easily visualize in preconfigured dashboards."}
GET long_desc/_search
{
"query": {
"multi_match": {
"query": "UK House Price Index",
"fields": [
"title",
"keyword",
"description"
]
}
},
"size": 5,
"_source": [
"title"
],
"sort": {
"modified": "asc"
},
"highlight": {
"pre_tags": "",
"post_tags": "",
"fragment_size": 1,
"fields": {
"description": {
"fragment_size": 100,
"number_of_fragments": 1,
"fragmenter": "span"
}
}
}
}
屈服
[
{
"_index":"long_desc",
"_type":"_doc",
"_id":"zSWAwnEBPAbzy1R_Uk-I",
"_score":null,
"_source":{
"title":"UK House Price Index"
},
"highlight":{
"description":[
"Ingest logs UK House Price Index from popular data sources and easily visualize in preconfigured dashboards"
]
}
...
}
]
仅供参考pre_
并post_tags
默认为,<em>
因此您的前端实际上不需要进行任何后期处理来突出显示匹配项。我选择了空字符串来保持description
清洁。
推荐阅读
- android - 如何修改android源项目(AOSP)并禁用私有库检查?
- php - MySQL Join - 连接两个表并将结果显示为第三个表的列
- php - 如何创建一个事件日历 Outlook PHP
- c++ - 为 GCC 4 兼容性配置 GCC
- python - Python setup.py 定义具有可选依赖项的可选子包
- php - 我可以在 PHP 文件上传中获取错误名称吗?
- angularjs - 带有对象的 ng-options 不呈现键和值
- python - Python 请求 HTTP 子代码响应
- timestamp - 如何使用 Spring Data Cassandra 从 Cassandra 时间戳类型列中选择 java.util.Date 值?
- selenium - 如何在 selenium 中使用 RestAssured 发布以下请求