symfony - ElasticSearch如何按2级嵌套项目进行搜索?

标签 symfony elasticsearch foselasticabundle

我有这种数据结构

[
    {
        id:1,
        translations: [
           {
               language: {id:1; name: "English"},
               value: "How are you ?"
           },
           {
               language: {id:2; name: "French"},
               value: "Comment allez-vous ?"
           },
           ...
        ]
    },
    ...
]

因此,现在我想进行一个查询,该查询将仅以英语翻译而不是法语或另一种语言搜索该词。并且如果用户键入“Comment allez-vous”,他将看不到任何结果。

这是config.yml
        index_name: %es.index_name%
        types:
            vocabularyItem:
                mappings:
                    translations:
                        type: "nested"
                        properties:
                            value: {boost: 5}
                            definition: {boost: 2}
                            alternativeTranslations:
                                type: "nested"
                                properties:
                                    value: ~
                            language:
                                type: "nested"
                                properties:
                                    id:
                                        type : integer
                persistence:
                    driver: orm
                    model: Bundle\Model\VocabularyItem
                    provider:
                        batch_size: 100
                    listener:
                        immediate: ~
                    finder: ~

最佳答案

首先,我建议您使用自定义存储库。这只是一个类巫婆搜索功能。这对于保持 Controller 清洁很有用。

在您的elastica配置中:

types:
    vocabularyItem:
        mappings:
            ...
        persistence:
            ...
            repository: AppBundle\SearchRepository\VocabularyItemRepository

然后在您的 Controller 中(假设索引的名称是app)
$vocabularyItemRepo = $this->get('fos_elastica.repository_manager')->getRepository('app/vocabularyItem');
$results = $vocabularyItemRepo->searchEnglish($keywords);

最后是有趣的部分,即带有查询的存储库
<?php

namespace AppBundle\SearchRepository;

use FOS\ElasticaBundle\Repository;
... //other use

class VocabularyItemRepository extends Repository
{
    public function searchEnglish($keywords)
    {
        $query = new BoolQuery();

        $englishQuery = new Match();
        $englishQuery->setField('translations.language.name', 'English');
        $query->addMust($englishQuery);

        $keywordsQuery = new Match();
        $keywordsQuery->setField('yourKeyWordField', $keywords);
        $query->addMust($keywordsQuery);

        return $this->find($query);
    }
}

请尝试这个,告诉我是否有问题

关于symfony - ElasticSearch如何按2级嵌套项目进行搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40463248/

相关文章:

symfony - SonataAdmin 自定义表单操作

php - Symfony3 Form 组件尝试将 null 传递给 PHP 7 中的类型提示方法

elasticsearch - Elasticsearch AND条件与词条查询

elasticsearch - 在 Elasticsearch 中映射以下非结构化数据的最佳方法是什么?

Symfony2和Doctrine 2结果数据错误地返回空数组

php - 无法在 Symfony 中为多对一关联设置 NULL 值

python - Elasticsearch 在对系统进行任何操作时出错?

node.js - elastic js v5.0文档中没有id时如何自动生成id

symfony - Symfony2/弹性束

symfony - FOSElasticaBundle 和 Doctrine Hydration