在不同字段中按空格分隔的查询字符串搜索

Search by whitespace-delimited query string across differend fields

这是我的示例索引实体集。假设它代表客户的一组产品订单。

[
    {
        "Id": 1,
        "Product": "Peugeot",
        "Customer": "John Woo"
    },
    {
        "Id": 2,
        "Product": "Peugeot",
        "Customer": "John Carpenter"
    },
    {
        "Id": 3,
        "Product": "Peugeot",
        "Customer": "Bruce Lee"
    },
]

请注意,他们都是 'Peugeot',但客户不同:有两个 'Johns' 和一个 'Bruce Lee'。

这是我的查询字符串: 'Peugeot John'

我想从 Elastic 得到的是 return 客户订购的所有 'Peugeot' 产品,这些产品的名称中包含 'John'。这意味着这个结果:

[
    {
        "Id": 1,
        "Product": "Peugeot",
        "Customer": "John Woo"
    },
    {
        "Id": 2,
        "Product": "Peugeot",
        "Customer": "John Carpenter"
    }
]

请注意,应跳过 ID=3 的实体,因为客户是 'Bruce Lee'。所以查询字符串中的每个词应该应用于整个对象,而不是按顺序应用于每个字段。

所以我 运行 我的 "multi_match" 使用 NEST C# 库查询:

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")
            )
        )
)

它return没什么。因为它将查询拆分为两个词:'Peugeot' 和 'John' 并使用 AND 运算符将它们应用于每个字段。由于没有值同时包含 'Peugeot' 和 'John' 的字段,因此结果为空。

当我将运算符更改为 OR 时,行为仍然不正确:它 return 是所有 3 个对象,因为它们都匹配 'Peugeot' 个词。

那么我怎样才能实现那种 'object-wide-delimited-query-exclusive-search' 行为呢?

谢谢。

看起来很适合 cross_field type of multi-match query

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