简体   繁体   English

使用多匹配和通配符查询

[英]Query with multimatch and wildcard

I'm trying to execute a query on multi fields but also using a wildcard in the MobileNumber, basically, if a mobile number is for example 3530831112233 if I search by 831122 I want to return this record.我正在尝试对多字段执行查询,但也在 MobileNumber 中使用通配符,基本上,如果手机号码是 3530831112233,如果我按 831122 搜索,我想返回此记录。 This is what I have done so far.这是我迄今为止所做的。

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

If you want to perform wildcard query, you will need to use something like wildcard query and combine it with match query on CustomerName field in a bool query .如果要执行通配符查询,则需要使用通配符查询之类的内容,并将其与布尔查询中CustomerName字段的匹配查询相结合。

Here is the simple app showing usage:这是显示用法的简单应用程序:

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);
        }
    }
}

Output:输出:

1

However, I would suggest avoiding the wildcard query whenever you can at it maybe result in query performance degradation.但是,我建议尽可能避免使用通配符查询,这可能会导致查询性能下降。

As a wildcard substitution, you can have a look at ngram tokenizer or phone number analyzer plugin .作为通配符替换,您可以查看ngram tokenizer电话号码分析器插件

Hope that helps.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM