1. 程式人生 > >es索引與資料結構 ,以及其他知識使用

es索引與資料結構 ,以及其他知識使用

1:es   索引與資料結構對映
----es索引說明: 
索引結構: index/type/id   
對應的關係型資料庫結構  index = database , type = table , id = id 


----預設
--插入新的索引
PUT test-index/default/1
{
  "test":"one",
  "other":1
}
--查詢索引與資料結構
GET test-index/_mapping?pretty
-結果
{
  "test-index": {
    "mappings": {
      "default": {
        "properties": {
          "other": {
            "type": "long"
          },
          "test": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}


--解釋
es會對欄位型別進行分析和模式匹配,test的欄位會對映為string,other欄位對映為long


mapping - 對映和定義資料結構 
mapping由一個或多個analyzer(分詞器)組成, analyzer又由一個或多個filter組成的。當ES索引文件的時候,欄位中的內容傳遞給analyzer並通過子集的filters進行資料轉換(分詞,索引等操作)。


--自定義mapping
PUT newindex
{
  "mappings": {
      "default": {
        "properties": {
          "name": {
            "type": "text"
          },
          "age": {
            "type": "long"
          }
        }
      }
    }
}
--說明
索引newIndex - 指定 name為text ,age指定long


--檢視
GET newIndex/_mapping?pretty


--測試
PUT newindex/default/1
{
  "name":"xxx",
  "age":"xxx"
}
--結果
mapper_parsing_exception
failed to parse [age]


--測試2
PUT newindex/default/1
{
  "name":"xxx",
  "age":11
}
--結果
"created": true




2:es  對binary資料型別的支援


--原理
該binary型別接受一個二進位制值作為 Base64編碼的字串。該欄位預設情況下不儲存,不可搜尋

--使用
PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "name": {
          "type": "text"
        },
        "blob": {
          "type": "binary" ,
          "doc_values" : true
        }
      }
    }
  }
}

資料插入:
PUT my_index/_doc/1
{
  "name": "Some binary blob",
  "blob": "U29tZSBiaW5hcnkgYmxvYg==" 
}

----在外掛中獲取final ScriptDocValues.BytesRefs bytesRefs = (ScriptDocValues.BytesRefs)doc().get("blob");
byte[] binary = bytesRefs.getValue().bytes;
3:es 中文分詞器安裝與使用   
--這裡搬運自github  原github地址https://github.com/medcl/elasticsearch-analysis-ik
--安裝
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.2/elasticsearch-analysis-ik-6.2.2.zip


--使用
1.create a index


curl -XPUT http://localhost:9200/index


2.create a mapping
curl -XPOST http://localhost:9200/index/fulltext/_mapping -H 'Content-Type:application/json' -d'
{
        "properties": {
            "content": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word"
            }
        }
    
}'
3.index some docs


curl -XPOST http://localhost:9200/index/fulltext/1 -H 'Content-Type:application/json' -d'
{"content":"美國留給伊拉克的是個爛攤子嗎"}
'
curl -XPOST http://localhost:9200/index/fulltext/2 -H 'Content-Type:application/json' -d'
{"content":"公安部:各地校車將享最高路權"}
'
curl -XPOST http://localhost:9200/index/fulltext/3 -H 'Content-Type:application/json' -d'
{"content":"中韓漁警衝突調查:韓警平均每天扣1艘中國漁船"}
'
curl -XPOST http://localhost:9200/index/fulltext/4 -H 'Content-Type:application/json' -d'
{"content":"中國駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首"}
'
4.query with highlighting


curl -XPOST http://localhost:9200/index/fulltext/_search  -H 'Content-Type:application/json' -d'
{
    "query" : { "match" : { "content" : "中國" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}
'
Result


{
    "took": 14,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 2,
        "hits": [
            {
                "_index": "index",
                "_type": "fulltext",
                "_id": "4",
                "_score": 2,
                "_source": {
                    "content": "中國駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首"
                },
                "highlight": {
                    "content": [
                        "<tag1>中國</tag1>駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首 "
                    ]
                }
            },
            {
                "_index": "index",
                "_type": "fulltext",
                "_id": "3",
                "_score": 2,
                "_source": {
                    "content": "中韓漁警衝突調查:韓警平均每天扣1艘中國漁船"
                },
                "highlight": {
                    "content": [
                        "均每天扣1艘<tag1>中國</tag1>漁船 "
                    ]
                }
            }
        ]
    }
}


4:es  索引模板
--場景
當定義多個類似結構的索引資料時使用索引分段儲存命名my_* 會對所有以my_為字首的索引進行結構對映

--使用

PUT _template/my_template
{
    "template" : "my-*",
    "order" : 0,
    "settings" : {
         "number_of_shards" : 10,
 "number_of_replicas" : 0
    },
    "mappings": {

      "default": {

  "_all": {
        "enabled": false
      }
        "properties": {
          "name": {
            "type": "text"
          },
          "age": {
            "type": "long"
          }
        }
    }
  }

}


5:es   查詢快取
--原理


--使用


6:es  分片與查詢
--原理與意義


--合理使用


7: es系統監控工具 安裝x-pack 
可看這篇文章:http://blog.csdn.net/ailice001/article/details/79474290