如何从字段中获取特定大小的结果?

时间:2015-11-16 02:45:13

标签: elasticsearch

我知道我们可以控制突出显示的片段的大小。 但我想控制特定字段结果的大小。

离) 我可以使用fragment_sizenumber_of_fragments来突出显示大小。

highlight: {
    "CONTEXT": [
        "I am leaving today.I want to be a part of it New York, New York These <b>vagabond</b> shoes They are"
    ]
}

但我希望得到这样的结果;

fields: {
    "CONTEXT": [
        "I am leaving today.I want to be a part of it New York, New York These vagabond shoes They are longing to stray Right through the very heart of it New York, New York ..."
    ],
    "TITLE": [
         "new york new york"
    ]
},

因为,有时此上下文字段太长而无法显示结果网页。 所以,我想控制大小的CONTEXT字段结果。

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作,但我建议您是否可以在客户端执行此操作。

{
  "settings": {
    "analysis": {
      "filter": {
        "truncate_filter": {
          "type": "truncate",
          "length": 20
        }
      },
      "analyzer": {
        "my_truncate_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "truncate_filter"
          ]
        }
      }
    }
  }, 
  "mappings": {
    "test": {
      "properties": {
        "context": {
          "type": "string",
          "fields": {
            "truncated": {
              "type": "string",
              "analyzer": "my_truncate_analyzer"
            }
          }
        }
      }
    }
  }
}

在您的查询中,您需要添加fielddata_fields才能看到截断字段:

{
  "query": {
    ...
  }, 
  "fielddata_fields": ["context.truncated"]
}