elasticsearch 使用Nest搜索文本时,无法将类型“string”隐式转换为“System.Collections.Generic.IEnumerable< string>

mf98qq94  于 2023-02-18  发布在  ElasticSearch
关注(0)|答案(1)|浏览(189)

尝试使用Nest搜索文本时遇到代码错误 “无法将类型”string“隐式转换为”System.Collections.Generic.IEnumerable”

class Program
  {
    static void Main(string[] args)
    {
        var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
            .DefaultIndex("my_index");

        var client = new ElasticClient(settings);

        var text = "testinsg, test, my testing";
        var analyzeResponse = client.Indices.Analyze(new AnalyzeRequest
        {
            // error occurred here when trying to pass the value of the variable text
            Text = text,
            Analyzer = "standard",
            Tokenizer = "standard"
        });
        var stemmedWords = analyzeResponse.Tokens.Select(t => t.Token);
        var stemCounts = stemmedWords
            .GroupBy(w => new Stemmer().Stem(w))
            .Select(g => new { Stem = g.Key, Count = g.Count() })
        foreach (var stemCount in stemCounts)
        {
            Console.WriteLine($"Stem: {stemCount.Stem}, Count: {stemCount.Count}");
        }
    }
}
oprakyz7

oprakyz71#

看起来TextIEnumerable<string>,所以只需将字符串 Package 到集合中,例如array:

new AnalyzeRequest
{
    Text = new [] {text},
    // ...
}

相关问题