【发布时间】:2020-09-13 10:23:37
【问题描述】:
我正在尝试从 Lambda 工作人员查询 AWS ElasticSearch 域。
为此,我使用http-aws-es 和用于 Elastic Search 的主要 javascript 客户端。
我查询具有以下相关字段的文档:
-
ref字段 - 字符串 -
status字段 - 字符串枚举(REMOVED、BLOCKED、PUBLISHED、PENDING、VERIFIED) -
field字段 - 字符串数组 -
thematics字段 - 字符串数组
我想要实现的是:
- 过滤所有不是
PUBLISHED或VERIFIED或设置了ref字段的文档 - 返回与
keywwords参数(字符串数组)相对于field和thematics中的值的最佳匹配 - 排序以将具有
PUBLISHED状态的文档放在首位 - 将结果数限制为 20 个
我找到了more_like_this 运算符,并试了一下。我一步一步构建我的查询,至少实际版本不会返回错误,但不会返回任何文档。它仍然错过了上面的 ref 过滤器 + #3 和 #4。这是查询:
const client = new elasticsearch.Client({
host: ELASTICSEARCH_DOMAIN,
connectionClass: httpAwsEs,
amazonES: {
region: AWS_REGION,
credentials: new AWS.EnvironmentCredentials('AWS')
}
})
let keywords = event.arguments.keywords
let rst = await client.search({
body: {
'query': {
'bool': {
'filter': {
'bool': {
'must_not': [
{
'term': {
'status': 'REMOVED'
}
},
{
'term': {
'status': 'PENDING'
}
},
{
'term': {
'status': 'BLOCKED'
}
}
]
}
},
'must': {
'more_like_this': {
'fields': ['field', 'thematics'],
'like': keywords,
'min_term_freq': 1,
'max_query_terms': 2
},
'should': [
{
'term': {
'status': 'PUBLISHED'
}
}
]
}
}
}
}
})
console.log(rst)
return rst
我必须上传我的 lambda 代码来调试它,它使调试变得非常复杂。由于我以前从未进行过 ES 查询,因此我想至少获得一些关于如何进行此操作的提示,或者知道我是否误用了 ES 查询语法。
编辑:
根据要求,这是我的索引映射(JS类型):
- 城市文本(字符串)
- contact_email 文本(字符串)
- contact_entity 文本(字符串)
- contact_firstname 文本(字符串)
- contact_lastname 文本(字符串)
- 联系人文本(字符串列表)
- 国家文本(字符串)
- createdAt 日期(字符串)
- 描述文本(字符串)
- editKey 文本(字符串)
- 字段文本(字符串)
- id 文本(字符串)
- 名称文本(字符串)
- pubId 文本(字符串)
- 参考文本(字符串)
- 状态文本(字符串)
- 状态文本(字符串)
- 主题文本(字符串数组)
- 输入文本(字符串数组)
- updatedAt(字符串)
- 网址文本(字符串)
- verifyKey 文本(字符串)
- 区域文本(字符串数组)
取自 AWS 弹性搜索管理控制台(索引选项卡 > 映射)
【问题讨论】:
标签: javascript elasticsearch morelikethis