owned this note
                
                
                     
                     owned this note
                
                
                     
                    
                
                
                     
                    
                
                
                     
                    
                        
                            
                            Published
                        
                        
                            
                                
                                Linked with GitHub
                            
                            
                                
                                
                            
                        
                     
                
            
            
                
                    
                    
                
                
                    
                
                
                
                    
                        
                    
                    
                    
                
                
                
                    
                
            
            
         
        
        ###### tags: `Web`
# Elasticsearch(BackendSG)
## What Is?
分散式搜尋分析系統
到2019年4月開始紅起來(機器學習、資料處理等原因使用到等等因素)
NoSQL
~ 但是他主要是透過HTTP以JSON方式來query
也可用curl
### 基於Apache Lucene的基礎下建立
Apache Lucene
~ 全文檢索和搜尋的開放原始碼程式庫,也是一個[程式](https://lucene.apache.org/)
Doug Cutting
~ Hadoop之父 全文索引及檢索專家
## Why Use?
跟MongoDB的差異?
兩者都支援
1. 文件取向儲存
2. 無schema
3. 分散式儲存機制
4. 高可用性(是指針對軟硬體所可能產生的錯誤發生時,仍能維持正常運作的方式,如自動備份,etc.)
5. DB replication(支援多台機器同步共用)
用誰都可以,只是取向不同
|服務|取向|Indexing|Index|Store|Restful
-|-|-|-|-|-
MongoDB|主要是用以提供大量<br>文件式資料的NoSQL DBMS|一般DB用的B+ Tree|自己定義某些欄位|BSON|No
ElasticSearch|除了文件是存儲以外,目標是希望達到即時的資料萃取、分析等整合的功能|Apache Lucene|相當於所有的field都index到|JSON|==Yes==
## Getting Started
### :zero: How it works
ES繼承Lucene定義的檢索方式

假設我們想搜尋 `row boat`




*Analysis* :將paragraph拆解成terms

snowball:支援24種語言的stem分析

`CharFilter`:把資料過濾出是文字的內容
`Tokenizer`: 將字句拆解成Stem
`TokenFilter`: 過濾stem找出文字斷句
term:整理成剛剛那個array樣子的hashing
這幾個人都有很多種,像是tokenizer還有一些是專門處理synonyms
像是allan的小名是al
搜尋時allan跟al會放在一起處理。
### :one: Get ElasticSearch, Kibana
1. [下載ES](https://www.elastic.co/cn/downloads/elasticsearch)(brew/exe/...c)
2. [下載Kibana](https://www.elastic.co/cn/downloads/kibana)(brew/exec/...)
3. 用terminal去執行<br>:open_file_folder: elascicsearch-xxx/bin/elasticsearch.exe
4. 去`http://localhost:9200`看到這個就成功啟動es了<br>
5. 用terminal去執行<br>:open_file_folder: kibana-xxx/bin/kibana.exe 
6. `localhost:5601`就會看到kibana<br>
7. 7. <br>
8. <br>
9. 
10. 
11. 新增資料結果
[Kibana Index Creation](https://www.elastic.co/guide/en/kibana/current/tutorial-define-index.html)
### :two:(Optional) Have a server/backend (node express.js, django, ROR, Flask, ...)
也可先看懂下面的api功能
直接戳他們的api
[Index API](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html)
### :three: Usage / Demo
#### Query(Using Query DSL syntax)
[DSL query](https://www.tutorialspoint.com/elasticsearch/elasticsearch_query_dsl.htm)
```
//支援callback、promise兩種寫法
```
```javascript
//create index
 client.indices.create({
    index: 'articles'
  }, function(err,resp,status){})
```
```javascript
//add document
  client.index(
  {
    index: 'articles',
    type: 'normal', // type will be deprecated
    body:{
      "mykey": "myvalue"
    }
  },function(err, resp, status){
    
  })
```
```javascript
//query
client.search({
    index: 'articles',
    body:{
    query:{
      match_all:{}
    }
  }
},function(err,resp,stsc){
  
})
client.search({
    index: 'articles',
    body:{
    query:{
      multi_match:{
        query: '亂幹阿翔',
        fields: ['title', 'content']
      }
    }
  }
},function(err,resp,stsc){
  console.log(JSON.stringify(resp.hits.hits))
})
```
```javascript
//update by query
client.search({
    index: 'articles',
    body:{
    query:{
      multi_match:{
        query: '亂幹阿翔',
        fields: ['title', 'content']
      }
    }
  }
},function(err,resp,stsc){
})
```
```javascript
//delete index
  client.indices.delete({
    index: 'articles'
  }, function (err, resp, status) {
  }
```
## References
[MySQL Replication](https://blog.toright.com/posts/5062/mysql-replication-%E4%B8%BB%E5%BE%9E%E5%BC%8F%E6%9E%B6%E6%A7%8B%E8%A8%AD%E5%AE%9A%E6%95%99%E5%AD%B8.html)
[Comparison with MongoDB(Raw)](https://db-engines.com/en/system/Elasticsearch%3BMongoDB)
[Comparison with MongoDB](https://www.quora.com/What-are-the-main-differences-between-ElasticSearch-and-NoSQL-DBs-like-MongoDB-Do-you-think-these-two-technologies-products-would-have-more-similarities-than-differences-in-the-near-future)
[Official Node.js Client Document](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html)
[Lucene](https://zhuanlan.zhihu.com/p/35469104)
[MongoDB Distributed(Using Sharded)](https://docs.mongodb.com/manual/core/distributed-queries/)
<hr>
[Getting Started](https://www.compose.com/articles/getting-started-with-elasticsearch-and-node/)
[ES7.x Deprecation of `type`](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/removal-of-types.html)
## 熱門列表
Article: PostgresQL
id: integer
user_id: integer
title: string
/article/like/:id
Redis: 
key/values article:[1,2,3,4]
ElasticSearch(10.minutes)
index: `articles`
id: integer
likes_count:
comment_count:
{
  query: 
  {
    bool:{
    must: {
      time: {
        gte: Time.now - 10.minutes
      }
    },
    filter:{
       like_count: {
        gte: 50
      }
    }
    }
  }
}