首页 > 代码库 > elasticsearch最佳实践

elasticsearch最佳实践

创建索引

  1. 无mapping 
    创建索引名称为index的索引

    curl -XPUT http://localhost:9200/book
  2. 有mapping

    如果需要定义每个类型的结构映射,创建type名称为user和blogpost的mapping。

    curl -XPUT "http://localhost:9200/book" -d{"mappings": {"user": {   "_all":       { "enabled": false  },   "properties": {     "title":    { "type": "string"  },     "name":     { "type": "string"  },     "age":      { "type": "integer" }    }},"blogpost": {   "_all":       { "enabled": false  },   "properties": {     "id":       { "type": "string"  },    "title":    { "type": "string"  },     "body":     { "type": "string"  },    "created":  {      "type":   "date",       "format": "strict_date_optional_time||epoch_millis"    }  }}}}

索引文件

添加四个文档

curl -XPOST http://localhost:9200/book/blogpost/1 -d‘{"body":"美国留给伊拉克的是个烂摊子吗"}curl -XPOST http://localhost:9200/book/blogpost/2 -d‘{"body":"公安部:各地校车将享最高路权"}curl -XPOST http://localhost:9200/book/blogpost/3 -d‘{"body":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}curl -XPOST http://localhost:9200/book/blogpost/4 -d‘{"body":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}

 

高亮查询

curl -XPOST http://localhost:9200/book/blogpost/_search  -d‘{    "query" : { "term" : { "body" : "中国" }},    "highlight" : {        "pre_tags" : ["<tag1>", "<tag2>"],        "post_tags" : ["</tag1>", "</tag2>"],        "fields" : {            "content" : {}        }    }}
 

删除索引

curl -XDELETE http://localhost:9200/book

 

elasticsearch最佳实践