2013-10-01 196 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