elasticsearch索引结构和配置优化
elasticsearch索引結(jié)構(gòu)和配置簡單調(diào)優(yōu).
1.搜索時對特定字段設(shè)置更高權(quán)值,以弱化相關(guān)性低的字段
例如:我們在搜索時認為標題對我們更重要就可以對標題提高匹配權(quán)重
boolQuery.must(QueryBuilders.matchQuery(HouseIndexKey.TITLE, rentSearch.getKeywords()).boost(2.0f));2.一般elasticsearch只是用來做檢索的,而不適合存儲原始結(jié)果集,所以我們只需要檢索后id(比如houseId),而不需要返回整個結(jié)果集
所以我們只需要獲取id即可(如果返回整個字段的數(shù)據(jù)集,當數(shù)據(jù)量過大將會導(dǎo)致性能大大降低);因此我們可以通過
setFetchSource(HouseIndexKey.HOUSE_ID, null)方法只返回houseId。
SearchRequestBuilder requestBuilder = this.esClient.prepareSearch(INDEX_NAME).setTypes(INDEX_TYPE).setQuery(boolQuery).addSort(HouseSort.getSortKey(rentSearch.getOrderBy()),SortOrder.fromString(rentSearch.getOrderDirection())).setFrom(rentSearch.getStart()).setSize(rentSearch.getSize()).setFetchSource(HouseIndexKey.HOUSE_ID, null);?? ??? ??? ??? ?
3.索引結(jié)構(gòu)優(yōu)化
索引讀寫優(yōu)化:索引存儲采用niofs
"index.store.type": "niofs",索引模版采用strict嚴格模式,即在整個索引結(jié)構(gòu)穩(wěn)定的情況下不允許隨意更改,當然不穩(wěn)定的情況下可以指定為false,可以動態(tài)更改
"dynamic": "strict",禁用_all字段,防止將所有的字符串字段連接起來做全文檢索,影響檢索性能(es6.x以上版本貌似已經(jīng)廢棄該字段)
Index中默認會有_all這個字段(es6.x已經(jīng)禁用),默認會把所有字段的內(nèi)容都拷貝到這一個字段里面,這樣會給查詢帶來方便,但是會增加索引時間和索引尺寸。
"_all": {"enabled": false},設(shè)置默認查詢字段
"index.query.default_field": "title"設(shè)置節(jié)點掉線延時操作時間(5m),防止由于網(wǎng)絡(luò)原因?qū)е录褐行遁d該分配節(jié)點
"index.unassigned.node_left.delayed_timeout": "5m"?
注意:分片和副本的設(shè)置需要看集群的大小(我如下的索引設(shè)置副本為0,分配為5是因為我是單節(jié)點測試的,各位如果是集群節(jié)點注意修改這另兩個參數(shù))
{"settings": {"number_of_replicas": 0,"number_of_shards": 5,"index.store.type": "niofs","index.query.default_field": "title","index.unassigned.node_left.delayed_timeout": "5m"},"mappings": {"house": {"dynamic": "strict","_all": {"enabled": false},"properties": {"houseId": {"type": "long"},"title": {"type": "text","index": "analyzed","analyzer": "ik_smart","search_analyzer": "ik_smart"},"price": {"type": "integer"},"area": {"type": "integer"},"createTime": {"type": "date","format": "strict_date_optional_time||epoch_millis"},"lastUpdateTime": {"type": "date","format": "strict_date_optional_time||epoch_millis"},"cityEnName": {"type": "keyword"},"regionEnName": {"type": "keyword"},"direction": {"type": "integer"},"distanceToSubway": {"type": "integer"},"subwayLineName": {"type": "keyword"},"subwayStationName": {"type": "keyword"},"tags": {"type": "text"},"street": {"type": "keyword"},"district": {"type": "keyword"},"description": {"type": "text","index": "analyzed","analyzer": "ik_smart","search_analyzer": "ik_smart"},"layoutDesc" : {"type": "text","index": "analyzed","analyzer": "ik_smart","search_analyzer": "ik_smart"},"traffic": {"type": "text","index": "analyzed","analyzer": "ik_smart","search_analyzer": "ik_smart"},"roundService": {"type": "text","index": "analyzed","analyzer": "ik_smart","search_analyzer": "ik_smart"},"rentWay": {"type": "integer"},"suggest": {"type": "completion"},"location": {"type": "geo_point"}}}}}?
4.配置優(yōu)化
禁止通配符刪除索引(索引刪除的 后果是不可逆的,且刪且珍惜)
執(zhí)行PUT http://10.0.2.19:9200/_cluster/settings
設(shè)置請求參數(shù):
{"transient":{"action.destructive_requires_name":true}}查看設(shè)置:
設(shè)置延時刷新時間
調(diào)整refresh時間間隔,優(yōu)化點: 減少刷新頻率,降低潛在的寫磁盤性能損耗, 默認的刷新時間間隔是1s,對于寫入量很大的場景,這樣的配置會導(dǎo)致寫入吞吐量很低,適當提高刷新間隔,可以提升寫入量,代價就是讓新寫入的數(shù)據(jù)在60s之后可以被搜索,新數(shù)據(jù)可見的及時性有所下降。
index.refresh_interval: 30s集群發(fā)現(xiàn)超時優(yōu)化
#節(jié)點間的存活時間檢測間隔discovery.zen.fd.ping_interval: 10s#存活超時時間discovery.zen.fd.ping_timeout: 120s#存活超時重試次數(shù)discovery.zen.fd.ping_retries: 5另外對于集群機器資源夠多的情況下,可以設(shè)置主節(jié)點不存儲數(shù)據(jù)(一般小集群規(guī)模會設(shè)置主節(jié)點和從節(jié)點都作為數(shù)據(jù)節(jié)點),看各自的業(yè)務(wù)情況應(yīng)變處理。
指揮節(jié)點(主節(jié)點):
#指揮節(jié)點配置 #節(jié)點名稱 node.name: master #是否是主節(jié)點 node.master: true #是否存儲數(shù)據(jù)(改為false則不做數(shù)據(jù)節(jié)點,根據(jù)情況設(shè)計) node.data: true數(shù)據(jù)節(jié)點(從節(jié)點):
#數(shù)據(jù)節(jié)點配置 node.name: slave1 node.master: false node.data: true針對數(shù)據(jù)節(jié)點 http功能關(guān)閉設(shè)置(關(guān)閉數(shù)據(jù)節(jié)點的http通信,只開啟tcp數(shù)據(jù)通信,可以降低數(shù)據(jù)節(jié)點的訪問負載)
?
?
總結(jié)
以上是生活随笔為你收集整理的elasticsearch索引结构和配置优化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: elasticsearch搜素关键字自动
- 下一篇: 女孩4万多元的摩托 被男摩友加白糖报废!