python_note
  • Introduction
  • My Python
    • Anaconda
    • argparse
    • datetime
    • json
    • logging
    • numpy
    • open
    • openCC
    • pandas & csv
    • Socket & SocketServer
    • re
    • yaml
    • smtp
    • 物件操作
    • unittest
    • 線程
    • prettytable
    • IO
    • pycurl
    • sys
    • pickle
    • auto-python-to-exe
    • cython
    • nuitka
  • Crawler
    • Urllib & Requests
      • User-agent
      • Percent-Encoding
      • mail code
    • Selenium
    • TCP & UDP
    • 控制字符(control character)
  • Web Development
    • Flask
      • RESTful api
      • Template
      • blueprint
    • Django
      • 環境佈署(windows)
    • 檢查Port
    • Apache
    • 使用者行為
    • jQuery
    • 壓力測試
    • DataTable
    • Bootstrap
    • CSS
    • JavaScript
    • Chart.js
  • Deep Learning
    • Keras 設定
    • RNN
    • LSTM
  • Test
    • T-Test
  • 資料結構
    • Hash
    • 時間複雜度
  • NLP
    • N-gram
    • CKIP
    • 中文轉數字
    • CRF
    • Mutual Information
    • 模糊比對
  • Linebot
    • Heroku
    • 圖文選單
    • channel
  • Linux
    • 常用指令
    • shell script
    • sshfs
    • ssh
    • nodejs & npm
    • debug
  • GCP
    • app engine
    • ssh(gcp)
    • gsutil
    • brabrabra
    • Load Balancer
    • k8s
  • Database
    • mysql
    • elasticsearch
      • Query
      • Backup and Restore
      • elasticdump
      • es2csv
      • ELK
    • mongodb
      • install
      • authentication
      • pymongo
    • sql server
  • go
    • Swarm
  • Docker
    • Kitematic
    • Dockerfile
    • Swarm
  • Git
  • 其他
    • USB軟體保護
    • Windows效能監視器
  • Blockchain
Powered by GitBook
On this page
  • 常見名詞
  • Index建立、文章新增
  • mapping 映射
  • score 相關性
  • 查詢語法
  • match
  • match_phrase
  • term
  • terms
  • aggregations
  • 組合查詢
  • Range
  • 巢狀判斷式

Was this helpful?

  1. Database
  2. elasticsearch

Query

常見名詞

Node

具有ES功能的Server

Cluster

很多Node

Index

DB

Type

Table

Field

欄位(Columns)

Document

一筆資料(Row)

Shard

組成Index的部分

Index建立、文章新增

#連線server
es=Elasticsearch([{'host': 'localhost', 'port': 9200}])

#如果需要身分驗證
es=Elasticsearch(['ip&port'],http_auth=('username','password'))


#建立index
#若連線狀態是ignore則回傳錯誤訊息
#建立時可在body加上表格欄位設定(table_format),也就是mapping設定
#其中article即doc_type
table_format={
        "mappings":{
            "article":{
                "properties":{
                    "title":{
                        "type":"string",
                        "index":"not_analyzed"
                    },
                    "content" : {
                        "type":"string",
                        "index":"not_analyzed"
                    }
                }
            }
        }
    }
res=es.indices.create(index=index_name, ignore=[400,409,404], body=table_format)


#判斷index是否存在,會回傳boolean
res=es.indices.exists(index='index_name')


#新增資料至doc_type中
#id可以是任意字串
# index :若已存在則覆蓋
# create:若已存在則error
res=es.create(index='index_name', doc_type='article', id='aid', body=data)
res=es.index(index='index_name', doc_type='article', id='aid', body=data)

mapping 映射

資料型態

text vs keyword

text:結構較複雜的資料,如:文章、句子。適用於full-text search

keyword:結構較簡單的資料,如:email、品牌型號。適用完全比對的需求

score 相關性

主要根據TF/IDF!? (gitbook 7-3, 8-3)

影響因素:字串與字串之間的距離;字串出現的次數;配對成功的句子長度

查詢語法

match

回傳結果包含_score排名(怪怪ㄉ),英文大小寫有影響

若搜尋字串無法完全比對,會根據部分搜尋字串進行比對,也就是搜尋字串會被拆解

#gitbook 7-4
query={
    'query': {
      'match': {
        'about':'Sony'
       }
    }
}
res=es.search(index='indexname',doc_type='typename',body=query) #搜尋about欄位包含sony的結果

match_phrase

結果同樣包含_score排名,會完全比對搜尋字串,也就是欄位中一定要出現"很好用"

如資料以陣列儲存則需要與陣列元素完全相同

#gitbook 7-4
qs={
    "query" : {
        "match_phrase" : {
            "about" : "很好用"
        }
    }
}

term

完全比對日期、數值與字串,比對字串記得將mapping中的字串欄位改為"index":"not_analyzed"

#gitbook 7-4
qs={
    "query" : {
        "term" : {
            "interests" : "workout"
        }
    }
}

terms

可使用多個比對對條件,符合其中一個即可

#gitbook 7-4
#interest包含workout'或'jogging
qs={
    "query" : {
        "terms" : {
            "interests" : ["workout","jogging"]
        }
    }
}

aggregations

#將interests的內容進行聚合
qs={
  "aggs": {
    "search_all_interests": { #query名稱
      "terms": { "field": "interests" }
    }
  }
}

#聚合first_name叫做Pan的員工的所有interest
qs={
  "query": {
    "match": {
      "first_name": "Pan"
    }
  },
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interests"
      }
    }
  }
}

組合查詢

must:and

must_not:not

should:or

#gitbook 12-2
#interests中不包含sony的結果
qs={
  "query" : {
      "bool":{
        "must_not":{
           "term":{
             "interests":"sony"
           }
         }
      }
   }
}


#與aggs結合,聚合interests中非sony的所有結果
qs={
  "query" : {
    "bool":{
      "must_not":{
        "term":{
          "interests":"sony"
        }
      }
    }
  },
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interests"
      }
    }
  }
}

Range

gt : 大於

gte:大於等於

lt:小於

lte:小於等於

#gitbook 12-5
#不用加filter
#price介於2000~5000
{
    "range" : {
        "price" : { #欄位名稱
            "gte" : 5000, #大於等於
            "lt" : 20000  #小於等於
        }
    }
}

巢狀判斷式

判斷是否連線成功

if not es.ping():
    raise ValueError("Connection failed")
PreviouselasticsearchNextBackup and Restore

Last updated 5 years ago

Was this helpful?

api doc:

中文GITBOOK:

https://www.elastic.co/blog/lost-in-translation-boolean-operations-and-filters-in-the-bool-query
http://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch
https://es.xiaoleilu.com/index.html