0

原諒我的新手性,因爲我對ElasticSearch和NEST都是新手。我正在製作一個原型來評估正在實施的.NET解決方案中的ElasticSearch。原型編譯並似乎搜索,但沒有正確返回結果。它只返回幾個關鍵字的結果,只是小寫,而忽略其他關鍵字並不返回任何內容。我在想我的查詢有問題。這裏是查詢部分(假定連接信息和默認索引指定和構建)。帶NEST查詢問題的ElasticSearch

// string searchString to be searched against ProductName and Description fields.    
var searchResults = client.Search<Product>(s=>s 
      .From(0) 
      .Size(100) 
      .Query(q=>q.Term(p=>p.ProductName, searchString) || 
       q.Term(p=>p.Description, searchString) 
      )); 

這裏的模型如果需要的話:

[ElasticType(IdProperty = "ProductID")] 
public class Product 
{ 
    [ScaffoldColumn(false)] 
    [JsonIgnore] 
    public int ProductID { get; set; } 

    [Required, StringLength(100), Display(Name = "Name")] 
    public string ProductName { get; set; } 

    [Required, StringLength(10000), Display(Name = "Product Description"), DataType(DataType.MultilineText)] 
    public string Description { get; set; } 

    public string ImagePath { get; set; } 

    [Display(Name = "Price")] 
    public double? UnitPrice { get; set; } 

    public int? CategoryID { get; set; } 
    [JsonIgnore] 
    public virtual Category Category { get; set; } 
} 

感謝幫助!

+1

您是否確實希望產品名稱和說明符合您的搜索字符串? – 2014-08-27 21:46:41

+0

我希望它可以匹配,並且是的,我確實使用了OR運算符以及幾乎沒有變化。 – Michael 2014-08-27 23:49:59

+0

更新:好的,顯然我沒有去過OR運算符。現在搜索工作更好,除了它似乎弄亂了字母的情況.... 「Custom」不返回任何內容,但「custom」返回「Kat Custom Car」... – Michael 2014-08-27 23:56:26

回答

2

這裏你的問題是你使用的是term queries,它們沒有被分析,因此區分大小寫。

嘗試使用match query(被分析),而不是:

var searchResults = client.Search<Product>(s => s 
    .From(0) 
    .Size(100) 
    .Query(q => 
     q.Match(m => m.OnField(p => p.ProductName).Query(searchString)) || 
     q.Match(m => m.OnField(p => p.Description).Query(searchString)) 
    ) 
); 

把它further-一個步驟,因爲要查詢在兩個不同的領域相同的文字,你可以使用multi match query代替結合兩個單項查詢:

var searchResults = client.Search<Product>(s => s 
    .From(0) 
    .Size(100) 
    .Query(q => q 
     .MultiMatch(m => m 
      .OnFields(p => p.Product, p => p.Description) 
      .Query(searchText) 
     ) 
    ) 
); 

爲了更好地理解分析,從The Definitive Guidemapping and analysis節是一個偉大的閱讀。

+0

你釘牢得很好!這解決了我的問題。你碰巧知道如何做一個類似於「包含」的查詢? (例如「kat」Kat的......) – Michael 2014-08-28 08:39:31