【问题标题】:NEST C# Get part of text where word/phrase appearsNEST C#获取出现单词/短语的部分文本
【发布时间】:2017-06-17 23:22:53
【问题描述】:

我正在将 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。 但是我没有看到只获取其中一部分或部分的选项,其中出现了单词。有可能吗?

非常感谢!!

【问题讨论】:

  • Stack Overflow 不是代码编写服务,请向我们展示您已经尝试解决此问题的代码,并告诉我们为什么它不起作用。
  • @RussCam 我读过这个,但这不是我想要的。我只想做和谷歌一样的事情。当您搜索一个短语时,它会在您的单词或短语出现的部分文本中显示结果。
  • @César 这正是高亮显示的内容;搜索单词或短语并在出现该单词或短语的_source 中突出显示。在您的应用程序中,您可以截断 _source 以仅显示每个突出显示的命中周围的一部分。
  • @RussCam 好的,那我试试!非常感谢。

标签: c# elasticsearch nest


【解决方案1】:

可以。试试这个

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;
                    }
                }

            }

【讨论】:

  • 谢谢。是的,这是在 C# 代码中执行此操作的一种方式。但我的问题是,NEST 是否有一种方法或类似的方法可以得到我正在寻找的结果。
猜你喜欢
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多