我正在下面的代码,我想做的是按对象本身查询。
例如:我有一个搜索表单,其中填充了如下的对象字段。然后,我要做的就是根据用户填写表单的方式搜索Elastic搜索。
即:下面,我想通过searchItem对象查询索引。我如何轻松做到这一点?
class Program
{
static void Main(string[] args)
{
var p = new Program();
var item1 = new Announcement() {Id=1, Title = "john", ContentText = "lorem", Bar = false, Num = 99, Foo = "hellow"};
//p.Index(item1, "add");
var searchItem = new Announcement() {Title="john",Num=99};
ElasticClient.Search<Announcement>();
Console.Read();
}
public void Index(Announcement announcement, String operation)
{
var uriString = "http://localhost:9200";
var searchBoxUri = new Uri(uriString);
var settings = new ConnectionSettings(searchBoxUri);
settings.SetDefaultIndex("test");
var client = new ElasticClient(settings);
if (operation.Equals("delete"))
{
client.DeleteById("test", "announcement", announcement.Id);
}
else
{
client.Index(announcement, "test", "announcement", announcement.Id);
}
}
private static ElasticClient ElasticClient
{
get
{
try
{
var uriString = "http://localhost:9200";
var searchBoxUri = new Uri(uriString);
var settings = new ConnectionSettings(searchBoxUri);
settings.SetDefaultIndex("test");
return new ElasticClient(settings);
}
catch (Exception)
{
throw;
}
}
}
}
最佳答案
你不能:)
NEST无法推断如何仅基于部分填充的POCO进行最佳查询。它应该使用OR还是AND进行嵌套的词条查询或包含在has_child中的词条查询?你捕获我的漂移。
Nest确实具有一项称为“无条件查询”的精巧功能,可让您像这样写出整个查询:
ElasticClient.Search<Announcement>(s=>s
.Query(q=>
q.Term(p=>p.Title, searchItem.Title)
&& q.Term(p=>p.Num, searchItem.Num)
//Many more queries use () to group all you want
)
)
当NEST看到传递给Term的参数为null或为空时,它根本不会呈现查询的那部分。
在此处阅读更多有关此功能如何工作的信息http://nest.azurewebsites.net/concepts/writing-queries.html
关于c# - Nest弹性搜寻,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19125843/