在elasticsearch php client[query\u string]中使用query\u string和range错误的查询,应为[end\u object],但找到[field\u name]

yftpprvb  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(2)|浏览(626)

我使用下面的代码和下面的错误连接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}
s71maibg

s71maibg1#

你应该试试这样的。这个 query_string 以及 range 查询需要合并到一个 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;
fnatzsnv

fnatzsnv2#

我解决了这个问题,但非常感谢“瓦尔”的帮助
我使用以下代码:

$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'];
    }
}

相关问题