代码之家  ›  专栏  ›  技术社区  ›  NeverBe

使用动态索引名的Elasticsearch映射

  •  0
  • NeverBe  · 技术社区  · 4 年前

    我有一个简单的存储库来保存我的状态日志

    class OrderStatusRepository
      include Elasticsearch::Persistence::Repository
      include Elasticsearch::Persistence::Repository::DSL
    
      def index_name
        "statuses-#{ Time.now.strftime('%Y-%m')}"
      end
    
      mapping do
        indexes :src_location, type: 'geo_point'
        indexes :dst_location, type: 'geo_point'
      end
    end
    

    问题是当我添加一些数据时,映射没有被应用。

     {"id":158,"src_location":"1.486912, 2.493157","dst_location":"11.489026, -22.501309"}
    
    "dst_location": {
      "type": "text", #NOT GEOPOINT !!!!
      "fields": {
        "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
    

    我可以手动创建索引和映射,但它有动态名称,我不会每月/每天都这样做。

    有什么方法可以自动化吗?谢谢。

    1 回复  |  直到 4 年前
        1
  •  1
  •   Bhavya    4 年前

    你可以用 index templates 这将在索引创建时自动应用。这个 模板可以包括设置和映射。

    对于您的数据,您可以创建这样一个索引模板,它将匹配任何匹配的索引 foo-* bar-*

    PUT/ _template/foobar
    
    {
      "index_patterns": [
        "foo-*",
        "bar-*"
      ],
      "mappings": {
        "properties": {
          "id": {
            "type": "keyword"
          },
          "src_location": {
            "type": "geo_point"
          },
          "dst_location": {
            "type": "geo_point"
          }
        }
      }
    }
    

    POST/ foo-1/_doc/1
    
    {
      "id": 158,
      "src_location": "1.486912, 2.493157",
      "dst_location": "11.489026, -22.501309"
    }
    

    检索索引的映射时。你会得到这个的

    GET /foo-1/_mapping
    
    {
      "foo-1": {
        "mappings": {
          "properties": {
            "dst_location": {
              "type": "geo_point"
            },
            "id": {
              "type": "keyword"
            },
            "src_location": {
              "type": "geo_point"
            }
          }
        }
      }
    }