Elasticsearch查询无法按预期工作

时间:2015-01-02 15:28:45

标签: elasticsearch

给出以下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

Index中的当前映射如下:

{
    "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",
              }
        }
    }
}

我的初始弹性搜索查询看起来像:

{
     "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"
    }
}

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

1 个答案:

答案 0 :(得分:0)

可能是一个长镜头,但你期望通过减去日期返回什么。检查小于7.但你的情况是7?你的意思是天?因为我不认为这种情况正在发生。我恐怕你会回来毫秒。所以你要做的就是找回差异在1到7毫秒之间的文档。由于您只是为日期编制索引,因此所有文档的时间均为0:00:00。

希望有所帮助