首页 > 解决方案 > 限制弹性搜索响应中的列文本长度

问题描述

以下是我的弹性搜索映射

{
  "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 的新手。有人可以建议我如何有效地做到这一点吗?

标签: elasticsearchsearchsubstring

解决方案


您可以使用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清洁。


推荐阅读