elasticsearch不会返回所有匹配

时间:2015-10-11 11:35:32

标签: symfony elasticsearch

我正在使用Symfony 2.3和ElasticSearchBundle 3.0。我为搜索实现了两个字段。搜索工作正常,但不显示所有结果。例如:当我搜索a关键字时,点击次数为33次点击,但只返回10次结果 config.php

fos_elastica:
    clients:
        default: { host: localhost, port: 9200 }
    serializer:
        callback_class: FOS\ElasticaBundle\Serializer\Callback
        serializer: serializer
    indexes:
        hortis:
            finder: ~
            client: default
            settings:
                index:
                    analysis:
                        analyzer:
                            custom_search_analyzer:
                                type: custom
                                tokenizer: standard
                                filter   : [standard, lowercase, asciifolding]
                            custom_index_analyzer:
                                type: custom
                                tokenizer: standard
                                filter   : [standard, lowercase, asciifolding, custom_filter]
                        filter:
                            custom_filter:
                                type: edgeNGram
                                side: front
                                min_gram: 3
                                max_gram: 100
            types:
                business:
                    mappings:
                        name:  { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type:string }
                        enabled: ~
                        gouvernaurat: ~
                        delegation: ~
                        postal_code: ~
#                        activities.principal:  { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type:string }
                        activities : 
                            type : object
                            properties : 
                                principal : ~

                    persistence:
                        driver: orm
                        model: Toto\AdminBundle\Entity\EntityName
                        provider: ~
                        listener: ~
                        finder: ~  

controller.php

public function searchEngineAction(Request $request) {
        $finder = $this->container->get('fos_elastica.index.hortis.business');
        // get data from both fields
        $querystring = strip_tags($request->get('name'));
        $querystring2 = strip_tags($request->get('location'));
        $boolQuery = new \Elastica\Query\Bool();

        // if both fields are empty then display all businesses
        if (empty($querystring) and empty($querystring2)) {
            $query = new \Elastica\Query\MatchAll();
            $boolQuery->addMust($query);
        } else {
            // create a boolean query
            if (!empty($querystring)) {

                $fieldQuery = new \Elastica\Query\QueryString();
                $fieldQuery->setFields(array('name', 'activities.principal'));
                $fieldQuery->setQuery($querystring);
                $boolQuery->addMust($fieldQuery);
            }
            if (!empty($querystring2)) {

                $fieldQuery2 = new \Elastica\Query\QueryString();
                $fieldQuery2->setFields(array(
                    'gouvernaurat', 'delegation', 'postal_code'));
                $fieldQuery2->setQuery($querystring2);
                $boolQuery->addMust($fieldQuery2);
            }
        }

        // select only enbaled business
        $enabled = new \Elastica\Query\Term();
        $enabled->setTerm('enabled', true);
        $boolQuery->addMust($enabled);
        $findAll = \Elastica\Query::create($boolQuery);
        $findAll->setSize(27);        

        // trigger search function
       $elasticaResultSet = $finder->search($findAll);
       dump($elasticaResultSet);

        // get results from 
        $findbusinesses = $elasticaResultSet->getResults();
        $noresult = '';
        if (!$findbusinesses) {
            $noresult = 'no result';
        }

        $em = $this->getDoctrine()->getManager();
        $FrontSettings = $em->getRepository('TotoAdminBundle:FrontSettings')->getFrontSettings();
        if (!$FrontSettings) {
            throw $this->createNotFoundException('Unable to find frontSettings entity');
        }

        // get all categories and activities
        $categories = $em->getRepository('TotoAdminBundle:Category')
                ->findBy(array(), array('order' => 'ASC'));

        if (!$categories) {
            throw $this->createNotFoundException('unable to find categories and activities');
        }

        $paginator = $this->get('knp_paginator');
        $businesses = $paginator->paginate(
                $findbusinesses, $this->get('request')->query->get('page', 1)/* page number */, 9/* limit per page */
        );
        return $this->render('TotoFrontBundle:Front:search_result.html.twig', array(
                    'querystring' => $querystring, 'businesses' => $businesses,
                    'FrontSettings' => $FrontSettings, 'noresult' => $noresult,
                    'categories' => $categories,
        ));
    }  

如何显示所有匹配?

1 个答案:

答案 0 :(得分:4)

默认情况下,Elasticsearch仅返回前10个结果。可以通过指定from and size parameters来修改此设置。请注意,在一个页面上显示所有结果很少有意义,而是使用可控量的可查看项目进行分页。

如果您希望一个页面上的所有匹配也考虑使用scroll api,因为在获得大量结果时,深度分页会变得非常低效。