elasticsearch索引结构和配置优化

elasticsearch索引结构和配置简单调优.

1.搜索时对特定字段设置更高权值,以弱化相关性低的字段

例如:我们在搜索时认为标题对我们更重要就可以对标题提高匹配权重

boolQuery.must(

                QueryBuilders.matchQuery(HouseIndexKey.TITLE, rentSearch.getKeywords())

                        .boost(2.0f)

       );

2.一般elasticsearch只是用来做检索的,而不适合存储原始结果集,所以我们只需要检索后id(比如houseId),而不需要返回整个结果集

所以我们只需要获取id即可(如果返回整个字段的数据集,当数据量过大将会导致性能大大降低);因此我们可以通过

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.索引结构优化

索引读写优化:索引存储采用niofs

"index.store.type": "niofs",

索引模版采用strict严格模式,即在整个索引结构稳定的情况下不允许随意更改,当然不稳定的情况下可以指定为false,可以动态更改

"dynamic": "strict",

禁用_all字段,防止将所有的字符串字段连接起来做全文检索,影响检索性能(es6.x以上版本貌似已经废弃该字段)

Index中默认会有_all这个字段(es6.x已经禁用),默认会把所有字段的内容都拷贝到这一个字段里面,这样会给查询带来方便,但是会增加索引时间和索引尺寸。

"_all": {

        "enabled": false

      },

设置默认查询字段

"index.query.default_field": "title"

设置节点掉线延时操作时间(5m),防止由于网络原因导致集群中卸载该分配节点

"index.unassigned.node_left.delayed_timeout": "5m"

 

注意:分片和副本的设置需要看集群的大小(我如下的索引设置副本为0,分配为5是因为我是单节点测试的,各位如果是集群节点注意修改这另两个参数)

{

  "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.配置优化

禁止通配符删除索引(索引删除的 后果是不可逆的,且删且珍惜)

执行PUT http://10.0.2.19:9200/_cluster/settings

设置请求参数:

{

    "transient":{

        "action.destructive_requires_name":true

    }

}

查看设置:

设置延时刷新时间

调整refresh时间间隔,优化点: 减少刷新频率,降低潜在的写磁盘性能损耗, 默认的刷新时间间隔是1s,对于写入量很大的场景,这样的配置会导致写入吞吐量很低,适当提高刷新间隔,可以提升写入量,代价就是让新写入的数据在60s之后可以被搜索,新数据可见的及时性有所下降。

index.refresh_interval: 30s

集群发现超时优化

#节点间的存活时间检测间隔

discovery.zen.fd.ping_interval: 10s

#存活超时时间

discovery.zen.fd.ping_timeout: 120s

#存活超时重试次数

discovery.zen.fd.ping_retries: 5

另外对于集群机器资源够多的情况下,可以设置主节点不存储数据(一般小集群规模会设置主节点和从节点都作为数据节点),看各自的业务情况应变处理。

指挥节点(主节点):

#指挥节点配置
#节点名称
node.name: master
#是否是主节点
node.master: true
#是否存储数据(改为false则不做数据节点,根据情况设计)
node.data: true

数据节点(从节点):

#数据节点配置
node.name: slave1
node.master: false
node.data: true

针对数据节点 http功能关闭设置(关闭数据节点的http通信,只开启tcp数据通信,可以降低数据节点的访问负载)
 

http.enable: false

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值