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接口

其它

模板

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