gpt4 book ai didi

c# - 使用多重匹配和通配符查询

转载 作者:行者123 更新时间:2023-12-02 23:14:24 24 4
gpt4 key购买 nike

我正在尝试对多个字段执行查询,但也尝试在MobileNumber中使用通配符,基本上,如果移动电话例如是3530831112233(如果我按831122搜索),我想返回此记录。到目前为止,这是我所做的。

var response = await this.client.SearchAsync<ElasticCustomer>(searchDescriptor => searchDescriptor
.AllTypes()
.Query(q => q
.MultiMatch(m => m
.Fields(f => f
.Field(u => u.CustomerName)
.Field(u => u.MobileNumber))
.Query(query)))
.Size(pageSize)
.Sort(q => q.Descending(u => u.CustomerLastUpdated)));

最佳答案

如果要执行通配符查询,则需要使用wildcard query之类的东西,并将其与bool queryCustomerName字段上的匹配查询结合起来。

这是显示用法的简单应用程序:

class Program
{
public class Document
{
public int Id { get; set; }
public DateTime Timestamp { get; set; }
public string CustomerName { get; set; }
public string MobileNumber { get; set; }
}

static async Task Main(string[] args)
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings = new ConnectionSettings(pool);
connectionSettings.DefaultIndex("documents");

var client = new ElasticClient(connectionSettings);

await client.Indices.DeleteAsync("documents");
await client.Indices.CreateAsync("documents");

var response = await client.IndexAsync(
new Document
{
Id = 1,
Timestamp = new DateTime(2010, 01, 01, 10, 0, 0),
MobileNumber = "3530831112233",
CustomerName = "Robert"
}, descriptor => descriptor);

await client.Indices.RefreshAsync();

string query = "8311122";
var result = await client.SearchAsync<Document>(s => s
.Query(q => q.Bool(b => b
.Should(
sh => sh.Match(m => m.Field(f => f.CustomerName).Query(query)),
sh => sh.Wildcard(w => w.Field(f => f.MobileNumber.Suffix("keyword")).Value($"*{query}*"))))));

foreach (var document in result.Documents)
{
Console.WriteLine(document.Id);
}
}
}

输出:
1

但是,我建议您尽可能避免使用通配符查询,这可能会导致查询性能下降。

作为通配符替换,您可以查看 ngram tokenizerphone number analyzer plugin

希望能有所帮助。

关于c# - 使用多重匹配和通配符查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56984878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com