【问题标题】:Filter elasticsearch by range of date on a string property在字符串属性上按日期范围过滤 elasticsearch
【发布时间】:2017-09-23 22:35:36
【问题描述】:

我需要查询 elasticsearch 并将结果过滤到某个日期范围内。

问题是日期属性被映射为一个字符串。 有可能吗?

这是我正在使用的搜索查询:

{
  "size": 1,
  "from": 0,

  "query": { 
    "bool": { 
      "must": [
        { "match": { "status":   "active"        }}, 
        { "match": { "last_action_state": "accepted" }}
      ],
      "filter": [ 
        {"missing" : { "field" : "store_id" }},
        { "range": { "list_time": { "gte": "2017/01/01 00:00:00", "lte": "2017/03/01 23:59:59", "format": "yyyy/MM/dd HH:mm:ss"}}}
      ]
    }
  }
}

问题是我无法控制映射,因为它是由另一个索引文档的程序自动创建的,并且一旦创建,我就无法更改映射。

ps:elasticsearch 版本:2.3

更新:

索引信息:

{
  "avindex_v3": {
    "aliases": {
      "avindex": {}
    },
    "mappings": {
      "ads": {
        "properties": {
          "account_id": {
            "type": "long"
          },
          "ad_id": {
            "type": "long"
          },
          "ad_params": {
            "type": "string"
          },
          "body": {
            "type": "string"
          },
          "category": {
            "type": "long"
          },
          "city": {
            "type": "long"
          },
          "company_ad": {
            "type": "boolean"
          },
          "email": {
            "type": "string"
          },
          "images": {
            "type": "string"
          },
          "lang": {
            "type": "string"
          },
          "last_action_state": {
            "type": "string"
          },
          "list_date": {
            "type": "long"
          },
          "list_id": {
            "type": "long"
          },
          "list_time": {
            "type": "string"
          },
          "modified_at": {
            "type": "string"
          },
          "modified_ts": {
            "type": "double"
          },
          "name": {
            "type": "string"
          },
          "orig_date": {
            "type": "long"
          },
          "orig_list_time": {
            "type": "string"
          },
          "phone": {
            "type": "string"
          },
          "phone_hidden": {
            "type": "boolean"
          },
          "price": {
            "type": "long"
          },
          "region": {
            "type": "long"
          },
          "status": {
            "type": "string"
          },
          "store_id": {
            "type": "long"
          },
          "subject": {
            "type": "string"
          },
          "type": {
            "type": "string"
          },
          "user_id": {
            "type": "long"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1493216710928",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "WEHGLF8iRyGk3Xgbmo7H8Q",
        "version": {
          "created": "2040499"
        }
      }
    },
    "warmers": {}
  }
}

【问题讨论】:

  • 您需要找到一种方法来修改该映射并将字段更改为date 而不是string,否则字符串日期的范围查询将无法正常工作。一种方法是定义一个包含正确字段映射的index template,以便使用正确的字段类型创建索引,而您无需依赖其他程序。
  • Thanx @Val ,我不知道索引模板,如果您可以将您的评论作为回复发布,我可以接受它作为正确回复。
  • 你能提供更多关于你的索引是如何命名的信息吗?还有你当前的映射?只需运行 curl -XGET localhost:9200/index_name 并使用结果更新您的问题
  • 我在帖子@Val中添加了索引信息
  • 我也创建了模板,但是重新索引时,模板不适用

标签: elasticsearch elasticsearch-2.0


【解决方案1】:

您可以尝试将其作为关键字:

{
    "range": {
        "list_time.keyword": {
            "gte": "2020-08-12 22:24:55.56",
            "lte": "2020-08-12 22:24:56.56"
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多