FOS Elastica,在整个句子中搜索

时间:2016-05-24 07:52:02

标签: elasticsearch foselasticabundle

我对elasticsearch和FOSElastica很新, 我想获得maped字段的相关结果,它代表了学校的名称。

我的配置:

fos_elastica:
clients:
    default: { host: localhost, port: 9200 }
indexes:
    app:
        settings:
                index:
                  analysis:
                    analyzer:
                      czech :
                        tokenizer:    standard
                        filter   :    [czech_stop, czech_stemmer ,lowercase, asciifolding]
                    filter:
                      czech_stop:
                        type:   stop
                        stopwords:  _czech_
                      czech_stemmer:
                        type:   stemmer
                        languague:  czech

        types:
            school:
                mappings:
                    name:
                        type: string
                        analyzer : czech
                        boost : 10
                persistence:
                    driver: orm
                    model:  Yearbook\SUBundle\Entity\School
                    provider: ~
                    listener:
                       immediate: ~
                    finder: ~

在名称字段中存储学校名称。我使用命令行fos:elastica:search school "Hi"获得零结果,但搜索整个单词的工作正常fos:elastica:search school "High"

我还采用不同的方式在控制器中获得结果,但没有一种方法可行。我认为问题可能出在配置文件中。

我没有发现任何相关问题。 谢谢各位回复

1 个答案:

答案 0 :(得分:1)

找到解决方案,

将custom_filter EdgeNGram添加到 confing 文件中:

fos_elastica:
clients:
    default: { host: localhost, port: 9200 }
indexes:
    app:
        settings:
                index:
                  analysis:
                    analyzer:
                      custom_search_analyzer :
                        type     :    custom
                        tokenizer:    standard
                        filter   :    [czech_stop, czech_stemmer ,lowercase, asciifolding,standard]
                      custom_index_analyzer :
                        tokenizer:    standard
                        type     :    custom
                        filter   :    [czech_stop, czech_stemmer ,lowercase, asciifolding,standard,custom_filter]
                    filter:
                      custom_filter:
                          type: edgeNGram
                          side: front
                          min_gram: 1
                          max_gram: 20
                      czech_stop:
                        type:   stop
                        stopwords:  _czech_
                      czech_stemmer:
                        type:   stemmer
                        languague:  czech

        types:
            school:
                mappings:
                    name:
                        type: string
                        search_analyzer : custom_search_analyzer
                        index_analyzer : custom_index_analyzer
                        boost : 10
                    urlFriendly:
                        type: string
                persistence:
                    driver: orm
                    model:  Yearbook\SUBundle\Entity\School
                    provider: ~
                    listener:
                       immediate: ~
                    finder: ~

获取控制器中的结果列表:

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

        $index =$this->container->get('fos_elastica.index.app.school');

        $searchQuery=new QueryString();
        $searchQuery->setParam('query',$needle);
        $searchQuery->setDefaultOperator('OR');
        $searchQuery->setParam('fields',array('name','urlFriendly'));
        $results=$index->search($searchQuery,10)->getResults();