【问题标题】:Is it possible to get contents of copy_to field in Elasticsearch?是否可以在 Elasticsearch 中获取 copy_to 字段的内容?
【发布时间】: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 字段并将所有文本复制到其中?

【问题讨论】:

    标签: elasticsearch querying


    【解决方案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"
              ]
            }
          }
        ]
      }
    }
    

    参考文献

    【讨论】:

      【解决方案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"
                     ]
                  }
               }
            ]
         }
      }
           
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多