在 elasticsearch php 客户端中使用 query_string 和范围 [query_string] 格式错误的查询,预计 [END_OBJECT] 但发现 [FIELD_NAME]

use query_string and range in elasticsearch php client [query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]

我使用下面的代码并得到以下错误 为了连接 php 到 elasticsearch 我使用 elasticsearch php 客户端

    $params = [
    'index'=>'index1,index2',
    'from'=>$startfrom,
    'size'=>$maxperpage,
    'sort'  => array('id:desc'),
    'body'  => [
        'query' => [
            'query_string' => [
                'query' => "\"$search_text\""
            ]
        ]
    ],
];

if($limit_time['start']){
    $params['body']['query']['range']['date_insert']['gte']=$limit_time['start'];
}
if($limit_time['end']){
    $params['body']['query']['range']['date_insert']['lte']=$limit_time['end'];
}

错误:

{"error":{"root_cause":[{"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":104}],"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":104},"status":400}

你应该试试这样的方法。 query_stringrange 查询需要组合成一个 bool/filter 查询:

$params = [
    'index'=>'index1,index2',
    'from'=>$startfrom,
    'size'=>$maxperpage,
    'sort'  => array('id:desc'),
    'body'  => [
        'query' => [
            'bool' => [
                'filter' => [
                    [
                        'query_string' => [
                            'query' => "\"$search_text\""
                        ]
                    ]
                ]
            ]
        ]
    ],
];

$range = ['range' => ['date_insert' => []]]
if($limit_time['start']){
    $range['range']['date_insert']['gte'] = $limit_time['start'];
}
if($limit_time['end']){
    $range['range']['date_insert']['lte'] = $limit_time['end'];
}

$params['body']['query']['bool']['filter'][] = $range;

我解决了问题 但非常感谢“Val”的帮助

我使用下面的代码:

    $params = [
    'index'=>'index1,index2',
    'from'=>$startfrom,
    'size'=>$maxperpage,
    'sort'  => array('id:desc')
];

if($search_text){
    $params['body']['query']['bool']['must'][0]['match_phrase']['src']=$search_text;
}

if($use_time_status){
    if($limit_time['start']){
        $params['body']['query']['bool']['must'][1]['range']['date_insert']['gte']=$limit_time['start'];
    }
    if($limit_time['end']){
        $params['body']['query']['bool']['must'][1]['range']['date_insert']['lte']=$limit_time['end'];
    }
}