elasticsearch 跨不同字段按空格分隔的查询字符串搜索
下面是我的索引实体示例集。假设它代表客户的一组产品订单elasticsearch 跨不同字段按空格分隔的查询字符串搜索,elasticsearch,nest,elasticsearch,Nest,下面是我的索引实体示例集。假设它代表客户的一组产品订单 [ { "Id": 1, "Product": "Peugeot", "Customer": "John Woo" }, { "Id": 2, "Product": "Peugeot", "Customer": "John Carpenter" }, { "Id": 3, "
[
{
"Id": 1,
"Product": "Peugeot",
"Customer": "John Woo"
},
{
"Id": 2,
"Product": "Peugeot",
"Customer": "John Carpenter"
},
{
"Id": 3,
"Product": "Peugeot",
"Customer": "Bruce Lee"
},
]
请注意,它们都是“标致”,但客户不同:有两个“约翰”和一个“李小龙”
以下是我的查询字符串:
“标致约翰
”
我想从Elastic公司得到的是退回客户订购的所有“标致”产品,并在其名称中注明“John”。这意味着这个结果:
[
{
"Id": 1,
"Product": "Peugeot",
"Customer": "John Woo"
},
{
"Id": 2,
"Product": "Peugeot",
"Customer": "John Carpenter"
}
]
请注意,应该跳过Id=3的实体,因为客户是“Bruce Lee”。因此,查询字符串中的每个单词都应该应用于整个对象,而不是按顺序应用于每个字段
因此,我使用NEST C#库运行我的“multi_match”查询:
elasticClient.SearchAsync(描述符=>
描述符
.索引(“myIndex”)
.尺寸(20)
.Query(q=>q
.MultiMatch(m=>m
.Fields(f=>f
.菲尔德(
p=>p.乘积,
p=>p.客户
)
)
.操作员(操作员和)
.Query(“标致约翰”)
)
)
)
它什么也不返回。因为它将查询拆分为两个词:“标致”和“约翰”,并使用and运算符将它们应用于每个字段。由于没有值同时包含“标致”和“约翰”的字段,因此结果为空
当我将运算符更改为或行为仍然不正确时:它返回所有3个对象,因为它们都匹配“标致”单词
那么,如何实现“对象范围的分隔查询独占搜索”行为呢
谢谢。看起来很适合这种类型的
看起来很适合这种类型的
谢谢你,罗布。很好用,正是我需要的。谢谢Rob。工作完美,正是我所需要的。
elasticClient.SearchAsync<OrderModel>(descriptor =>
descriptor
.Index("myIndex")
.Size(20)
.Query(q => q
.MultiMatch(m => m
.Fields(f => f
.Fields(
p => p.Product,
p => p.Customer
)
)
.Operator(Operator.And)
.Query("Peugeot John")
)
)
)
var results = await client.SearchAsync<Document>(s => s
.Query(q => q.MultiMatch(m => m
.Fields(f => f.Fields("product", "customer"))
.Type(TextQueryType.CrossFields)
.Operator(Operator.And)
.Query(query))));
Results for query "Peugeot John":
Peugeot - John Woo
Peugeot - John Carpenter