首页 文章

Elasticsearch:如何使对象类型的所有属性都不被分析?

提问于
浏览
4

我需要使用Object字段创建一个Elasticsearch映射,该字段的密钥事先是未知的 . 此外,值可以是整数或字符串 . 但我希望将值存储为非分析字段(如果它们是字符串) . 我尝试了以下映射:

PUT /my_index/_mapping/test
{
  "properties": {
    "alert_text": {
      "type": "object",
      "index": "not_analyzed"
    }
  }
}

现在索引创建得很好 . 但是,如果我插入这样的值:

POST /my_index/test
{
  "alert_text": {
    "1": "hello moto"
  }
}

使用标准分析仪将值“hello moto”存储为分析字段 . 我希望它存储为非分析字段 . 如果我事先不知道所有钥匙都可以出现,是否可能?

1 回答

  • 5

    试试dynamic templates . 使用此功能,您可以为动态创建的字段配置一组规则 .

    在这个例子中,我已经配置了我认为你需要的规则,即 alert_text 中的所有字符串字段都是 not_analyzed

    PUT /my_index
    {
        "mappings": {
            "test": {
                "properties": {
                    "alert_text": {
                      "type": "object"   
                    }
                },
                "dynamic_templates": [
                    {
                        "alert_text_strings": {
                            "path_match": "alert_text.*", 
                            "match_mapping_type": "string",
                            "mapping": {
                                "type": "string",
                                "index": "not_analyzed"
                            }
                        }
                    }
                ]
            }
        }
    }
    
    POST /my_index/test
    {
      "alert_text": {
        "1": "hello moto"
      }
    }
    

    执行上述请求后,您可以执行此查询以显示当前映射:

    GET /my_index/_mapping
    

    你将获得:

    {
       "my_index": {
          "mappings": {
             "test": {
                "dynamic_templates": [
                   {
                      "alert_text_strings": {
                         "mapping": {
                            "index": "not_analyzed",
                            "type": "string"
                         },
                         "match_mapping_type": "string",
                         "path_match": "alert_text.*"
                      }
                   }
                ],
                "properties": {
                   "alert_text": {
                      "properties": {
                         "1": {
                            "type": "string",
                            "index": "not_analyzed"
                         }
                      }
                   }
                }
             }
          }
       }
    }
    

    在哪里可以看到 alert_text.1 存储为 not_analyzed .

相关问题