ElasticSearch筆記

ElasticSearch

  • 基於lucene的搜索服務器(搜索引擎)
  • 分佈式多用戶
  • 基於Restful
  • java開發
  • ELK日誌分析系統
  • 是一個nosql
  • 關係數據搜索缺點

    • 無法打分
    • 無法分佈式
    • 無法解析搜索請求
    • 效率低
    • 分詞

安裝

概念

  • 集羣
  • 節點
  • 分片:一份數據分幾份
  • 副本:創建幾份數據
  • 倒排索引(inverted index)
  • TF-IDF

文檔/索引的CRUD操作

PUT lagou
{
    "setting":{
        "index":{
            "number_of_shards":5, # 切片數
            "number_of_replicas":1 # 副本數,可修改
        }
    }
}
# 獲取設置
GET lagou/_settings
GET _all/_settings
GET _settings
GET lagou1,lagou2/_settings

#更新配置
PUT lagou/_settings
{
    "number_of_replicas":2
}

#獲取索引
GET lagou
GET _all

#添加數據
POST lagou/job/1
{
    "key":"value",
    ...
}

#獲取數據
GET lagou/job/1 #指定id存儲
GET lagou/job/  #自動生成一個uuid當作id
GET lagou/job/1?_source=key1,key2

#修改數據
PUT lagou/job/1
{
    "key":"value",
    ...
}
POST lagou/job/1/_update
{
    "doc":{
        "key":"value",
        ...
    }
}

#刪除數據
DELETE lagou/job/1
DELETE lagou

批量操作

  • 批量獲取
GET lagou/job/_mget
{
    "docs":{
        條件
    }
}

GET lagou/job/1/_mget
{
    "ids":[id1,id2...]
}
  • bulk批量操作
POST _bulk
{"index":{"_index":"lagou","_type":"job","_id":"1"}}
{"title":"批量操作嘍","city":"北京"}
{"delete":{"_index":"lagou","_type":"job","_id":"1"}}
{"create":{"_index":"lagou","_type":"job","_id":"1"}}
{"update":{"_index":"lagou","_type":"job","_id":"1"}}

映射(即:創建表字段)

PUT lagou
{
    "mappings":{
        "properties":{
            "name":{
                "store":true,(是否保存)
                "type":屬性,(即字段類型)
                "analyzer":"ik_max_word"(分析器,即分詞類型)
            },
            "age":{
                "store":true,
                "type":屬性(如果是keywork則不會被分詞)
            },
            "date":{
                "store":true,
                "type":date,
                "format":"yy-MM-dd"
            }
            ...
        }
    }
}

查詢

基本查詢

  • match查詢
GET lagou/job/_search
{
    "query":{
        "match":{
            "字段":內容
            ...
        }
    }
}
  • term查詢(全量查詢,必須完全匹配)
GET lagou/job/_search
{
    "query":{
        "term":{
            "字段":內容
            ...
        }
    }
}
  • terms查詢(有一個匹配就返回,並且不是全量查詢)
GET lagou/job/_search
{
    "query":{
        "terms":{
            "字段":[內容1,...]
            ...
        }
    }
}
  • 控制返回數量
GET lagou/job/_search
{
    "query":{
        "match":{
            "字段":內容
            ...
        }
    },
    "form":0,
    "size":3
}
  • match_all查詢
GET lagou/job/_search
{
    "query":{
        "match_all":{}
    }
}
  • 多字段查詢
GET lagou/job/_search
{
    "query":{
        "multi_match":{
            "query":內容,
            "fields":[字段1^3,字段2...] # ^3表示權重
        }
    }
}
  • 指定返回字段
GET lagou/job/_search
{
    "stored_fields":[字段1,字段2...],
    "query":{
        "match":{
            "字段":內容,
            ...
        }
    }
}
  • 結果排序
GET lagou/job/_search
{
    "query":{
        "match_all":{}
    }
    "sort":[{
        "字段":{
            "order":"desc"
        }
    }
    ]
}
  • 範圍查詢(時間字段可以使用now關鍵字)
GET lagou/job/_search
{
    "query":{
        "range":{
            "字段":{
                "gte":10,
                "lte":20,
                "boost":2.0 # 權重
            }
        }
    }
}

組合查詢

  • bool查詢
bool:{
    "filter":[],    # 過濾字段
    "must":[],      # 條件必須全部滿足
    "should":[],    # 條件滿足其一
    "must_not":[]   # 必須一個不滿足
}

GET lagou/job/_search
{
    "query":{
        "bool":{
            "filter":{
                "查詢方式":{
                    "字段":內容
                }
            }
        }
    }
}
  • 查看分析器結果
GET _analyze
{
    "analyzer":"ik_max_word", # 與ik_smart區別:ik_smart以最小詞量分詞,如工程師就不會再進行工程/師分詞
    "text":"內容"
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章