在 Elastic Search NEST 中的主 MultiMatch 查询之后应用不同的过滤器
Apply different filters after main MultiMatch query in Elastic Search NEST
所以,这是我的查询:
_elasticClient.Search<SearchItem>(x =>
x.Sort(sort).Size(itemsPerPage)
.Query(q =>
q.MultiMatch(m => m
.Fields(fs => fs
.Field(p => p.Field1)
.Field(p => p.Field2)
.Field(p => p.Field3)
.Field(p => p.Field4)
.Field(p => p.Field5)
.Field(p => p.Field6)
)
.Operator(Operator.And)
.Query(pattern)
)));
我必须应用不同的过滤器。范围过滤器(用于价格),过滤器结果集,其中 field1 = "Audi" 和 field2 = "Sale Car"。我试图做类似的事情:
.Query(q =>
q.MultiMatch(m => m
.Fields(fs => fs
Field(p => p.Field1)
.Field(p => p.Field2)
.Field(p => p.Field3)
.Field(p => p.Field4)
.Field(p => p.Field5)
.Field(p => p.Field6)
)
.Operator(Operator.And)
.Query(pattern)))
.Query(q=>q.Range(ra=>ra.Field(ff=>ff.SalePrice).GreaterThan(1000))));
但这不起作用。我有价格大于 1000 的索引的所有结果,但只需要搜索结果。谁能帮帮我?
您可以 use a bool
query to combine queries 并且 NEST 通过重载运算符来组合 QueryContainer
s(根查询类型)使这稍微容易一些。这是 NEST 2.x
的示例
void Main()
{
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(connectionPool);
var client = new ElasticClient(settings);
var itemsPerPage = 20;
var pattern = "match query";
client.Search<SearchItem>(x => x
.Sort(so => so
.Descending("_score")
)
.Size(itemsPerPage)
.Query(q => q
.MultiMatch(m => m
.Fields(fs => fs
.Field(p => p.Field1)
.Field(p => p.Field2)
.Field(p => p.Field3)
.Field(p => p.Field4)
.Field(p => p.Field5)
.Field(p => p.Field6)
)
.Operator(Operator.And)
.Query(pattern)
) && q
.Range(ra => ra
.Field(ff=>ff.SalePrice)
.GreaterThan(1000)
)
)
);
}
public class SearchItem
{
public int SalePrice { get; set; }
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
public string Field4 { get; set; }
public string Field5 { get; set; }
public string Field6 { get; set; }
}
产生
{
"size": 20,
"sort": [
{
"_score": {
"order": "desc"
}
}
],
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "match query",
"operator": "and",
"fields": [
"field1",
"field2",
"field3",
"field4",
"field5",
"field6"
]
}
},
{
"range": {
"salePrice": {
"gt": 1000.0
}
}
}
]
}
}
}
这将找到与 multi_match
查询匹配的文档 和 也有一个 salePrice
大于 1000。因为我们不需要为范围查询计算的分数(文档的 salePrice
大于 1000 或不大于 1000),范围查询可以 运行 在过滤器上下文中。略微精炼的版本
client.Search<SearchItem>(x => x
.Sort(so => so
.Descending("_score")
)
.Size(itemsPerPage)
.Query(q => q
.MultiMatch(m => m
.Fields(fs => fs
.Field(p => p.Field1)
.Field(p => p.Field2)
.Field(p => p.Field3)
.Field(p => p.Field4)
.Field(p => p.Field5)
.Field(p => p.Field6)
)
.Operator(Operator.And)
.Query(pattern)
) && +q
.Range(ra => ra
.Field(ff=>ff.SalePrice)
.GreaterThan(1000)
)
)
);
将一元 +
运算符附加到范围查询是布尔查询过滤器的 shorthand。查询 json 现在看起来像
{
"size": 20,
"sort": [
{
"_score": {
"order": "desc"
}
}
],
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "match query",
"operator": "and",
"fields": [
"field1",
"field2",
"field3",
"field4",
"field5",
"field6"
]
}
}
],
"filter": [
{
"range": {
"salePrice": {
"gt": 1000.0
}
}
}
]
}
}
}
所以,这是我的查询:
_elasticClient.Search<SearchItem>(x =>
x.Sort(sort).Size(itemsPerPage)
.Query(q =>
q.MultiMatch(m => m
.Fields(fs => fs
.Field(p => p.Field1)
.Field(p => p.Field2)
.Field(p => p.Field3)
.Field(p => p.Field4)
.Field(p => p.Field5)
.Field(p => p.Field6)
)
.Operator(Operator.And)
.Query(pattern)
)));
我必须应用不同的过滤器。范围过滤器(用于价格),过滤器结果集,其中 field1 = "Audi" 和 field2 = "Sale Car"。我试图做类似的事情:
.Query(q =>
q.MultiMatch(m => m
.Fields(fs => fs
Field(p => p.Field1)
.Field(p => p.Field2)
.Field(p => p.Field3)
.Field(p => p.Field4)
.Field(p => p.Field5)
.Field(p => p.Field6)
)
.Operator(Operator.And)
.Query(pattern)))
.Query(q=>q.Range(ra=>ra.Field(ff=>ff.SalePrice).GreaterThan(1000))));
但这不起作用。我有价格大于 1000 的索引的所有结果,但只需要搜索结果。谁能帮帮我?
您可以 use a bool
query to combine queries 并且 NEST 通过重载运算符来组合 QueryContainer
s(根查询类型)使这稍微容易一些。这是 NEST 2.x
void Main()
{
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(connectionPool);
var client = new ElasticClient(settings);
var itemsPerPage = 20;
var pattern = "match query";
client.Search<SearchItem>(x => x
.Sort(so => so
.Descending("_score")
)
.Size(itemsPerPage)
.Query(q => q
.MultiMatch(m => m
.Fields(fs => fs
.Field(p => p.Field1)
.Field(p => p.Field2)
.Field(p => p.Field3)
.Field(p => p.Field4)
.Field(p => p.Field5)
.Field(p => p.Field6)
)
.Operator(Operator.And)
.Query(pattern)
) && q
.Range(ra => ra
.Field(ff=>ff.SalePrice)
.GreaterThan(1000)
)
)
);
}
public class SearchItem
{
public int SalePrice { get; set; }
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
public string Field4 { get; set; }
public string Field5 { get; set; }
public string Field6 { get; set; }
}
产生
{
"size": 20,
"sort": [
{
"_score": {
"order": "desc"
}
}
],
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "match query",
"operator": "and",
"fields": [
"field1",
"field2",
"field3",
"field4",
"field5",
"field6"
]
}
},
{
"range": {
"salePrice": {
"gt": 1000.0
}
}
}
]
}
}
}
这将找到与 multi_match
查询匹配的文档 和 也有一个 salePrice
大于 1000。因为我们不需要为范围查询计算的分数(文档的 salePrice
大于 1000 或不大于 1000),范围查询可以 运行 在过滤器上下文中。略微精炼的版本
client.Search<SearchItem>(x => x
.Sort(so => so
.Descending("_score")
)
.Size(itemsPerPage)
.Query(q => q
.MultiMatch(m => m
.Fields(fs => fs
.Field(p => p.Field1)
.Field(p => p.Field2)
.Field(p => p.Field3)
.Field(p => p.Field4)
.Field(p => p.Field5)
.Field(p => p.Field6)
)
.Operator(Operator.And)
.Query(pattern)
) && +q
.Range(ra => ra
.Field(ff=>ff.SalePrice)
.GreaterThan(1000)
)
)
);
将一元 +
运算符附加到范围查询是布尔查询过滤器的 shorthand。查询 json 现在看起来像
{
"size": 20,
"sort": [
{
"_score": {
"order": "desc"
}
}
],
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "match query",
"operator": "and",
"fields": [
"field1",
"field2",
"field3",
"field4",
"field5",
"field6"
]
}
}
],
"filter": [
{
"range": {
"salePrice": {
"gt": 1000.0
}
}
}
]
}
}
}