2016-11-07 7 views
1

は、私は今、私はフランス人か、別の1にのみ英訳の単語を検索しないことになるクエリを作りたいので、データ構造のこの種ElasticSearchで2レベルのネストされたアイテムを検索する方法は?

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

を持っています。また、ユーザーが「対話的にコメントする」と入力すると、結果は表示されません。ここ

とは、私はあなたがカスタムリポジトリを使用することをお勧めしますすべての

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

こんにちはHayk、 "for_elastica:"の下にyamlマッピング設定を表示できますか? – Elyass

+0

こんにちは@Elyassさん、ちょうど投稿を更新しました。見てください –

+0

ありがとうございます。ネストされたクエリをご覧くださいhttps://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html、私はあなたの問題の答えを準備します – Elyass

答えて

0

最初config.ymlです。それは単なるクラスウィッチの検索機能です。これは、コントローラをきれいに保つのに便利です。あなたのエラスティカの設定で

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

その後、あなたのコントローラで

$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); 
    } 
} 
でリポジトリ(索引の名前は、アプリと仮定)

これを試して、何かが間違っている場合は教えてください