您的查询不正确。假设以下POCO
public class Product
{
public string Name { get; set; }
public List<Variant> Variants { get; set; }
public List<int> AttachedCategoryIds { get; set; }
}
public class Variant
{
public string LongDescription { get; set; }
}
查询应该是
var index = "index_name";
var categoryId = 1;
var value = "this is the query";
var take = 20;
var query = new SearchDescriptor<Product>()
.Index(index)
.Query(q => q
.Bool(b => b
.Must(s => s
.QueryString(m => m
.Query(value)
.Fields(ff => ff
.Field(f => f.Name)
.Field(f => f.Variants.First().LongDescription)
)
.Type(TextQueryType.CrossFields)
)
)
.Filter(f => f
.Term(ff => ff.AttachedCategoryIds, categoryId)
)
)
)
.Size(take)
.Sort(ss => ss.Descending(SortSpecialField.Score));
var searchResponse = client.Search<Product>(query);
几点
-
.Field(f => f.Variants.First().LongDescription)
是一个
表达
这将解析为一个字符串,该字符串将在JSON中序列化,以在ElasticSearch中针对某个字段。在这种情况下,这将解决
"variants.longDescription"
-
一
term
query
可用于确定ElasticSearch中的字段是否包含特定值。我已将查询放入
bool
查询筛选子句,因为我认为您不想为查询的这一部分计算相关性得分,即文档要么在字段中包含该术语,要么不包含该术语。
此操作将序列化到以下查询
POST http:
{
"query": {
"bool": {
"filter": [
{
"term": {
"attachedCategoryIds": {
"value": 1
}
}
}
],
"must": [
{
"query_string": {
"fields": [
"name",
"variants.longDescription"
],
"query": "this is the query",
"type": "cross_fields"
}
}
]
}
},
"size": 20,
"sort": [
{
"_score": {
"order": "desc"
}
}
]
}
此查询假设
Variants
在
Product
被映射为
object
数据类型。它可以更多
succinctly written
作为
var query = new SearchDescriptor<Product>()
.Index(index)
.Query(q => q
.QueryString(m => m
.Query(value)
.Fields(ff => ff
.Field(f => f.Name)
.Field(f => f.Variants.First().LongDescription)
)
.Type(TextQueryType.CrossFields)
) && +q
.Term(ff => ff.AttachedCategoryIds, categoryId)
)
.Size(take)
.Sort(ss => ss.Descending(SortSpecialField.Score));