gpt4 book ai didi

elasticsearch - 查询特定字段时输出为空

转载 作者:行者123 更新时间:2023-12-03 00:47:14 24 4
gpt4 key购买 nike

我想从我的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
}
}

谢谢

最佳答案

eventName 的类型为文本。您应该使用匹配而不是术语。

Avoid using the term query for text fields.



取自 Here

如果要使用 eventName.keyword ,则必须在字段中添加自定义规范化器,否则必须搜索 确切术语。

更改
    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',
},
}
],
},
},
},

});

关于elasticsearch - 查询特定字段时输出为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58733355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com