提问者:小点点

在任何列中搜索单词的任何部分


我在搜full_name邮箱或者电话

举个例子

如果我开始输入“16”,它应该显示电话号码开始或包含“16”的所有用户。全名和电子邮件相同

我的ES配置是:

{
   "users" : {
      "mappings" : {
         "user" : {
            "properties" : {
               "full_name" : {
                  "analyzer" : "trigrams",
                  "include_in_all" : true,
                  "type" : "string"
               },
               "phone" : {
                  "type" : "string",
                  "analyzer" : "trigrams",
                  "include_in_all" : true
               },
               "email" : {
                  "analyzer" : "trigrams",
                  "include_in_all" : true,
                  "type" : "string"
               }
            },
            "dynamic" : "false"
         }
      },
      "settings" : {
         "index" : {
            "creation_date" : "1472720529392",
            "number_of_shards" : "5",
            "version" : {
               "created" : "2030599"
            },
            "uuid" : "p9nOhiJ3TLafe6WzwXC5Tg",
            "analysis" : {
               "analyzer" : {
                  "trigrams" : {
                     "filter" : [
                        "lowercase"
                     ],
                     "type" : "custom",
                     "tokenizer" : "my_ngram_tokenizer"
                  }
               },
               "tokenizer" : {
                  "my_ngram_tokenizer" : {
                     "type" : "nGram",
                     "max_gram" : "12",
                     "min_gram" : "2"
                  }
               }
            },
            "number_of_replicas" : "1"
         }
      },
      "aliases" : {},
      "warmers" : {}
   }
}

按名称的一部分搜索名称“罗伯特”

curl-XGET'localhost:9200/users/_search?pretty'-d'{"query":{"match": { "_all":"Rob" } } }'

不提供预期结果,仅使用全名。


共1个答案

匿名用户

由于分析器设置在字段full_namephoneemail上,因此不应使用_all字段,而应在multi_match查询中枚举这些字段,如下所示:

curl -XGET 'localhost:9200/users/_search?pretty' -d'{
  "query": {
    "multi_match": {
      "query": "this is a test",
      "fields": [
        "full_name",
        "phone",
        "email"
      ]
    }
  }
}'