我想从我的ElasticSearch索引中获取数据。
所以我有以下索引:
使用Node,如果我这样做:
const hits = await elasticClient.search({
index: esIndex,
size: 999,
sort: 'ts:desc',
body: {
query: {
bool: {
must: [
{
term: {
userId: '56',
},
}
],
},
},
},
});
它将返回:
[
{
"_index": "events_rc",
"_type": "_doc",
"_id": "tf-szm0B_tB6kax4xmnr",
"_score": null,
"_source": {
"userId": "56",
"eventName": "synchronizationStart",
"ts": 1571130486383
}
},
{
"_index": "events_rc",
"_type": "_doc",
"_id": "tP-szm0B_tB6kax4xmnr",
"_score": null,
"_source": {
"userId": "56",
"eventName": "showSynchronizationModal",
"ts": 1571130447209
}
}
]
但是如果我执行以下查询:
const hits = await elasticClient.search({
index: esIndex,
size: 999,
sort: 'ts:desc',
body: {
query: {
bool: {
must: [
{
term: {
eventName: 'synchronizationStart',
},
}
],
},
},
},
});
它将返回一个空数组...
我想知道为什么它可以找到与userId
而不是eventName
的匹配项?
两个字段的映射似乎相同
"eventName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
"userId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
thx
答案 0 :(得分:1)
您需要在eventName.keyword
字段上进行查询,它将起作用:
const hits = await elasticClient.search({
index: esIndex,
size: 999,
sort: 'ts:desc',
body: {
query: {
bool: {
must: [
{
term: {
'eventName.keyword': 'synchronizationStart',
},
}
],
},
},
},
});
答案 1 :(得分:1)
事件名称的类型为文本。您应该使用匹配而不是术语。
避免对文本字段使用术语查询。
取自Here
如果您将使用 eventName.keyword ,则必须在字段中添加自定义规范化器,否则必须搜索 exact 字词。
更改
const hits = await elasticClient.search({
index: esIndex,
size: 999,
sort: 'ts:desc',
body: {
query: {
bool: {
must: [
{
term: {
eventName: 'synchronizationStart',
},
}
],
},
},
},
});
收件人:
const hits = await elasticClient.search({
index: esIndex,
size: 999,
sort: 'ts:desc',
body: {
query: {
bool: {
must: [
{
match: {
eventName: 'synchronizationStart',
},
}
],
},
},
},
});