ElasticSearch-返回字段的标记

8tntrjer  于 2022-09-20  发布在  ElasticSearch
关注(0)|答案(3)|浏览(150)

如何在结果中返回特定字段的标记

例如,GET请求

curl -XGET 'http://localhost:9200/twitter/tweet/1'

退货

{
    "_index" : "twitter",
    "_type" : "tweet",
    "_id" : "1", 
    "_source" : {
        "user" : "kimchy",
        "postDate" : "2009-11-15T14:12:12",
        "message" : "trying out Elastic Search"
    } 
}

我希望在结果中包含‘_Soure.Message’字段的标记

bxfogqkk

bxfogqkk1#

还有另一种方法可以使用以下脚本_field脚本完成此操作:

curl -H 'Content-Type: application/json' -XPOST 'http://localhost:9200/test-idx/_search?pretty=true' -d '{
    "query" : {
        "match_all" : { }
    },
    "script_fields": {
        "terms" : {
            "script": "doc[field].values",
            "params": {
                "field": "message"
            }
        }

    }
}'

重要的是要注意,虽然该脚本返回已索引的实际术语,但它也缓存所有字段值,并且在大型索引上可能会使用大量内存。因此,对于大型索引,从存储的字段或源检索字段值并使用以下MVEL脚本动态重新解析它们可能更有用:

import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import java.io.StringReader;

// Cache analyzer for further use
cachedAnalyzer=(isdef cachedAnalyzer)?cachedAnalyzer:doc.mapperService().documentMapper(doc._type.value).mappers().indexAnalyzer();

terms=[];
// Get value from Fields Lookup
//val=_fields[field].values;

// Get value from Source Lookup
val=_source[field];

if(val != null) {
  tokenStream=cachedAnalyzer.tokenStream(field, new StringReader(val)); 
  CharTermAttribute termAttribute = tokenStream.addAttribute(CharTermAttribute); 
  while(tokenStream.incrementToken()) { 
    terms.add(termAttribute.toString())
  }; 
  tokenStream.close(); 
} 
terms

此MVEL脚本可以存储为config/scripts/analyze.mvel,并与以下查询一起使用:

curl 'http://localhost:9200/test-idx/_search?pretty=true' -d '{
    "query" : {
        "match_all" : { }
    },
    "script_fields": {
        "terms" : {
            "script": "analyze",
            "params": {
                "field": "message"
            }
        }

    }
}'
rn0zuynd

rn0zuynd2#

如果您指的是已编制索引的令牌,则可以在消息字段中创建terms facet。增加size值以获取更多条目,或设置为0以获取所有术语。

Lucene提供了存储术语向量的能力,但到目前为止(据我所知)还无法通过ElasticSearch访问它。

你为什么需要这个?如果您只想检查您正在索引的内容,您可以查看analyze api

qmelpv7a

qmelpv7a3#

如今,使用Term vectors API是可能的:

curl http://localhost:9200/twitter/_termvectors/1?fields=message

结果:

{
  "_index": "twitter",
  "_id": "1",
  "_version": 1,
  "found": true,
  "took": 0,
  "term_vectors": {
    "message": {
      "field_statistics": {
        "sum_doc_freq": 4,
        "doc_count": 1,
        "sum_ttf": 4
      },
      "terms": {
        "elastic": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 2,
              "start_offset": 11,
              "end_offset": 18
            }
          ]
        },
        "out": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 1,
              "start_offset": 7,
              "end_offset": 10
            }
          ]
        },
        "search": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 3,
              "start_offset": 19,
              "end_offset": 25
            }
          ]
        },
        "trying": {
          "term_freq": 1,
          "tokens": [
            {
              "position": 0,
              "start_offset": 0,
              "end_offset": 6
            }
          ]
        }
      }
    }
  }
}

注意:Map类型(此处:tweet)已在Elasticsearch 8.x中删除(请参阅迁移指南)。

相关问题