C# 使用ElasticSearch上的嵌套-如何使用部分填充的对象作为搜索条件
因此,我成功地创建了“Package”对象的索引,一个直接的文本查询工作得非常好 我想知道是否/如何使用部分填充的对象(类型为“Package”)作为搜索条件 包看起来像:C# 使用ElasticSearch上的嵌套-如何使用部分填充的对象作为搜索条件,c#,
elasticsearch,nest,C#,
elasticsearch,Nest,因此,我成功地创建了“Package”对象的索引,一个直接的文本查询工作得非常好 我想知道是否/如何使用部分填充的对象(类型为“Package”)作为搜索条件 包看起来像: var packages = new List<Package> { new Package { Name = "Maverick", TargetBusiness = new Business {
var packages = new List<Package> {
new Package {
Name = "Maverick",
TargetBusiness = new Business {
Industry = "Retail",
BusinessType = BusinessType.Product,
LocationType = LocationType.Store
},
Description = "Standard package for retail shops"
},
new Package {
Name = "Goose",
TargetBusiness = new Business {
Industry = "Retail",
BusinessType = BusinessType.Product,
LocationType = LocationType.Online
},
Description = "Standard package for ecommerce shops"
},
new Package {
Name = "Viper",
TargetBusiness = new Business {
Industry = "Advertising",
BusinessType = BusinessType.Service,
LocationType = LocationType.Office
},
Description = "Standard package test retail"
}
}
var result = client.Search<Package>(x => x.Query(q => q.QueryString(qs => qs.Query("q=retail"))));
var result = client.Search<Package>(x => x.Query(q => q.Object(new Package{...etc ...})));
var packages=新列表{
新包装{
Name=“Maverick”,
TargetBusiness=新业务{
Industry=“Retail”,
BusinessType=BusinessType.Product,
LocationType=LocationType.Store
},
Description=“零售店标准包装”
},
新包装{
Name=“Goose”,
TargetBusiness=新业务{
Industry=“Retail”,
BusinessType=BusinessType.Product,
LocationType=LocationType.Online
},
Description=“电子商务商店的标准套餐”
},
新包装{
Name=“Viper”,
TargetBusiness=新业务{
行业=“广告”,
BusinessType=BusinessType.Service,
LocationType=LocationType.Office
},
Description=“标准包装测试零售”
}
}
查询当前看起来像:
var packages = new List<Package> {
new Package {
Name = "Maverick",
TargetBusiness = new Business {
Industry = "Retail",
BusinessType = BusinessType.Product,
LocationType = LocationType.Store
},
Description = "Standard package for retail shops"
},
new Package {
Name = "Goose",
TargetBusiness = new Business {
Industry = "Retail",
BusinessType = BusinessType.Product,
LocationType = LocationType.Online
},
Description = "Standard package for ecommerce shops"
},
new Package {
Name = "Viper",
TargetBusiness = new Business {
Industry = "Advertising",
BusinessType = BusinessType.Service,
LocationType = LocationType.Office
},
Description = "Standard package test retail"
}
}
var result = client.Search<Package>(x => x.Query(q => q.QueryString(qs => qs.Query("q=retail"))));
var result = client.Search<Package>(x => x.Query(q => q.Object(new Package{...etc ...})));
var result=client.Search(x=>x.Query(q=>q.QueryString(qs=>qs.Query(“q=retail”)));
但是我想要一些类似的东西:
var packages = new List<Package> {
new Package {
Name = "Maverick",
TargetBusiness = new Business {
Industry = "Retail",
BusinessType = BusinessType.Product,
LocationType = LocationType.Store
},
Description = "Standard package for retail shops"
},
new Package {
Name = "Goose",
TargetBusiness = new Business {
Industry = "Retail",
BusinessType = BusinessType.Product,
LocationType = LocationType.Online
},
Description = "Standard package for ecommerce shops"
},
new Package {
Name = "Viper",
TargetBusiness = new Business {
Industry = "Advertising",
BusinessType = BusinessType.Service,
LocationType = LocationType.Office
},
Description = "Standard package test retail"
}
}
var result = client.Search<Package>(x => x.Query(q => q.QueryString(qs => qs.Query("q=retail"))));
var result = client.Search<Package>(x => x.Query(q => q.Object(new Package{...etc ...})));
var result=client.Search(x=>x.Query(q=>q.Object(新包{…etc…}));
我希望我说的有道理:D
提前谢谢
var result = client.Search<Package>(x => x.Query(q => q.Object(new Package{...etc ...})));
在上面的示例中,您必须创建自己的方法,该方法根据您的包提供查询字符串查询中要搜索的所有术语
i、 e
公共字符串MyPackageQueryString(包)
{
var myTerms=List();
myTerms.Add(package.Name);
if(package.TargetBusiness!=null)
{
myTerms.Add(软件包行业)
....
}
...
返回string.Join(“,myTerms.Where(t=>!string.IsNullOrWhiteSpace(t));
}
然后
client.Search<Package>(s=>s
.From(0)
.Size(10)
.Filter(f=>
f.Term(p=>p.TargetBusiness.Industry, "Advertising")
&& f.Exists(p=>p.Name)
)
.Query(q=>
q.QueryString(qs=>qs
.Query(this.MyPackageQueryString(package))
.Operator(Operator.or)
)
)
)
client.Search(s=>s
.从(0)
.尺寸(10)
.Filter(f=>
f、 术语(p=>p.TargetBusiness.Industry,“广告”)
&&f.Exists(p=>p.Name)
)
.Query(q=>
q、 查询字符串(qs=>qs
.Query(此.MyPackageQueryString(包))
.操作员(操作员或)
)
)
)
这真的帮助我走上了正确的道路——最终看起来像是一种交叉搜索:用我不想要的每一个字段搜索每一个字段
结果(可能不正确)是:
return _searchClient.Search<Package>(s => s.Query(q =>
q.Term("industry", criteriaPackage.TargetBusiness.Industry.ToLower()) ||
q.Term("description", criteriaPackage.TargetBusiness.Description.ToLower()) ||
q.Term("businessType",((int)criteriaPackage.TargetBusiness.BusinessType).ToString()) ||
q.Term("locationType", ((int)criteriaPackage.TargetBusiness.LocationType).ToString()) ||
q.Term("marketSegment", criteriaPackage.TargetBusiness.MarketSegment.ToLower()) ||
q.Term("offer", criteriaPackage.TargetBusiness.Offer.ToLower()))
).Documents;
return\u searchClient.Search(s=>s.Query(q=>
q、 术语(“行业”,criteriaPackage.TargetBusiness.industry.ToLower())||
q、 术语(“description”,criteriaPackage.TargetBusiness.description.ToLower())||
q、 术语(“业务类型”((int)criteriaPackage.TargetBusiness.businessType.ToString())||
q、 术语(“locationType”,((int)criteriaPackage.TargetBusiness.locationType.ToString())||
q、 术语(“marketSegment”,criteriaPackage.TargetBusiness.marketSegment.ToLower())||
q、 术语(“offer”,criteriaPackage.TargetBusiness.offer.ToLower())
).文件;
经过几次单元测试后,它似乎产生了我想要的结果。好的,这意味着根据每个其他属性权限搜索每个属性?是的,虽然可以轻松地用“field:value”语法替换它,但这可能是要添加到NEST中的内容。我需要让这个想法有点深入人心:)