Elasticsearch随笔02

fields 自定义返回字段

{
    //"_source": false,
    //"_source": ["aa*", "bb*"]
    //"_source": ["username"]
    "fields": ["username"],
    "aggs" : {
        "genders" : {
            "terms" : { "field": "username" }
        }
    }
}

基于时间范围的分类汇总

{
    //select yearmon(createtime) yearmon,method,status,count(*) 
    //  from test group by yearmon,method,status
    "size": 0,
    "aggs" : {
        "day_total" : {
            "date_histogram":{
                "field": "CreateTime",
                "interval": "day",
                "format": "yyyyMMdd"
            },
            "aggs": {
                "method_total":{
                    "terms": {"field": "method"},
                    "aggs":{                    
                        "status_total":{
                            "terms": {"field": "status"}
                        }
                    }                      
                }      
            }
        }
    }
}

Date detectionedit

If date_detection is enabled (default), then new string fields are checked to see whether their contents match any of the date patterns specified in dynamic_date_formats. If a match is found, a new date field is added with the corresponding format.

The default value for dynamic_date_formats is:

[“strict_date_optional_time”,”yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z”]

For example:

PUT my_index/my_type/1
{
  "create_date": "2015/09/02"
}

GET my_index/_mapping  

The create_date field has been added as a date field with the format: “yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z”.

Disabling date detectionedit

Dynamic date dection can be disabled by setting date_detection to false:

PUT my_index
{
  "mappings": {
    "my_type": {
      "date_detection": false
    }
  }
}

PUT my_index/my_type/1 
{
  "create": "2015/09/02"
}

The create_date field has been added as a string field.

Customising detected date formatsedit

Alternatively, the dynamic_date_formats can be customised to support your own date formats:

PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic_date_formats": ["MM/dd/yyyy"]
    }
  }
}

PUT my_index/my_type/1
{
  "create_date": "09/25/2015"
}

Disabling automatic type creationedit

Automatic type creation can be disabled by setting the index.mapper.dynamic setting to false, either by setting the default value in the config/elasticsearch.yml file, or per-index as an index setting:

PUT /_settings 
{
  "index.mapper.dynamic":false
}

强制清除Elasticsearch中已删除的文件

POST /_optimize?only_expunge_deletes=true

下面两个设置可以用于控制清除时的处理速度,其中给出值是默认值,可以根据需求进行调整,具体请参见 Merge。 此外, 还可以临时将所有索引的replica设置为0,这样只用针对Primary进行expunge,以减小I/O压力。

PUT /{index}/_settings
{
    "settings": {
        "index.merge.policy.expunge_deletes_allowed": "10",
        "index.merge.policy.max_merge_at_once_explicit" : "30" 
    }
}

检查文档是否存在

如果你想做的只是检查文档是否存在——你对内容完全不感兴趣——使用 HEAD 方法来代替 GET 。 HEAD 请求不会返回响应
体,只有HTTP头:

curl -i -XHEAD http://localhost:9200/website/blog/123
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 0

curl -i -XHEAD http://localhost:9200/website/blog/124
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=UTF-8
Content-Length: 0

该API同样也适合于检查index,type是否存在

Elasticsearch权威指南精华章节

官方API接口文档收集

内存性能优化方面

Index模块

Cluster模块

API接口

其它

模板

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章