2017-02-01 8 views
-2

NEST C#でElasticsearchを使用していますが、検索された単語または句が表示されるテキストの一部を取得できるかどうかは疑問でした。例えばNEST C#単語/句が表示される部分のテキストを取得

:私たちは、ドキュメントの分野で、このテキストを使用している場合:

「ウィリアム・シェイクスピアは英語の詩人、劇作家、俳優だった彼は、ストラトフォード・アポン・エイボンで1564年4月26日に生まれました。彼の父親は成功した地元のビジネスマンであり、彼の母親は土地所有者の娘であったシェイクスピアは、英国の偉大な作家と世界有数の劇作家として広く知られています。彼は約38の演劇、154のソネット、2つの長い物語の詩、いくつかの他の詩を書いたが、その中のいくつかの著者は不明である。彼の演劇はあらゆる主要な言語に翻訳され、他の劇作家。

そして、私は "バードオブアボン"を検索します。私の言葉やフレーズが表示されたフルテキストの部分と周囲の言葉を得る方法はありますか?ような何か: "...イギリスの国民詩人と呼ばれ、エイボンの吟遊詩人の愛称で呼ばれ、彼は約38演劇、154のソネットを書いた...。"

私は検索:

var result = client.Search<Books>(b => b 
     .Size(100) 
     .Query(
      query => query.Match(
       s => s. 
        Field(p => p.ContainedText).Query("Bard of Avon")))); 

、たとえば、最初の文書を保存すると、この文書が持つすべてのフィールドにアクセスできます。

var doc = res.Documents.First(); 

私は今すべてのテキスト(シェイクスピアの段落)を持つdoc.ContainedTextを持っています。 しかし、私は、単語が現れる場所の一部または一部を取得するためのオプションは表示されません。出来ますか?

多くの感謝!

+2

Stack Overflowはコード作成サービスではありません。この問題を解決しようとしているコードを表示して、なぜ機能していないのかをお知らせください。 – Ben

+0

@Césarハイライトを見てください:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html –

+0

@RussCamこれを読んだことがありますが、それは私が望むものではありません。私はGoogleと同じことをしたいだけです。フレーズを検索すると、単語やフレーズが表示される部分のテキストが表示されます。 –

答えて

0

はい可能です。これを試してください

string mytext = "William Shakespeare was an English poet, playwright, and actor. He was born on 26 April 1564 in Stratford-upon-Avon. His father was a successful local businessman and his mother was the daughter of a landowner. Shakespeare is widely regarded as the greatest writer in the English language and the world's pre-eminent dramatist. He is often called England's national poet and nicknamed the Bard of Avon. He wrote about 38 plays, 154 sonnets, two long narrative poems, and a few other verses, of which the authorship of some is uncertain. His plays have been translated into every major living language and are performed more often than those of any other playwright."; 
      string tofind = "Bard of Avon"; 
      string resltantSentence = ""; 
      if (mytext.Contains(tofind)) 
      { 
       // suppose tofind is InStart/In Middle /In the End of a sentence. 
       // and we want the whole sentence out. 
       //so first split all sentences in arrays 
       string[] sentences = mytext.Split(". "); 
       foreach (string sentence in sentences) 
       { 
        //and find the string in each sentence, 
        // if the string is repeated in multiple sentences, then you can have array of "resltantSentence" instead and comment the break statement. 
        if (sentence.Contains(tofind)) 
        { 
         resltantSentence = sentence; 
         break; 
        } 
       } 

      } 
+0

ありがとうございます。はい、これはC#コードで行う方法です。しかし、私の質問は、NESTが、私が探している結果を得るための方法などを持っているかどうかです。 –

関連する問題