是否可以在 Elasticsearch 中获取 copy_to 字段的内容?

Posted

技术标签:

【中文标题】是否可以在 Elasticsearch 中获取 copy_to 字段的内容?【英文标题】:Is it possible to get contents of copy_to field in Elasticsearch? 【发布时间】:2016-08-05 17:43:28 【问题描述】:

我正在使用 Elasticsearch v2.3.0。假设我有一个带映射的索引:


"mappings": 
  "post": 
    "dynamic": false,
    "_source": 
      "enabled": true
    ,
    "properties": 
      "text": 
        "norms": 
          "enabled": false
        ,
        "fielddata": 
          "format": "disabled"
        ,
        "analyzer": "text_analyzer",
        "type": "string"
      ,
      "subfield": 
        "properties": 
          "subsubfield": 
            "properties": 
              "subtext": 
                "index": "no",
                "analyzer": "text_analyzer",
                "type": "string",
                "copy_to": "text"
              
              
          
        
      
    ,
    "_all": 
      "enabled": false
    
  

因此,由于copy_to,来自subfield.subsubfield.subtext 的文本被复制到文本field。如果您查询帖子,则text 字段中仅显示来自text 的数据,因为_source 未修改。如果要获取所有文本,则应聚合客户端上的所有字段。如果有很多子字段,这可能会很不方便。

是否有一个神奇的查询,允许获取 text 字段并将所有文本复制到其中?

【问题讨论】:

【参考方案1】:

老问题,新的简单测试。以下是您可以使用简单索引对其进行测试的方法。

请求

DELETE test-index
PUT test-index

  "mappings": 
    "properties": 
      "first_name": 
        "type": "text",
        "copy_to": "full_name" 
      ,
      "last_name": 
        "type": "text",
        "copy_to": "full_name"
      ,
      "full_name": 
        "type": "text",
        "store": true
      
    
  


PUT test-index/_doc/1

  "first_name": "John",
  "last_name": "Doe"


GET test-index/_search

  "query": 
    "match": 
      "full_name": 
        "query": "John Doe"
      
    
  ,
  "stored_fields": [
    "full_name"
  ]

回应


  "took" : 2,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 1,
      "relation" : "eq"
    ,
    "max_score" : 0.5753642,
    "hits" : [
      
        "_index" : "test-index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "fields" : 
          "full_name" : [
            "John",
            "Doe"
          ]
        
      
    ]
  

参考文献

copy_to: https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html 商店:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-store.html

【讨论】:

【参考方案2】:

text 字段集"store":"yes" 的映射中 您应该可以使用fields 获取它

示例

put test/test/_mapping

  "properties" : 
   "name" :  "type" : "string","copy_to": "text",
   "text" : "type" : "string" ,"store" :"yes",
    "subfield": 
        "properties": 
          "subsubfield": 
            "properties": 
              "subtext": 
                "index": "no",
                "type": "string",
                "copy_to": "text"
              
              
          
        
      
     
    


put test/test/1 

    "name" : "hello",
    "subfield" : 
        "subsubfield" : [
            "subtext" : "soundgarden" ,
            "subtext" : "alice in chains" ,
            "subtext" : "dio" 
        ]
    
 

post test/_search

    "stored_fields": [
       "text"
    ]

结果


   "took": 2,
   "timed_out": false,
   "_shards": 
      "total": 5,
      "successful": 5,
      "failed": 0
   ,
   "hits": 
      "total": 1,
      "max_score": 1,
      "hits": [
         
            "_index": "test",
            "_type": "test",
            "_id": "1",
            "_score": 1,
            "fields": 
               "text": [
                  "hello",
                  "soundgarden",
                  "alice in chains",
                  "dio"
               ]
            
         
      ]
   

     

【讨论】:

使用 elasticsearch 7+,"store" : true 弹性文档上存储参数elastic.co/guide/en/elasticsearch/reference/current/…

以上是关于是否可以在 Elasticsearch 中获取 copy_to 字段的内容?的主要内容,如果未能解决你的问题,请参考以下文章

是否可以在 ElasticSearch 中使用 presto 或 Hive (ElasticSearch-Hadoop) 的任何 ES 连接器进行 JOIN 操作?

如何从 ElasticSearch 中的形状中获取相交点

是否可以更新 ElasticSearch 6.x 索引中现有字段映射的“存储”值?

是否可以对 ElasticSearch 索引进行分区?

是否可以使用 Nest 将查询列表传递给 ElasticSearch?

对DynamoDB数据进行排序