2014-10-19 211 views
0

我需要使用NEST客戶端在單個字段上使用多個類似條件從ES獲取文檔。使用elasticsearch的QueryString查詢中的多個值NEST

我的查詢是:

SELECT * FROM Customer WHERE CustomerName LIKE '%john%' OR CustomerName Like '%george%' 

我的彈性搜索NEST查詢(單像操作)是:

var customers= ElasticSearchHelper.ElasticClient.SearchAsync<Customer>(body => body 
       .Take(100000) 
       .Filter(f => f 
        .And 
        (
         fs=> fs.Query(q=> .QueryString(qs => qs 
           .Query("*" + SearchText + "*") 
           .OnField(new string[] { "FirstName"}) 
           .DefaultOperator(Operator.or) 
           .MinimumShouldMatchPercentage(100) 
           .AnalyzeWildcard(true))) 
        ))); 
      return customers.Documents; 

我怎樣才能做到這一點有多個同樣的操作,在單場?請指導我做錯了什麼。

回答

2

你需要使用什麼是過濾器或用正則表達式過濾器組合:

SearchDescriptor<T> searchDescriptor = new SearchDescriptor<T>(); 
FilterDescriptor<T> filterDescriptor = new FilterDescriptor<T>(); 

FilterContainer filterContainer1 = new FilterContainer(); 
filterContainer1 = filterDescriptor.Regexp(rg => 
        rg.OnField("CustomerName").Value(".*" + "john" + ".*")); 

FilterContainer filterContainer2 = new FilterContainer(); 
filterContainer2 = filterDescriptor.Regexp(rg => 
        rg.OnField("CustomerName").Value(".*" + "george" + ".*")); 

searchDescriptor.Filter(flt => 
      flt.Or(filterContainer1, filterContainer2)); 

var resulet = this.ElasticClient().Search<T>(body => searchDescriptor); 

會產生以下查詢(T爲您的文檔的類型):

{ 
     "filter": { 
     "or": { 
      "filters": [ 
      { 
       "regexp": { 
       "CustomerName": { 
        "value": ".*john.*" 
       } 
       } 
      }, 
      { 
       "regexp": { 
       "CustomerName": { 
        "value": ".*doe.*" 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
相關問題