【问题标题】:Search by Date range in Elastic search malformed query在弹性搜索格式错误的查询中按日期范围搜索
【发布时间】:2017-11-27 17:26:06
【问题描述】:

我正在尝试将按日期范围搜索与 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]","line":1,"col":78},"status":400}

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

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

【问题讨论】:

    标签: php elasticsearch logstash


    【解决方案1】:

    这是原生的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 ("2016-05-30T09:39:40.000Z") 中以与 datecreatedsort 相同的格式传递日期范围

    【讨论】:

    • hi for only range 这个查询是正确的,但我也必须使用通配符字符串。你能建议怎么放吗
    • @VipinS,你能给我举个例子,你到底需要什么?
    【解决方案2】:

    试试这个:

    {
        "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  
      )
    );
    

    【讨论】:

    • 它显示 {"root_cause":[{"type":"parsing_exception","re​​ason":"对于 START_OBJECT 中的未知键
    • 你能帮忙解释一下为什么会出现这个问题
    【解决方案3】:

    您好,解决方案正常

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

    谢谢大家的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多