iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 15
0
Elastic Stack on Cloud

Python&Elasticsearch 入門系列 第 15

IT鐵人第15天 Elasticsearch 使用python查詢資料 Nested

今天文章的內容是如何搜尋Nested與object
話不多說直接開始

Object:

假設文檔如下面

{
  "student" : {
    "sid" : "s1090104",
    "name" : "小新",
    "age" : 18,
    "class" : "資工一1"
  }
}

下面是可以搜尋出這個文檔的query

{
  "query": {
    "bool": {
      "must": {
        "term": {
          "student.name": "小新" #object在ES query內的使用方式
        }
      }
    }
  }
}

Nested

假設文檔如下面

{
  "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": "小新"
            }
          }
        }
      }
    }
  }
}

今天的教學就到這邊,下篇講的是搜尋時的計分


上一篇
IT鐵人第14天 Elasticsearch 使用python查詢資料 parent/child
下一篇
IT鐵人第16天 Elasticseach 使用python查詢資料 文檔分數 score
系列文
Python&Elasticsearch 入門30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言