use*_*610 2 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; }
}
Run Code Online (Sandbox Code Playgroud)
将创建该类的对象,并将其插入到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);
Run Code Online (Sandbox Code Playgroud)
以下查询有效,并返回两行:
var searchResults = client.Search<Magazine>(s => s.From(0).Size(20));
Run Code Online (Sandbox Code Playgroud)
但是,这没有返回任何内容:
var searchResults = client.Search<Magazine>(s => s.From(0).Size(20).Query(q => q.Term(p => p.Author, "John Smith")));
Run Code Online (Sandbox Code Playgroud)
怎么了?
由于您使用的是标准分析器(默认选项),因此“ John Smith”字符串分为2个标记“ john”和“ smith”。
术语查询:
匹配具有包含术语(未分析)的字段的文档。
也就是说,您要搜索的短语将不会通过上述分析过程。
尝试搜寻
client.Search<Magazine>(s => s.From(0).Size(20).Query(q => q.Term(p => p.Author, "john")));
Run Code Online (Sandbox Code Playgroud)
或使用以下匹配查询:
client.Search<Magazine>(s => s.From(0).Size(20)..Query(q => q.Match(m => m.OnField(p => p.Author).Query("John Smith"))
Run Code Online (Sandbox Code Playgroud)
查阅官方文档中的术语查询以获取更多信息。
| 归档时间: |
|
| 查看次数: |
919 次 |
| 最近记录: |