【发布时间】:2016-07-07 13:13:08
【问题描述】:
我需要通过电子邮件搜索联系人。根据ES documentation,实现这一目标的最佳方法是使用uax_url_email 标记器。这是我的索引设置:
settings: {
index: {
creation_date: "1467895098804",
analysis: {
analyzer: {
email: {
type: "custom",
tokenizer: "uax_url_email"
}
}
},
number_of_shards: "5",
number_of_replicas: "1",
uuid: "wL0P6OIaQqqYpFDvIHArTw",
version: {
created: "2030399"
}
}
}
和映射:
contact: {
dynamic: "false",
properties: {
contact_status: {
type: "string"
},
created_at: {
type: "date",
format: "strict_date_optional_time||epoch_millis"
},
email: {
type: "string"
},
id: {
type: "long"
},
mailing_ids: {
type: "long"
},
subscription_status: {
type: "string"
},
type_ids: {
type: "long"
},
updated_at: {
type: "date",
format: "strict_date_optional_time||epoch_millis"
},
user_id: {
type: "long"
}
}
}
创建索引后,我插入了两个文档:
curl -X PUT 'localhost:9200/contacts/contact/1' -d '{"contact_status": "confirmed", "email": "example@gmail.com", "id": "1", "user_id": "1", "subscription_status": "on"}'
和
curl -X PUT 'localhost:9200/contacts/contact/2' -d '{"contact_status": "confirmed", "email": "example@yahoo.com", "id": "2", "user_id": "2", "subscription_status": "on"}'
然后我尝试以不同的方式通过电子邮件搜索联系人:
curl -X POST 'localhost:9200/contacts/_search?pretty' -d '{"query": {"bool": {"must": [ {"match": {"_all": { "query": "example@google.com", "analyzer": "email" } } } ] } } }'
我希望得到 1 个 id=1 的结果,但结果为空:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
我测试的下一个搜索查询是:
curl -X POST 'localhost:9200/contacts/_search?pretty' -d '{"query": {"bool": {"must": [ {"match": {"_all": { "query": "example@google", "analyzer": "email" } } } ] } } }'
返回 2 个结果:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.016878016,
"hits" : [ {
"_index" : "contacts",
"_type" : "contact",
"_id" : "2",
"_score" : 0.016878016,
"_source" : {
"contact_status" : "confirmed",
"email" : "example@yahoo.com",
"id" : "2",
"user_id" : "2",
"subscription_status" : "on"
}
}, {
"_index" : "contacts",
"_type" : "contact",
"_id" : "1",
"_score" : 0.016878016,
"_source" : {
"contact_status" : "confirmed",
"email" : "example@gmail.com",
"id" : "1",
"user_id" : "1",
"subscription_status" : "on"
}
} ]
}
}
但如您所知,我希望在搜索结果中获得 1 个文档。我做错了什么?
【问题讨论】:
-
如果
email只包含电子邮件地址,为什么不将该字段设为"index":"not_analyzed",然后使用term过滤器来搜索电子邮件地址? -
因为我还需要按user_id、id等字段进行搜索。此外,我想按电子邮件的一部分进行搜索,如下所示:在输入中输入
example并获取包含“示例”的电子邮件列表,在我的情况下 - 两个文档。或者,如果我输入gmail.com=> 获取 id 为 1 的文档 -
我建议采用这种方法:stackoverflow.com/questions/30115867/… 如果您有任何困难或与此不同的用例,请告诉我。
标签: elasticsearch