---
title: "Elasticsearch TermFacet 耗内存问题"
author: "maxrocray"
date: "2012-11-01T15:02:01Z"
tags: []
categories: ["开源软件"]
slug: "elasticsearch-termfacet"
draft: false
lastmod: "2012-12-14T17:02:11Z"
id: 1711456
url: "https://maxrocray.iteye.com/blog/1711456"
---

解决方案有下：

 1.设置cache大小和过期时间。

   index.cache.field.max\_size

   index.cache.field.expire

 

例如设置：

  index.cache.field.max\_size: 50000 //index中每个segment中可包含的最大的entries数目

`index.cache.field.expire: 10m //过期时间为10分钟`

 

 2.改变cache类型。

 index.cache.field.type: soft

 默认类型为*resident， 字面意思是常驻（居民）， 一直增加，直到内存* 耗尽。 改为soft就是当内存不足的时候，先clear掉 占用的，然后再往内存中放。设置为soft后，相当于设置成了相对的内存大小。*resident的话，除非内存够大。*

 

 3.对数据进行处理。

 文章中提到的是减小字段值长度，如将大写转成小写。

 这点上，实际中可能将数据精炼。当然， 也可以把要做facet的字段做一个转化，用int型代替。

 关于string转化int呢， 可以参考M大神的: [https://github.com/medcl/elasticsearch-analysis-string2int](https://github.com/medcl/elasticsearch-analysis-string2int)

 

 

 

当然， 实际中采用方式2基本上没有问题。 如果根据自己的实际情况结合使用效果会更好。

 

可参考如下文章：

[http://blog.sematext.com/2012/05/17/elasticsearch-cache-usage/](http://blog.sematext.com/2012/05/17/elasticsearch-cache-usage/)