ElasticSearchDSL:如果设置,则按相关性或其他字段排序

时间:2017-08-31 11:09:09

标签: php elasticsearch ongr

希望你顺利。

我是弹性搜索的新手,当你有标准可以应用某些逻辑时,使用ElasticSearchDSL插件是非常了不起的,例如设置某些东西,包括它等等。

我似乎打了一个砖墙我希望能够通过相关性订购默认但是请给出通过date_closing,date_posted等订购的选项(它的工作搜索功能)。

除非我错过了ONGR网站上的一些术语,否则任何人都会在ElasticSearchDSL之前出现这种情况?

我在当前代码之下(如果需要):

$termQueryForTitle = new ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery('title', $keyword);
        $termQueryForDescription = new ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery('description', $keyword);
        $termQueryForStatus = new ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery('status', 1);
        $termQueryForDeleted = new ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery('is_deleted', 0);
        $rangeQueryDateClosing = new ONGR\ElasticsearchDSL\Query\TermLevel\RangeQuery('date_closing', ['gte' => date('Y-m-d')]);
        $termsQuerySector = new ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery('job_sector_id', [implode('\', \'', array_filter($sector_id))]);
        $termsQuerySubject = new ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery('job_subject_id', [implode('\', \'', array_filter($subject_id))]);
        $termsQueryArea = new ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery('area_id', [implode('\', \'', array_filter($area_id))]);
        $termsQueryTown = new ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery('town_id', [implode('\', \'', array_filter($town_id))]);

        $bool = new ONGR\ElasticsearchDSL\Query\Compound\BoolQuery();
        //$bool->addParameter('minimum_should_match', 1);
        //$bool->addParameter('boost', 1);
        if($keyword != '')
        {
            $bool->add($termQueryForTitle, $bool::SHOULD);
            $bool->add($termQueryForDescription, $bool::SHOULD);
        }
        if(count($sector_id) > 0)
        {
            $bool->add($termsQuerySector, $bool::MUST);
        }
        if(count($subject_id) > 0)
        {
            $bool->add($termsQuerySubject, $bool::MUST);
        }
        if(count($area_id) > 0)
        {
            $bool->add($termsQueryArea, $bool::MUST);
        }
        if(count($town_id) > 0)
        {
            $bool->add($termsQueryTown, $bool::MUST);
        }
        $bool->add($rangeQueryDateClosing, $bool::MUST);
        $bool->add($termQueryForStatus, $bool::FILTER);
        $bool->add($termQueryForDeleted, $bool::FILTER);

        $DSL_search = new ONGR\ElasticsearchDSL\Search();
        $DSL_search->addQuery($bool);

        $query_params = $DSL_search->toArray();

        $data = $this->__elastic->Search_document('jobs', $query_params, $limit, $offset);
        //pprint_r($data);

TIA

1 个答案:

答案 0 :(得分:1)

很高兴听到你喜欢ElasticSearchDSL。首先建议,不要将BOOL查询作为独立使用。此查询旨在作为搜索对象内的帮助程序,除非您确实需要它。

所以请尝试这种方法。我添加了如何使用sort作为示例。

    $termQueryForTitle = new ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery('title', $keyword);
    $termQueryForDescription = new ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery('description', $keyword);
    $termQueryForStatus = new ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery('status', 1);
    $termQueryForDeleted = new ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery('is_deleted', 0);
    $rangeQueryDateClosing = new ONGR\ElasticsearchDSL\Query\TermLevel\RangeQuery('date_closing', ['gte' => date('Y-m-d')]);
    $termsQuerySector = new ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery('job_sector_id', [implode('\', \'', array_filter($sector_id))]);
    $termsQuerySubject = new ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery('job_subject_id', [implode('\', \'', array_filter($subject_id))]);
    $termsQueryArea = new ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery('area_id', [implode('\', \'', array_filter($area_id))]);
    $termsQueryTown = new ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery('town_id', [implode('\', \'', array_filter($town_id))]);


    $dslSearch = new ONGR\ElasticsearchDSL\Search();
    if($keyword != '') {
        $dslSearch->addQuery($termQueryForTitle, BoolQuery::SHOULD);
        $dslSearch->addQuery($termQueryForDescription, BoolQuery::SHOULD);
    }

    //.....
    //.....

    $sortField1 = new FieldSort('field_to_sort', FieldSort::ASC);
    $sortField2 = new FieldSort('second_field_to_sort', FieldSort::DESC);
    $dslSearch->addSort($sortField1);
    $dslSearch->addSort($sortField2);

    $query_params = $dslSearch->toArray();

    $data = $this->__elastic->Search_document('jobs', $query_params, $limit, $offset);
    //pprint_r($data);