NEST查询Elasticsearch无法正常工作

时间:2015-04-01 12:43:51

标签: elasticsearch nest

我们正在使用NEST API与C#一起使用Elasticsearch。 虽然我们可以插入数据,但是引用对象中特定字段的查询不起作用。

例如,给定以下类:

internal class Magazine
{
   public Magazine(string id, string title, string author)
   {
      Id = id;
      Title = title;
      Author = author;
   }

   public string Id { get; set; }
   public string Title { get; set; }
   public string Author { get; set; }
}

创建类的对象并将其插入到ElasticSearch中,如下所示:

Magazine mag1= new Magazine("1", "Soccer Review", "John Smith");
Magazine mag2= new Magazine("2", "Cricket Review", "John Smith");

Uri node = new Uri("http://localhost:9200");
ConnectionSettings settings = new ConnectionSettings(node, defaultIndex: "mag-application");
ElasticClient client = new ElasticClient(settings);
client.Index(mag1);
client.Index(mag2);

以下查询有效,并返回两行:

var searchResults = client.Search<Magazine>(s => s.From(0).Size(20));

但是这个没有回报:

var searchResults = client.Search<Magazine>(s => s.From(0).Size(20).Query(q => q.Term(p => p.Author, "John Smith")));

有什么问题?

1 个答案:

答案 0 :(得分:3)

由于您使用的是标准分析仪(默认选项)&#34; John Smith&#34;字符串分为2个令牌&#34; john&#34;和&#34;史密斯&#34;。

术语查询:

  

匹配包含字词(未分析)的字段的文档。

也就是说,您要搜索的短语不会从上述分析过程中消失。

尝试搜索

client.Search<Magazine>(s => s.From(0).Size(20).Query(q => q.Term(p => p.Author, "john")));

或使用匹配查询,如下所示:

client.Search<Magazine>(s => s.From(0).Size(20)..Query(q => q.Match(m => m.OnField(p => p.Author).Query("John Smith"))

查看official documentation for term query以获取更多信息。

相关问题