我试图在多个字段上执行查询,但也要在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)));
答案 0 :(得分:1)
如果要执行通配符查询,则需要使用wildcard query之类的东西,并将其与bool query中CustomerName
字段的匹配查询结合起来。
这是显示用法的简单应用程序:
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 tokenizer或phone number analyzer plugin。
希望有帮助。