使用多匹配和通配符查询
Query with multimatch and wildcard
我正在尝试对多个字段执行查询,但也在 MobileNumber 中使用通配符,基本上,如果手机号码是 3530831112233,如果我按 831122 搜索,我想 return 这条记录.这是我到目前为止所做的。
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 and combine it with match query on CustomerName
field in a bool query.
这样的东西
这是显示用法的简单应用程序:
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 or phone number analyzer plugin。
希望对您有所帮助。
我正在尝试对多个字段执行查询,但也在 MobileNumber 中使用通配符,基本上,如果手机号码是 3530831112233,如果我按 831122 搜索,我想 return 这条记录.这是我到目前为止所做的。
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 and combine it with match query on CustomerName
field in a bool query.
这是显示用法的简单应用程序:
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 or phone number analyzer plugin。
希望对您有所帮助。