Elasticsearch之_default_—— 為索引添加默認映射


前篇說過,ES可以自動為文檔設定索引。但是問題也來了——如果默認設置的索引不是我們想要的,該怎么辦呢?

要知道ES這種搜索引擎都是以Index為實際的分區,Index里面包含了不同的類型,不同的類型是邏輯上的分區;每種類型可能包含有相同的字段,如果字段的類型相同還好,如果不同....那就會導致字段的沖突了。

本篇就講述如何使用REST API以及Logstash設置默認的索引。

更多內容參考:Elasticsearch知識匯總

使用Rest API設置默認的索引

首先先看一下不設置默認索引時,我們想要索引一個IP地址的字段,會發生什么?輸入下面的命令:

$ curl -XPUT localhost:9200/test/test/1 -d '{"ip":"192.168.0.1"}'

查看映射可以發現,我們想要存儲成IP類型,但是默認給存儲成了字符串類型:

$ curl -XGET localhost:9200/test/_mapping?pretty           {
  "test" : {
    "mappings" : {
      "test" : {
        "properties" : {
          "ip" : {
            "type" : "string"
          }
        }
      }
    }
  }
}

這並不是我們想要的。

由於映射一旦設定好了,就不能修改了。因此再次實驗的時候,需要刪除索引test

$ curl -XDELETE localhost:9200/test                        
{"acknowledged":true}

然后設置test的默認映射:

$ curl -XPUT localhost:9200/test?pretty -d '{"mappings":{"_default_":{"properties":{"ip":{"type":"ip"}}}}}'

上面的命令中,設置test索引中,默認字段ip的屬性為ip。這樣我們查詢test的映射時,發現ip字段已經被設置為ip:

$ curl -XGET localhost:9200/test/_mapping?pretty           {
  "test" : {
    "mappings" : {
      "_default_" : {
        "properties" : {
          "ip" : {
            "type" : "ip"
          }
        }
      }
    }
  }
}

然后插入一段數據,為了觀察到插入數據后的映射的變化,可以多插入一個字段:

$ curl -XPUT localhost:9200/test/test/2 -d '{"name":"xingoo","ip":"192.168.0.1"}'

然后查詢映射,可以讀取到默認映射信息以及當前的映射信息:

$ curl -XGET localhost:9200/test/_mapping?pretty           {
  "test" : {
    "mappings" : {
      "test" : {
        "properties" : {
          "ip" : {
            "type" : "ip"
          },
          "name" : {
            "type" : "string"
          }
        }
      },
      "_default_" : {
        "properties" : {
          "ip" : {
            "type" : "ip"
          }
        }
      }
    }
  }
}

恭喜~IP字段的類型已經變成了ip,類似的,我們可以設置date,geo,object等類型。

在Logstash中配置默認的索引

Logstash中默認索引的設置是基於模板的,原理上跟上面差不多。

首先我們需要指定一個默認的映射文件,文件的內容大致如下:

{
  "template" : "logstash-*",
  "mappings" : {
    "_default_" : {
       "properties" : {
             "ip" :{
                  "type":"ip"
             }
          }
       }
    }
}    

其中template定義了匹配的索引模式,如果針對於特定的某個索引,則直接寫成索引的名字即可。下面定義了映射的相關信息,與API的內容相同。

有了上面的配置文件,就可以在Logstash中配置output插件了:

output {
    elasticsearch {
        host => "localhost" #ES的服務器地址
        protocol => "http" #使用的協議,默認可能會使用Node,具體還要看機器的環境
        index => "logstash-%{+YYYY.MM.dd}" #匹配的索引模式
        document_type => "test" #索引的類型,舊的配置會使用index_type,但是這個字段在新版本中已經被舍棄了,推薦使用document_type
       manage_template => true #注意默認為true,一定不能設置為false
        template_overwrite => true #如果設置為true,模板名字一樣的時候,新的模板會覆蓋舊的模板
        template_name => "myLogstash" #注意這個名字是用來查找映射配置的,盡量設置成全局唯一的
      template => "D:/test/logstash.conf" #映射配置文件的位置
    }
}

其中后四個是使用默認映射需要注意的地方,詳細的可以多了解Logstash的源碼。

參考

【1】_default_映射:https://www.elastic.co/guide/en/elasticsearch/reference/current/default-mapping.html

【2】Elasticsearch output插件:https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html

【3】ELK電子書:http://kibana.logstash.es/content/logstash/plugins/output/elasticsearch.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM