弹性搜索从搜索结果中排除嵌套元素,通过 id 获取元素
Elastic search exclude a nested element from search results, get element by id
我有嵌套对象的项目:
{
"name": "The Amazon rainforest",
"id": "610d33da26c25b00191ebcbe",
"tags": [
{
"name": "Brazil",
"verified": 1
},
{
"name": "new_tag",
"verified": 0,
}
],
}
在搜索结果中应省略未验证的标签:
按 id 搜索的输出:610d33da26c25b00191ebcbe
{
"name": "The Amazon rainforest",
"id": "610d33da26c25b00191ebcbe",
"tags": [
{
"name": "Brazil",
"verified": 1
}
],
}
Node.js 版本的答案:
const { body } = await elasticWrapper.client.search({
index: ElasticIndexs.Products,
filter_path:
'hits.hits._source*, hits.hits.inner_hits.tags.hits.hits._source*',
body: {
_source: {
excludes: ['tags'],
},
query: {
bool: {
must: [
{
match: {
id: req.params.id,
},
},
],
should: [
{
nested: {
path: 'tags',
query: {
term: {
'tags.verified': 1,
},
},
inner_hits: {},
},
},
],
},
},
},
});
我有嵌套对象的项目:
{
"name": "The Amazon rainforest",
"id": "610d33da26c25b00191ebcbe",
"tags": [
{
"name": "Brazil",
"verified": 1
},
{
"name": "new_tag",
"verified": 0,
}
],
}
在搜索结果中应省略未验证的标签:
按 id 搜索的输出:610d33da26c25b00191ebcbe
{
"name": "The Amazon rainforest",
"id": "610d33da26c25b00191ebcbe",
"tags": [
{
"name": "Brazil",
"verified": 1
}
],
}
Node.js 版本的答案:
const { body } = await elasticWrapper.client.search({
index: ElasticIndexs.Products,
filter_path:
'hits.hits._source*, hits.hits.inner_hits.tags.hits.hits._source*',
body: {
_source: {
excludes: ['tags'],
},
query: {
bool: {
must: [
{
match: {
id: req.params.id,
},
},
],
should: [
{
nested: {
path: 'tags',
query: {
term: {
'tags.verified': 1,
},
},
inner_hits: {},
},
},
],
},
},
},
});