今天文章的內容是如何搜尋Nested與object
話不多說直接開始
假設文檔如下面
{
"student" : {
"sid" : "s1090104",
"name" : "小新",
"age" : 18,
"class" : "資工一1"
}
}
下面是可以搜尋出這個文檔的query
{
"query": {
"bool": {
"must": {
"term": {
"student.name": "小新" #object在ES query內的使用方式
}
}
}
}
}
假設文檔如下面
{
"student" : [
{
"sid" : "s1090101",
"name" : "王小明",
"age" : 18,
"class" : "資工一1"
},
{
"sid" : "s1090102",
"name" : "許小美",
"age" : 20,
"class" : "資工二2"
},
{
"sid" : "s1090103",
"name" : "風間",
"age" : 18,
"class" : "資工一1"
},
{
"sid" : "s1090104",
"name" : "小新",
"age" : 18,
"class" : "資工一1"
}
]
}
下面是可以搜尋出這個文檔的query
{
"query": {
"nested": {
"path": "student", # Nested的key
"query": { # 底下object的搜索條件
"bool": {
"must": {
"match": {
"student.name": "小新"
}
}
}
}
}
}
}
今天的教學就到這邊,下篇講的是搜尋時的計分