Search code examples
elasticsearchfoselasticabundle

FOS Elastica, Search in whole sentences


I am pretty new to elasticsearch and FOSElastica, I would like to get relevant result of maped field, which represent name of the school.

my config:

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: ~

In name field is stored name of school. I get zero results using command line fos:elastica:search school "Hi", however searching for whole word works fine fos:elastica:search school "High"

I also triend different ways to get results in controller, but none of it worked. I think the problem may be in config file.

I did not find any related problem. Thank you guys for your replies


Solution

  • Found solution,

    by adding custom_filter EdgeNGram to confing file:

    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: ~
    

    getting list of results in controller:

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