2013-10-01 148 views
5

我正在研究下面的代码,我想要做的是按对象本身查询。嵌套弹性搜索

例如:我有一个搜索表单,填充下面的对象字段。那么我想要做的就是根据用户填写的表单来搜索弹性搜索。

ie:below,我想通过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; 
      } 
     } 
    } 
} 

回答

5

不能:)

NEST无法推断如何最好地查询只基于部分填充的POCO。如果它或者或者应该做一个嵌套的词条查询或者一个包含在has_child中的词条查询?你赶上我的漂移。

鸟巢确实有一个漂亮的功能,称为conditionless查询,让你写出来,希望整个查询,像这样:

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看到传递给期限参数为空或空它只是惯于渲染查询的一部分。

在这里了解更多此功能的工作原理http://nest.azurewebsites.net/concepts/writing-queries.html

+0

感谢您的快速提示。将查看您的文档。 – helloworld