【问题标题】:Elasticsearch part of string multi_match bool字符串 multi_match bool 的 Elasticsearch 部分
【发布时间】:2019-12-20 19:38:10
【问题描述】:

我刚刚将 mu 客户数据库复制到 Elasticsearch 中以获得更快的结果。

在我的列表页面上,我有一个搜索字段,可以将任何字符串搜索到我的客户数据库中。搜索应该能够匹配电子邮件、名字、姓氏、身份证和电话的结果。

我创建了一个 nGram 过滤器来仅匹配部分查询

ES.client.indices.putSettings({
            index: `customer`,
            body: {
                analysis : {
                    index_analyzer: {
                        my_index_analyzer: {
                            type: "custom",
                            tokenizer: "standard",
                            filter: ["lowercase", "mynGram"]
                        }
                      },
                      search_analyzer: {
                        my_search_analyzer: {
                            type: "custom",
                            tokenizer: "standard",
                            filter: [ "standard", "lowercase", "mynGram"]
                        }
                      },
                      filter: {
                        mynGram: {
                            type: "nGram",
                            min_gram: 2,
                            max_gram: 30
                        }
                      }
                },
                max_ngram_diff: 30
            }
        })

我的查询是

{
   "index":"customer",
   "pageNum":1,
   "perPage":20,
   "query":{
      "bool":{
         "must":[
            {
               "bool":{
                  "should":[
                     {
                        "match":{
                           "organizationId":"org_1"
                        }
                     },
                     {
                        "match":{
                           "organizationId":"org_2"
                        }
                     }
                  ]
               }
            },
            {
               "multi_match":{
                  "fields":[
                     "id",
                     "email",
                     "firstName",
                     "lastName",
                     "phone"
                  ],
                  "query":"loc"
               }
            }
         ]
      }
   },
   "sort":{
      "createdAt":{
         "order":"desc"
      }
   }
}

但这不是我想要的。

例如,我的一封电子邮件是test@geo.loc,如果我搜索loc,我没有结果。

我用 4 封电子邮件进行了另一次测试 test@geo.loc test1@test.com test2@test.com test@test.com。寻找 test 它只返回 test@geo.loctest@test.com 应该返回所有结果

如果我正在寻找1111,其他测试电话号码之一是2211111111 我没有结果,但2211111111 有效

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    multi_match 代替 query_string

     {
           "index":"customer",
           "pageNum":1,
           "perPage":20,
           "query":{
              "bool":{
                 "must":[
                    {
                       "bool":{
                          "should":[
                             {
                                "match":{
                                   "organizationId":"org_1"
                                }
                             },
                             {
                                "match":{
                                   "organizationId":"org_2"
                                }
                             }
                          ]
                       }
                    },
                  {
                  "query_string": {
                    "query": "*1111*",
                    "fields":["id", "email", "firstName", "lastName", "phone"]
                  }
                }
    
           },
           "sort":{
              "createdAt":{
                 "order":"desc"
              }
           }
        }
    

    【讨论】:

    • 它有效,但我读到如果有很多文件,这是一个不好的做法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 2013-05-31
    • 2022-10-15
    相关资源
    最近更新 更多