【问题标题】:Elasticsearch [match] unknown token [START_OBJECT] after [created_utc][created_utc] 之后的 Elasticsearch [match] 未知令牌 [START_OBJECT]
【发布时间】:2018-08-08 16:46:13
【问题描述】:

我正在学习如何使用来自 pushshift.io 的 2006 年 reddit cmets 数据集来使用 elasticsearch。

created_utc 是包含创建评论时间的字段。

我正在尝试获取特定时间范围内的所有帖子。我google了一下,发现我需要使用“范围”关键字。

这是我现在的查询:

{
    "query": {
        "match" : {
            "range": {
                "created_utc": {
                    "gte": "1/1/2006",
                    "lte": "31/1/2006",
                    "format": "dd/MM/yyyy"
                }
            }
        }
    }
}

然后我尝试使用 bool 查询,这样我就可以将时间范围与edited must not = False 匹配(edited 是告诉我帖子是否已被编辑的布尔字段):

{
    "query": {
        "bool" : {
            "must" : {
                "range" : {
                    "created_utc": {
                        "gte" : "01/12/2006", "lte": "31/12/2006", "format": "dd/MM/yyyy" 
                    }
                }
            },
            "must_not": {
                "edited": False
            }
        }
    }
}

但是,这给了我另一个我无法弄清楚的错误:

[已编辑] 查询格式错误,查询名称后没有 start_object

如果有人能帮我解决这个问题,我将不胜感激,谢谢!

如果有帮助,这是我的评论映射:

{
   "comment":{
      "properties":{
         "author":{
            "type":"text",
            "fields":{
               "keyword":{
                  "type":"keyword",
                  "ignore_above":256
               }
            }
         },
         "body":{
            "type":"text",
            "fields":{
               "keyword":{
                  "type":"keyword",
                  "ignore_above":256
               }
            }
         },
         "controversiality":{
            "type":"long"
         },
         "created_utc":{
            "type":"date"
         },
         "edited":{
            "type":"boolean"
         },
         "gilded":{
            "type":"long"
         },
         "id":{
            "type":"text",
            "fields":{
               "keyword":{
                  "type":"keyword",
                  "ignore_above":256
               }
            }
         },
         "link_id":{
            "type":"text",
            "fields":{
               "keyword":{
                  "type":"keyword",
                  "ignore_above":256
               }
            }
         },
         "parent_id":{
            "type":"text",
            "fields":{
               "keyword":{
                  "type":"keyword",
                  "ignore_above":256
               }
            }
         },
         "score":{
            "type":"long"
         },
         "subreddit":{
            "type":"text",
            "fields":{
               "keyword":{
                  "type":"keyword",
                  "ignore_above":256
               }
            }
         }
      }
   }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    如果您想获取某个时间范围内的所有帖子,那么您必须使用范围查询。您的查询的问题是您在弹性搜索中不允许的匹配查询中使用range,因此您的查询应如下所示:

    {
      "query": {
        "range": {
          "created_utc": {
              "gte": 1136074029,
              "lte": 1136076410
          }
        }
      }
    }
    

    如果created_utc字段保存为epoch,则必须使用epoch格式查询。

    第二个查询要在edited不能为假的范围内查找帖子:

    {
      "query": {
        "bool": {
          "must": [
              {
                "range": {
                  "created_utc": {
                    "gte": 1136074029,
                    "lte": 1136076410
                  }
                }
              }
          ],
          "must_not": [
            {
              "match": {
                "edited": false
              }
            }
          ]
        }
      }
    } 
    

    注意:如果您的 created_utcdd/MM/yyyy 格式存储,那么在查询时您应该使用 strict companion format,即而不是 2006 年 1 月 1 日,您应该给出 2006 年 1 月 1 日。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 2020-04-15
      • 1970-01-01
      相关资源
      最近更新 更多