【问题标题】:Issues mapping a document with ElasticSearch使用 ElasticSearch 映射文档的问题
【发布时间】:2014-02-18 20:30:30
【问题描述】:

我有一个文档,我希望将其存储在 ElasticSearch 中并能够对其运行查询,但我认为文档结构可能格式不正确,因此我无法进行有效的查询。

该文档试图是通用的,因此具有一组重复结构。

例如:

  description : [
    { type : "port", value : 1234 }.
    { type : "ipaddress", value : "192.168.0.1" },
    { type : "path", value : "/app/index.jsp app/hello.jsp" },
    { type : "upsince", value : "2014-01-01 12:00:00" },
    { type : "location", value : "-40, 70" }
  ]

注意:我已经简化了示例,因为在真实文档中,重复结构大约有 7 个字段,其中 3 个字段将明确标识“类型”。

从上面的示例中,我看不出如何编写映射,因为“值”可能是:

  • 整数
  • IP 地址
  • 只需要用空格标记的字段
  • 日期时间
  • 一个 GEO 点

我能看到的唯一解决方案是需要将文档转换为另一种更容易与 ElasticSearch 映射的格式?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    这里有些描述这个案例:http://www.found.no/foundation/beginner-troubleshooting/#keyvalue-woes

    在同一个字段中不能有不同类型的值。您可以做的是拥有不同的字段,例如location_valuetimestamp_value 等。

    这是一个可运行的示例:https://www.found.no/play/gist/ad90fb9e5210d4aba0ee

    #!/bin/bash
    
    export ELASTICSEARCH_ENDPOINT="http://localhost:9200"
    
    # Create indexes
    
    curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{
        "mappings": {
            "type": {
                "properties": {
                    "description": {
                        "type": "nested",
                        "properties": {
                            "integer_value": {
                                "type": "integer"
                            },
                            "type": {
                                "type": "string",
                                "index": "not_analyzed"
                            },
                            "timestamp_value": {
                                "type": "date"
                            }
                        }
                    }
                }
            }
        }
    }'
    
    # Index documents
    curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
    {"index":{"_index":"play","_type":"type"}}
    {"description":[{"type":"port","integer_value":1234},{"type":"upsince","timestamp_value":"2014-01-01T12:00:00"}]}
    '
    

    【讨论】:

    • 抱歉没有早点回来。您提供的链接非常好。我相应地重新排列了我的键值数据
    【解决方案2】:

    如果你先像这样转换文档,你会省去很多麻烦

    {
      "port": 1234,
      "ipaddress" : "192.168.0.1" ,
      "path" : "/app/index.jsp app/hello.jsp",
      "upsince" : "2014-01-01 12:00:00",
      "location" : "-40, 70" 
    }
    

    Elasticsearch 被设计为在字段和值方面具有灵活性,因此它已经可以处理您扔给它的几乎任何键/值组合。

    您可以选择将原始文档包含在明确存储但未编制索引的字段中,以防您需要查询中返回的原始文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-14
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 2021-05-16
      • 1970-01-01
      相关资源
      最近更新 更多