简体   繁体   English

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

[英]Search by Date range in Elastic search malformed query

I am trying to integrate search by date range with PHP and elastic search 我正在尝试将日期范围的搜索与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);
}

When I am running above query I am getting following error 当我在查询上方运行时,出现以下错误

[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":78},"status":400} [query_string]格式错误的查询,预期[END_OBJECT],但发现[FIELD_NAME]“,”行“:1,” col“:78},”状态“:400}

datecreatedsort filed mapping is date type is date and value in elastic search db is "datecreatedsort":"2016-05-30T09:39:40.000Z" datecreatedsort归档映射是日期类型是日期,弹性搜索数据库中的值是“ datecreatedsort”:“ 2016-05-30T09:39:40.000Z”

please help where is the issue in elastic query. 请帮助弹性查询的问题在哪里。

It's the native elasticsearch request. 这是本地的elasticsearch请求。

In PHP you need to create array query with the same structure. 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 You need to pass date ranges in the same format as datecreatedsort in db ("2016-05-30T09:39:40.000Z") 附注:您需要以与datecreatedsort相同的格式传递日期范围(在数据库中(“ 2016-05-30T09:39:40.000Z”))

try this: 尝试这个:

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

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

your code for the query will look something like this: 您查询的代码将如下所示:

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

Hi Got the solution its working fine 嗨,有解决方案,它的工作正常

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

Thanks guys for help. 谢谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM