ElasticSearch使用小結

         最近有個業務需求,即全文搜索關鍵字查詢列表,於是轉向ES的學習,也學習了大半個月了,作個筆記,總結下本身的學習歷程。css

         獨自學習一項新技術,老是不免走很多彎路的,在此推薦下ES的基礎教程,對,好好學習官網教程就能夠了!html

1)  Elasticsearch: 權威指南node

https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.htmlgit

權威中文教程,對於英文很差的同窗,讀這個教程能夠快速入門。github

2)ElasticsearchReference  官網英文教程spring

https://www.elastic.co/guide/en/elasticsearch/reference/current/index.htmlapi

真正想吃透ES仍是多看英文官網文檔,知識點講的是最全面的。restful

ES版本選擇

         說道ES版本選擇,初學ES的時候,查到的大部分資料都是針對2.x版本的,本打算也是用2.x版本,可是讀到ES5.x版本新特性說明的時候,果斷仍是選用5.x版本,由於新版ES性能比2.x版本好太多了,並且原本也想使用spring-data-elasticsearch,可是spring-data不支持5.x版本,並且ES發展勢頭強勁,半個月前使用的5.5.0版本,如今已經更新到5.5.1。markdown

ES客戶端請求方式

1)Java API:建立TransportClient,複雜應用推薦使用app

2)Java REST Client:建立RestClient

3)http restful api:使用最原始的http請求訪問

目前暫時使用的第三種,緣由ES業務需求單一,不須要動態建立、刪除索引,上手簡單,只須要學習es rest語法就能夠了。其實後期能夠切換到RestClient,它是持久化http連接(使用httpClient還須要一個http鏈接池),特色如官方所說:

The low-level client’s features include:

  • minimal dependencies
  • load balancing across all available nodes
  • failover in case of node failures and upon specific response codes
  • failed connection penalization (whether a failed node is retrieddepends on how many consecutive times it failed; the more failed attempts thelonger the client will wait before trying that same node again)
  • persistent connections
  • trace logging of requests and responses
  • optional automatic discovery of cluster nodes

ES經常使用插件

1)head插件

5.5使用教程:http://www.cnblogs.com/xing901022/p/6030296.html

2)ik中文分析器 – 中文分詞必備,能夠自定義詞典

github地址:https://github.com/medcl/elasticsearch-analysis-ik

插件下載地址:https://github.com/medcl/elasticsearch-analysis-ik/releases 博主更新很及時,5.5.1的已經有啦。

3)pinyin分析器

github地址: https://github.com/medcl/elasticsearch-analysis-pinyin

ik和pinyin同一個做者,elastic中文社區創始人。

4)elasticsearch-analysis-lc-pinyin分析器

這一款插件也很不錯,可是沒有pinyin聲勢大。支持全拼、首字母、中文混合搜索。後面拼音全文搜索準備測試下效果,目前分析器使用的仍是ik和pinyin。

ES集羣

ES集羣的配置,權威教程講的很粗糙,當時還花了好幾天返回測試,最終發現仍是配置文件參數沒有吃透。

Minimum Master Nodes

最小主節點數的設置對集羣的穩定是很是重要的。該設置對預防腦裂是有幫助的,即一個集羣中存在兩個master。

這個配置就是告訴Elasticsearch除非有足夠可用的master候選節點,不然就不選舉master,只有有足夠可用的master候選節點才進行選舉。

該設置應該始終被配置爲有主節點資格的法定節點數,法定節點數:(主節點資格的節點數/2)+1。例如:

一、若是你有10個符合規則的節點數,法定數就是6.

二、若是你有3個候選master,和100個數據節點,法定數就是2,你只要計算那些有主節點資格的節點數就能夠了。

三、若是你有2個符合規則的節點數,法定節點數應該是2,可是這意味着若是一個節點狗帶了,你的整個集羣就不能夠用了。設置成1將保證集羣的功能,可是就不能防止腦裂了。基於這樣的狀況,最好的解決就是至少有3個節點。

小集羣或本地測試能夠不用區分master node,data node,client node。但生產環境爲了保證最大的可伸縮性,官方建議不一樣的類型節點加以區分,默認狀況的elasticsearch既是master node,也是data node。關於節點的知識,可參看轉載的《Elasticsearch節點類型》。

我目前使用的集羣配置:一個Client節點,3個master/data混合節點。使用RestClient能夠省去一個Client節點。

建立索引、類型示例

一、建立索引與配置分析器
{
    "settings":{
        "index":{
            "number_of_shards":3,
            "number_of_replicas":1,
            "analysis":{
                "analyzer":{
                    "ik_analyzer":{
                        "type":"custom",
                        "tokenizer":"ik_smart"
                    },
                    "pinyin_analyzer":{
                        "tokenizer":"my_pinyin"
                    }
                },
                "tokenizer":{
                    "my_pinyin":{
                        "type":"pinyin",
                        "keep_original":true
                    }
                }
            }
        }
    }
}
二、建立type並設置mapping
{
    "ProductTour":{
        "properties":{
            "companyId":{
                "type":"integer"
            },
            "productCode":{
                "type":"keyword"
            },
            "productType":{
                "type":"text",
                "analyzer":"ik_analyzer",
                "fields":{
                    "pinyin":{
                        "type":"text",
                        "analyzer":"pinyin_analyzer"
                    }
                }
            },
            "gType":{
                "type":"keyword"
            },
            "lineType":{
                "type":"keyword"
            },
            "productState":{
                "type":"boolean"
            },
            "auditState":{
                "type":"integer"
            },
            "productMainTitle":{
                "type":"text",
                "analyzer":"ik_analyzer",
                "fields":{
                    "pinyin":{
                        "type":"text",
                        "analyzer":"pinyin_analyzer"
                    }
                }
            },
            "productSubTitle":{
                "type":"text",
                "analyzer":"ik_analyzer",
                "fields":{
                    "pinyin":{
                        "type":"text",
                        "analyzer":"pinyin_analyzer"
                    }
                }
            },
            "supplyProductName":{
                "type":"keyword"
            },
            "productMainPic":{
                "type":"keyword"
            },
            "productPic":{
                "type":"keyword"
            },
            "dpt":{
                "type":"keyword"
            },
            "arr":{
                "type":"text",
                "analyzer":"ik_analyzer",
                "fields":{
                    "pinyin":{
                        "type":"text",
                        "analyzer":"pinyin_analyzer"
                    }
                }
            },
            "productFeatures":{
                "type":"text",
                "analyzer":"ik_analyzer",
                "fields":{
                    "pinyin":{
                        "type":"text",
                        "analyzer":"pinyin_analyzer"
                    }
                }
            },
            "tripDay":{
                "type":"integer"
            },
            "tripNight":{
                "type":"integer"
            },
            "advanceDays":{
                "type":"integer"
            },
            "auditResult":{
                "type":"keyword"
            },
            "createTime":{
                "type":"date"
            }
        }
    }
}
三、全文檢索
{
    "from":0,
    "size":10,  // 分頁查詢
    "query":{
        "bool":{
            "must":[
                {
                    "multi_match":{   // 全文搜索
                        "query":"1日",  // 關鍵詞
                        "fields":[   // 全文搜索字段
                            "productType",
                            "productMainTitle",
                            "productSubTitle",
                            "arr",
                            "productFeatures"
                        ]
                    }
                }
            ],
            "filter":[   // 篩選條件
                {
                    "term":{
                        "productType":"themt"
                    }
                }
            ]
        }
    }
}
若是關鍵字爲字母混合漢字,全文搜索字段換成:

"fields": [
    //全文搜索字段"productType.pinyin",
    "productMainTitle.pinyin",
    "productSubTitle.pinyin",
    "arr.pinyin",
    "productFeatures.pinyin"
]

使用中遇到的坑:

mapping set to strict, dynamic introduction of [doc]不可用;
使用term類型,即精確查詢,字符型類型使用keyword!不能使用text;
關鍵字若爲漢字,使用ik分詞器;關鍵字若爲拼音或拼音漢字混合,使用pinyin分詞器;
相關文章
相關標籤/搜索