我正在使用Elasticsearch和NEST C#,我想知道是否有可能获得搜索的单词或短语出现的文本的一部分。
例如:如果我们在文档的字段中包含此文本:
"威廉·莎士比亚是英国诗人,剧作家和演员。他于1564年4月26日出生在埃文河畔斯特拉特福。他的父亲是一位成功的当地商人,他的母亲是土地所有者的女儿。莎士比亚被广泛认为是英语中最伟大的作家,也是世界上最杰出的剧作家。他经常被称为英国的民族诗人,绰号为雅芳的吟游诗人。他写了大约38部戏剧,154首十四行诗,两首长篇叙事诗和其他一些经文,其中一些作者的作者不确定。他的戏剧已被翻译成各种主要的生活语言,并且比其他任何剧作家都更频繁地表演。"
我搜索" 雅芳的吟游诗人"。是否有任何方法可以使用我的单词或短语和周围的单词来获取全文的一部分?就像是: " ...被称为英格兰的国家诗人,绰号为雅芳的吟游诗人。他写了大约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(莎士比亚段落)。 但是,我没有看到一个选项,只能获取它们的一部分或多个部分。有可能吗?
非常感谢!!
答案 0 :(得分: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;
}
}
}