由于 Elasticsearch 拥有精彩的文档,我无法找出搜索术语的正确语法,这是我的代码:
let checkuser = await client.search({
index: "users",
type: "my_users",
body: {
query: {
term: {
email: req.body.email
}
}
}
});
我希望搜索一个具有“email”键值对和特定电子邮件的对象,但我希望它是我写的确切电子邮件,如果它的 a@mail.com ab@mail.com 应该不匹配,我知道我需要使用术语,但是当我这样写时它不起作用,我的语法有什么问题吗?
PS这是我的索引映射:
"users" : {
"mappings" : {
"jobix_users" : {
"properties" : {
"confirmed" : {
"type" : "boolean"
},
"email" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"firstName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"jobNotification" : {
"type" : "boolean"
},
"jobTitle" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"lastName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"password" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"userName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
最佳答案
您使用默认映射,这意味着您使用 standard tokenizer当您索引文档时。
如何在映射电子邮件字段中看到有两种解释:
在文本中,标准标记生成器可以工作并将标记保存到您的索引中。这意味着您可以找到术语 alex 和术语 mail.com。如果您想查找整个电子邮件,那么您的查询应如下所示:
{
"query": {
"term": {
"email.keyword": req.body.email
}
}
}
但是elasticsearch有特殊的uax_url_email tokenizer对于网址和邮件。我想推荐在电子邮件字段中使用此分词器。
关于node.js - 在elasticsearch js中使用术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58435438/