5

在我的 symfony2 应用程序中,我使用 FOS Elastica 包来执行搜索。

我试图设置分析器和过滤器,但似乎它们没有任何效果。例如,如果我搜索单词 'cake',则不会返回包含句子 case 'Cake' 的对象。

如何正确配置这些分析器和过滤器?

我的配置:

#Elastic Search
fos_elastica:
    default_manager: orm
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        website:
            client: default
            settings:
                index:
                    analysis:
                        analyzer:
                            custom_index_analyzer :
                                type     :    custom
                                tokenizer:    nGram
                                filter   :    [stopwords, asciifolding ,lowercase, snowball, elision, worddelimiter]
                            custom_search_analyzer :
                                type     :    custom
                                tokenizer:    nGram
                                filter   :    [stopwords, asciifolding ,lowercase, snowball, elision, worddelimiter]
                        tokenizer:
                            nGram:
                                type:     nGram
                                min_gram: 1
                                max_gram: 2
                        filter:
                            snowball:
                                type:     snowball
                                language: French
                            elision:
                                type:     elision
                                articles: [l, m, t, qu, n, s, j, d]
                            stopwords:
                                type:      stop
                                stopwords: [_french_]
                                ignore_case : true
                            worddelimiter :
                                type:      word_delimiter
            index_name: foodmeup
            types:
                recipe:
                    mappings:
                        name:
                            boost: 5
                            index_analyzer : custom_index_analyzer
                            search_analyzer : custom_search_analyzer
                        nickName:
                            index_analyzer : custom_index_analyzer
                            search_analyzer : custom_search_analyzer
                        content:
                            index_analyzer : custom_index_analyzer
                            search_analyzer : custom_search_analyzer
                        recipeIngredients:
                            type: "nested"
                            properties:
                                name:
                                    index_analyzer : custom_index_analyzer
                                    search_analyzer : custom_search_analyzer
                                product:
                                    type: "nested"
                                    properties:
                                        name: { type: string, boost: 10}
                                        nickName: { type: string }
                                        content: { type: string }
                                        tags:
                                            type: "nested"
                                            boost: 5
                                            properties:
                                                name: { type: string }
                        userRecipes:
                            type: "nested"
                            properties:
                                name:
                                    index_analyzer : custom_index_analyzer
                                    search_analyzer : custom_search_analyzer
                                content:
                                    index_analyzer : custom_index_analyzer
                                    search_analyzer : custom_search_analyzer
                        tags:
                            type: "nested"
                            boost: 5
                            properties:
                                name:
                                    index_analyzer : custom_index_analyzer
                                    search_analyzer : custom_search_analyzer
                    persistence:
                        driver: orm
                        model: AppBundle\Entity\FoodAnalytics\Recipe
                        repository: AppBundle\Repository\FoodAnalytics\RecipeRepository
                        provider: ~
                        finder: ~
                        listener: ~ # by default, listens to "insert", "update" and "delete"
                product:
                    mappings:
                        name: { type: string, boost: 10}
                        nickName: { type: string }
                        content: { type: string }
                        userIngredients:
                            type: "nested"
                            properties:
                                name: { type: string }
                                content: { type: string }
                        tags:
                            type: "nested"
                            boost: 5
                            properties:
                                name: { type: string }
                    persistence:
                        driver: orm
                        model: AppBundle\Entity\MarketPlace\Product
                        repository: AppBundle\Repository\MarketPlace\ProductRepository
                        provider: ~
                        finder: ~
                        listener: ~ # by default, listens to "insert", "update" and "delete"
                user:
                    mappings:
                        firstName: { type: string, boost: 3}
                        lastName: { type: string, boost: 10 }
                        content: { type: string }
                        username: { type: string }
                        email: { type: string }
                        jobSeeker:
                            type: "nested"
                            properties:
                                skills:
                                    type: "nested"
                                    properties:
                                        name: { type: string }
                                experiences:
                                    type: "nested"
                                    properties:
                                        position:
                                            type: "nested"
                                            properties:
                                                name: { type: string }
                                        content: { type: string }
                                trainings:
                                    type: "nested"
                                    properties:
                                        name: { type: string }
                                        content: { type: string }
                                        diploma:
                                            type: "nested"
                                            properties:
                                                name: { type: string }
                    persistence:
                        driver: orm
                        model: AppBundle\Entity\User\User
                        repository: AppBundle\Repository\User\UserRepository
                        provider: ~
                        finder: ~
                        listener: ~ # by default, listens to "insert", "update" and "delete"
                organization:
                    mappings:
                        name: { type: string, boost: 10}
                        legalName: { type: string, boost: 10}
                        shortDescription: { type: string, boost: 3}
                        route: { type: string}
                        content: { type: string }
                    persistence:
                        driver: orm
                        model: AppBundle\Entity\User\Organization
                        repository: AppBundle\Repository\User\OrganizationRepository
                        provider: ~
                        finder: ~
                        listener: ~ # by default, listens to "insert", "update" and "delete"
                offer:
                    mappings:
                        name: { type: string, boost: 10}
                        content: { type: string }
                        responsibilities: { type: string }
                        skills:
                            type: "nested"
                            properties:
                                name: { type: string }
                        contractType:
                            type: "nested"
                            properties:
                                name: { type: string }
                        position:
                            type: "nested"
                            properties:
                                name: { type: string, boost: 10 }
                    persistence:
                        driver: orm
                        model: AppBundle\Entity\Job\Offer
                        repository: AppBundle\Repository\Job\OfferRepository
                        provider: ~
                        finder: ~
                        listener: ~
                post:
                    mappings:
                        name: { type: string, boost: 10}
                        content: { type: string }
                        summary: { type: string }
                        tags:
                            type: "nested"
                            boost: 5
                            properties:
                                name: { type: string }
                        comments:
                            type: "nested"
                            properties:
                                content: { type: string }
                    persistence:
                        driver: orm
                        model: AppBundle\Entity\Social\Post
                        repository: AppBundle\Repository\Social\PostRepository
                        provider: ~
                        finder: ~
                        listener: ~

查询是一个基本的查询:

$finder = $this->container->get('website.recipe')
$elasticaResults = $finder->find($search);
4

1 回答 1

0

每次更新fos_elastica config文件时,您需要在第一次使用以下命令更新索引:

php app/console fos:elastica:populate --index website --type= <<you type>>

此命令将通过擦除前一个数据来重新索引您的数据。第二次,您应该检查您的映射信息以检查一切是否正常且同步。

您可以使用以下命令检查您的分析仪:

curl 'http://<< yourdomain >>:9200/website/_analyze?pretty=true&analyzer=custom_french_analyzer' -d "Cake"
于 2016-01-07T20:45:39.463 回答