2016-11-18 50 views
0

如何NEST 1.x的表情下面可以改寫巢2.x或5.x的NEST 2.x的條款查詢無法接受2個參數

var searchResult = _elasticClient.Search<SearchResult>(
       request => request 
        .MinScore(0.7) 
        .Query(q => 
        { 
         QueryContainer query = null; 
         query &= q.Terms<int>(t => t.Categories 
         .SelectMany(s => s.ChildCategories.Select(c => c.Id)) 
         .ToArray(), 
         categories.Select(c => Convert.ToInt32(c))); 

接受它包含哪些元素的ID列表()彈性搜索查詢應該匹配

query &= q.Terms(c => c.Field(t => t.Categories.SelectMany(s => s.ChildCategories.Select(d => d.Id)))); 

This line will below complain about Terms has 1 parameter, but invoked with 2 
query &= q.Terms(c => c.Field(t => t.Categories.SelectMany(s => s.ChildCategories.Select(d => d.Id))), new List<int> {1}); 

UPDATE:

elasticsearch documentation for 1.X最後一個例子包含字段和 qff.Terms(p值=> p.Country,userInput.Countries),我想在NEST 5.x或2.x中實現。

回答

0

Take a look at the Terms query documentation。 A terms查詢需要包含要匹配的術語和匹配的術語集合的字段。

要匹配的字段可以使用.Field(),which can take anything from which a Field can be inferred指定,包括字符串或Lambda表達式。

要匹配的值可以使用.Terms()來指定,這是一組術語。

鑑於以下POCO

public class Project 
{ 
    public IEnumerable<string> Tags { get; set; } 
} 

在標籤領域的terms查詢是

var searchResponse = client.Search<Project>(s => s 
    .Query(q => q 
     .Terms(t => t 
      .Field(f => f.Tags) 
      .Terms("tag1", "tag2", "tag3") 
     ) 
    ) 
);