Elasticsearch 查询没有按预期工作

Elasticsearch query no working as expected

给定以下 Mysql SQL 查询:

SELECT * FROM `best_fares_GB` a 
WHERE is_dp=0 AND (origin_region = destination_region) AND
      (origin_country ='GB') AND (destination_city='MIL') AND 
      date_back IS NOT NULL, AND DATEDIFF(date_back,date_out)>=1 AND   
      DATEDIFF(date_back,date_out)<=7 AND DATEDIFF(date_out,CURDATE)<=90

索引中的当前映射如下:

{
    "bestfares_data": {
        "properties": {
            "dateBack": {
                "type": "date",
                "format": "basic_date",
              },
            "dateOut": {
                "type": "date",
                "format": "basic_date",
              },
            "destinationAirport": {
                "type": "string",
                "index": "not_analyzed",
              },
            "destinationCity": {
                "type": "string",
                "index": "not_analyzed",
              },
            "destinationCountry": {
                "type": "string",
                "index": "not_analyzed",
              },
            "destinationRegion": {
                "type": "string",
                "index": "not_analyzed",
              },
            "isDpSubagent": {
                "type": "boolean",
              },
            "originAirport": {
                "type": "string",
                "index": "not_analyzed",
              },
            "originCity": {
                "type": "string",
                "index": "not_analyzed",
              },
            "originCountry": {
                "type": "string",
                "index": "not_analyzed",
             },
            "originRegion": {
                "type": "string",
                "index": "not_analyzed",
              }
        }
    }
}

我最初的 elasticsearch 查询看起来像:

{
     "query": {
        "bool" : {
            "must" : [ 
                { "match": { "originCountry" : "GB" }},
                { "match": { "destinationCity" : "MIL" }}
            ]
         }
    },
    "filter" : {
        "and": {
           "filters": [
              {
                  "exists":  {"field": "dateBack"}
              } ,
              {
                  "script" : {"script" : "doc[\"originRegion\"].value == doc[\"destinationRegion\"].value"}
              },
              {
                  "script" : {"script" : "doc[\"dateBack\"].value - doc[\"dateOut\"].value >= 1"}
              },
              {
                  "script" : {"script" : "doc[\"dateBack\"].value - doc[\"dateOut\"].value <= 7"}
              },
              {
                 "range": {
                    "dateOut": {
                       "gte": "now+1d",
                       "lte": "now+3M"
                    }
                 }
              }
           ]
        } 
    }
}

这没有按预期工作。

如果我删除了该部分:

{
    "script" : {
         "script" : "doc[\"dateBack\"].value - doc[\"dateOut\"].value <= 7"
    }
}

我可以得到有效的结果,但是一旦我添加了这个条件,我就得到了零命中,即使我知道有很多记录满足这个条件。有人可以指出之前的查询有什么问题吗?

可能是一个远景,但您希望通过减去日期返回什么。您检查小于 7。但是在您的情况下 7 是多少?你是说天数吗?因为我认为这不会发生。恐怕你回来几毫秒。所以你正在尝试的是取回差异在 1 到 7 毫秒之间的文档。由于您仅索引日期,因此所有文档的时间为 0:00:00.

希望对您有所帮助