我有这种数据结构
[
{
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/