在弹性搜索格式错误的查询中按日期范围搜索

时间:2017-11-27 17:26:07

标签: php elasticsearch logstash

我正在尝试将日期范围内的搜索与PHP和弹性搜索相结合

$params1 = [
'index' => 'joborders',
'type' => 'joborder',
'from' =>0,
'size' => 50,


     'body' => [

     'query' => [
            'query_string' => [
                'query' => $wildCardString,
                'fields' => ['description'],

                ]
        ]
    ]
];

$filter_date=array();
$filter_date['range']['datecreatedsort']['gte']='2015-11-27';
$filter_date['range']['datecreatedsort']['lte']='2017-11-27';
$params1['body']['query']['filtered']['filter']=$filter_date;
$params1['body']['sort']['datecreatedsort']['order'] = 'desc';

   try {
     $results = $client->search($params1);
     //print_r($results);
   }
   catch (Exception $e) {
$last = $client->transport->getLastConnection()->getLastRequestInfo();
$last['results']['error'] = [];
print_r($last);
}

当我在查询之上运行时,我收到以下错误

  

[query_string]格式错误的查询,预期[END_OBJECT]但找到了   [FIELD_NAME]”, “线”:1, “栏”:78}, “状态”:400}

     

datecreatedsort字段映射是日期类型是日期和值   弹性搜索db是“datecreatedsort”:“2016-05-30T09:39:40.000Z”

请帮助弹性查询中的问题。

3 个答案:

答案 0 :(得分:2)

它是原生elasticsearch请求。

PHP中,您需要创建具有相同结构的数组查询。

GET /joborders/_search?pretty=true
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "created_at": {
              "gte": "2017-11-22 13:49:00",
               "lte": "2017-11-22 23:50:00"
            }
          }
        }
      ]
    }
  }

PS您需要以与db {(#34; 2016-05-30T09:39:40.000Z")中的datecreatedsort相同的格式传递日期范围

答案 1 :(得分:1)

试试这个:

{
    "query": {
      "filtered": {
        "query": {
          "query_string": {
            "default_field": "description",
            "query": $wildCardString
          }

        },
        "filter": {
          "range": {
            "datecreatedsort": {
              "gte": '2015-11-27',
              "lte": '2017-11-27'
            }
          }
        }
      }
    }  
}

您的查询代码如下所示:

$query=array(
  'filtered'=>array(
      'query' => array(
            'query_string' => [
                'query' => $wildCardString,
                'fields' => ['description'],
                ]
       ),
       'filter'=>$filter_date  
  )
);

答案 2 :(得分:0)

嗨,解决方案工作正常

$params1 = [
'index' => 'joborders',
'type' => 'joborder',
'body' => [
    'query' => [
        'bool' => [
            'filter' => [
                'range' => [ 'date_modified' => ['gt'=>$duration,'lt'=>$today,'boost'=> '2.0'] ]
            ],
            'must' => [
                'match' => [ 'description' => $wildCardString ]
            ]
        ]
    ]
 ]
];

谢谢大家的帮助。