[英]NEST C# - elasticsearch - ecommerce filter combination
我正在嘗試將彈性搜索應用到我的網上商店,但在使用過濾器方面遇到了一些麻煩。 過濾是動態完成的。
例:
我首先展示了所有被索引的產品。 所以沒有應用過濾器。 訪客可以選擇自己的過濾器,如:顏色,尺寸,品牌,類型,類別,....
但我現在不知道如何使用elasticsearch和NEST構建搜索結果。
這是我沒有過濾的解決方案:
var query = ElasticClient.Search<Product>(s => s
.From(from)
.Size(size)
);
我還有另一個關於索引集合<>或列表<>的問題。 我不得不在這些集合上使用JsonIgnore。 我可以索引那些嗎?
這是我的班級:
/// <summary>
/// Represents a product
/// </summary>
public partial class Product {
private ICollection<ProductCategory> _productCategories;
private ICollection<ProductManufacturer> _productManufacturers;
private ICollection<ProductPicture> _productPictures;
/// <summary>
/// Gets or sets the name
/// </summary>
public virtual string Name { get; set; }
/// <summary>
/// Gets or sets the short description
/// </summary>
public virtual string ShortDescription { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the entity is published
/// </summary>
public virtual bool Published { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the entity has been deleted
/// </summary>
public virtual bool Deleted { get; set; }
/// <summary>
/// Gets or sets the date and time of product creation
/// </summary>
public virtual DateTime CreatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the date and time of product update
/// </summary>
public virtual DateTime UpdatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the collection of ProductCategory
/// </summary>
[JsonIgnore] /* added - wesley */
public virtual ICollection<ProductCategory> ProductCategories
{
get { return _productCategories ?? (_productCategories = new List<ProductCategory>()); }
protected set { _productCategories = value; }
}
/// <summary>
/// Gets or sets the collection of ProductManufacturer
/// </summary>
[JsonIgnore] /* added - wesley */
public virtual ICollection<ProductManufacturer> ProductManufacturers
{
get { return _productManufacturers ?? (_productManufacturers = new List<ProductManufacturer>()); }
protected set { _productManufacturers = value; }
}
/// <summary>
/// Gets or sets the collection of ProductPicture
/// </summary>
[JsonIgnore] /* added - wesley */
public virtual ICollection<ProductPicture> ProductPictures
{
get { return _productPictures ?? (_productPictures = new List<ProductPicture>()); }
protected set { _productPictures = value; }
}
}
有人可以幫助我嗎?
請務必閱讀有關編寫查詢的完整文檔: http : //nest.azurewebsites.net/nest/writing-queries.html
接下來是從那里粘貼的摘錄。
編寫復雜的布爾查詢是一回事,但更常見的是,您不希望根據用戶輸入來決定如何進行查詢。
public class UserInput
{
public string Name { get; set; }
public string FirstName { get; set; }
public int? LOC { get; set; }
}
接着
.Query(q=> {
QueryDescriptor<ElasticSearch> query = null;
if (!string.IsNullOrEmpty(userInput.Name))
query &= q.Term(p=>p.Name, userInput.Name);
if (!string.IsNullOrEmpty(userInput.FirstName))
query &= q
.Term("followers.firstName", userInput.FirstName);
if (userInput.LOC.HasValue)
query &= q.Range(r=>r.OnField(p=>p.Loc).From(userInput.Loc.Value))
return query;
})
這又一次變得乏味和冗長。 因此,nest允許您將以前的查詢編寫為:
.Query(q=>
q.Term(p=>p.Name, userInput.Name);
&& q.Term("followers.firstName", userInput.FirstName)
&& q.Range(r=>r.OnField(p=>p.Loc).From(userInput.Loc))
)
如果任何查詢導致空查詢,則不會將其發送到elasticsearch。
因此,如果userInput上的所有術語都為null(或空字符串),除了userInput.Loc,它甚至不會將范圍查詢包裝在布爾查詢中,而只是發出一個普通范圍查詢。
如果所有這些都為空,則會產生match_all查詢。
默認情況下會啟用此無條件行為,但可以像這樣調整:
var result = client.Search<ElasticSearchProject>(s=>s
.From(0)
.Size(10)
.Strict() //disable conditionlessqueries by default
///EXAMPLE HERE
);
但是查詢本身可以選擇重新進入或退出。
.Query(q=>
q.Strict().Term(p=>p.Name, userInput.Name);
&& q.Term("followers.firstName", userInput.FirstName)
&& q.Strict(false).Range(r=>r.OnField(p=>p.Loc).From(userInput.Loc))
)
在此示例中,如果userInput.Name為null或為空,則將導致DslException。 無論SearchDescriptor是否使用.Strict(),范圍查詢都將使用無條件邏輯。
另外值得注意的是無條件查詢邏輯傳播:
q.Strict().Term(p=>p.Name, userInput.Name);
&& q.Term("followers.firstName", userInput.FirstName)
&& q.Filtered(fq => fq
.Query(qff =>
qff.Terms(p => p.Country, userInput.Countries)
&& qff.Terms(p => p.Loc, userInput.Loc)
)
)
如果userInput.Countries和userInput.Loc都為null或為空,則不會發出整個篩選的查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.