dtsc1684 2015-08-27 09:59
浏览 23
已采纳

使两个查询相互作用

I am trying to find out the names between a time period.

But the problem is when i try to run it

first-- it returns the selected names value second -- it returns data between that period.

Now what i want to do is that, i want to search name between a time period.

For explaining it more, suppose we have select a name "john", and select a date 2/08/2015 to 27/08/2015, so it should return all the names between that time period.

But it is returning the names of john and than searching for other documents between that period, where is should also take the name "John" when searching the date !

How to solve this problem !

    $name = $request->request->get('name');

    $strat_date = $request->request->get('strat_date');

    $end_date = $request->request->get('end_date');


    $params = array(
        'index' => "myIndex",
        'type' => "myType",
        'body' => array(
            'query' => array(
                'bool' => array(
                    'must' => array(
                    // empty should clause for starters
                    )
                )
            )
        )
    );

    // add each constraint in turn depending on whether the param is specified
    if (!empty($sourceFilter)) {
        $params['body']['query']['bool']['must'] = array(
            'query_string' => array(
                'default_field' => 'name',
                'query' => implode(" ", $name)
            )
        );
    }

    if (!empty($end_date)) {
        $params['body']['query']['bool']['must']['range'] = array(
            'datehistory' => array(
                "from" => $strat_date,
                "to" => $end_date
            )
        );
    }

    // special case if none is present, just match everything
    if (count($params['body']['query']['bool']['must']) == 0) {
        $params['body']['query'] = array(
            'match_all' => array()
        );
    }

    $docs = $client->search($params);

But at always return the an error like this ----

{"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[agEfJ6ltSJmlec3gpnPg3g][myIndex][1]: SearchParseException[[myIndex][1]: from[-1],size[-1]: Parse Failure [Failed to parse name [{\"query\":{\"bool\":{\"must\":{\"query_string\":{\"default_field\":\"name\",\"query\":\"john.se\"},\"range\":{\"datehistory\":{\"from\":1438207200,\"to\":1440626400}}}}}}]]]; nested: QueryParsingException[[myIndex] No query registered for [datehistory]]; }]","status":400}

  • 写回答

1条回答 默认 最新

  • duanhao4909 2015-08-27 11:43
    关注

    You have a mix of bool/should and bool/must so you simply need to change bool/should to bool/must.

    $params = array(
        'index' => "myIndex",
        'type' => "myType",
        'body' => array(
            'query' => array(
                'bool' => array(
                    'must' => array(                       <--- CHANGE
                    // empty should clause for starters
                    )
                )
            )
        )
    );
    
    // add each constraint in turn depending on whether the param is specified
    if (!empty($sourceFilter)) {
        $params['body']['query']['bool']['must'][] = array(  <--- CHANGE
            'query_string' => array(
                'default_field' => 'name',
                'query' => implode(" ", $name)
            )
        );
    }
    
    if (!empty($end_date)) {
        $params['body']['query']['bool']['must'][] = array(  <--- CHANGE
            'range' => array(
                'datehistory' => array(
                    "from" => $strat_date,
                    "to" => $end_date
                )
            )
        );
    }
    
    // special case if none is present, just match everything
    if (count($params['body']['query']['bool']['must']) == 0) {  <-- CHANGE
        $params['body']['query'] = array(
            'match_all' => array()
        );
    }
    
    $docs = $client->search($params);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等