希望你很好。

我是 Elasticsearch 的新手,当您有可以应用某些逻辑(例如设置了某些内容,包括某些内容等)的条件时,使用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

最佳答案

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

因此,请尝试这种方法。我添加了如何使用排序作为示例。

    $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);

09-25 20:33