[英]How to search nested objects with Elasticsearch
好的,到目前為止我還沒有弄清楚這一點。 希望有人能提供一些見解。
鑒於以下文件,我將如何搜索視頻標題中帶有“測試”的視頻的所有文件? 我正在使用 HTTP API。 (基本上,您如何使用彈性搜索搜索嵌套對象?我知道那里必須有文檔,但我真的找不到任何文檔。)
[{
id:4635,
description:"This is a test description",
author:"John",
author_id:51421,
video: {
title:"This is a test title for a video",
description:"This is my video description",
url:"/url_of_video"
}
},
{
id:4636,
description:"This is a test description 2",
author:"John",
author_id:51421,
video: {
title:"This is an example title for a video",
description:"This is my video description2",
url:"/url_of_video2"
}
},
{
id:4637,
description:"This is a test description3",
author:"John",
author_id:51421,
video: {
title:"This is a test title for a video3",
description:"This is my video description3",
url:"/url_of_video3"
}
}]
您不一定需要嵌套視頻; 您可以將其映射為普通字段。 這意味着它將存儲
'video:title': "This is a test title for a video3",
'video:description':"This is my video description3",
'video:url':"/url_of_video3"
您可以搜索video.title:'test'
。
據我所知,當您有多個嵌套項並且只想對嵌套項進行查詢時,嵌套字段很有用。 例如,有這個數據
[{
id:4635,
description:"This is a test description",
author:"John",
author_id:51421,
video: [
{
title:"This is a test title for a video",
description:"This is my video description",
url:"/url_of_video"
},
{
title:"This is an example title for a video",
description:"This is my video description2",
url:"/url_of_video2"
}
]
},
{
id:4637,
description:"This is a test description3",
author:"John",
author_id:51421,
video: [
{
title:"This is a test title for a video3",
description:"This is my video description3",
url:"/url_of_video3"
}
]
}]
如果您要搜索video.title: 'test' and video.description: 'description2'
,並且視頻沒有嵌套,它會給您一個假的結果(因為test
在第一個視頻中,而description2
在第二個視頻中,但在您擁有的所有視頻字段)。
在這種情況下,如果您將視頻映射為嵌套,它將記住每個視頻作為一個單獨的實體,並會搜索符合這些條件的單個視頻,因此對於video.title: 'test' and video.description: 'description2'
它將什么都不返回,對於video.title: 'example' and video.description: 'description2'
它將返回一個結果。
好的,我終於找到了這些頁面(應該事先花更多時間看文檔),看來我們將保存視頻的屬性設置為 type:nested,然后使用嵌套查詢。
http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html
http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-filter.html
希望這可以幫助某人在路上。
如果你想把它放在 Rest API URL 格式
/_search?pretty&q=video.title:*test*
如果嵌套對象的名稱是唯一的,則可以使用.keyword
后綴:
{
'query': {
'term': {
'title.keyword': "This is a test title for a video"
}
}
}
這應該與您的第一個示例條目匹配。 請注意, video
對象名稱未在任何地方指定; 這匹配具有title
子對象的所有對象。
架構是:
private schema = {
id: {
type: 'integer',
},
name: {
type: 'text',
},
tags: {
type: 'nested',
properties: {
id: {
type: 'integer',
},
name: {
type: 'keyword',
normalizer: 'useLowercase',
},
},
},
}
文檔結構是
id: 38938
name: "summer fruits"
tags:[
{
id : 73
name: "Grapes"
},
{
id : 74
name: "Pineapple"
}
]
搜索查詢:
const { tags } = req.body;
const { body } = await elasticWrapper.client.search({
index: ElasticIndexs.Fruits,
pretty: true,
filter_path: 'hits.hits._source*',
body: {
query: {
bool: {
must: tags.map((ele: { name: string }) => {
return {
nested: {
path: 'tags',
query: {
match: {
'tags.name': ele.name,
},
},
},
};
}),
},
},
},
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.