ElasticSearch之index

创建index

请求url:

PUT localhost:9200/test

请求体:

{


        "settings": {
                 "analysis": {
                      "analyzer": {
                          "comma": {
                               "type": "pattern",
                               "pattern":","
                           }
                      }
                  }
        },
    "mappings": {

        "article": {

            "properties": {

                "id": { "type": "long" },

                "pubdate": { "type": "date" },

                 "source": { "type": "text","analyzer": "standard"}, /*标准分词器*/

                "title": { "type": "text", "analyzer":"ik_max_word" }, /*ik分词器*/

                 "summary": {"type": "text", "analyzer":"ik_smart" }, /*ik分词器*/

                 "relatecolumn": {"type": "text", "analyzer":"pattern" },   /*按逗号分割,需要自定义符号分词器*/

                 "stocks": {"type": "text","analyzer":"comma","fields": {"size": {"type": "token_count","analyzer": "comma"}}}, /*按逗号分割,并附加filed统计分词个数*/

                 "photo": {"type": "keyword","null_value": "000000"}, /*null_value 空值处理 text类型不支持*/

            }

        }

    }

}

删除index

请求url:

DELETE localhost:9200/test

index设置bak

POST http://192.168.2.200:9200/_reindex
{
   "source": {
      "index": "pdf_msg"
   },
   "dest": {
      "index": "pdf_msg.bak",
      "version_type": "external"
   }
}


{
   "source": {
      "index": "pdf_msg.bak"
   },
   "dest": {
      "index": "pdf_msg",
      "version_type": "external"
   }
} 

index空值默认值

使用null_value替换显示的空值
删除上边定义的索引delete kaka,然后自定义mapping,给tags设置"null_value" : “null”,用指定的值替换显示的空值,"null"可以自定义为任意值

使用了null_value这样做的好处就是空字段也可以被索引,同时也不会在查询时报field name is null or empty的错误

put kaka 
{ 
	 "mappings":{    
		"properties":{      
			"tags" :{        "type":"keyword",        "null_value":"null"      }   
		 } 
	}
}

再插入上边的数据

POST /kaka/_bulk
{ "index": { "_id": "1"}}{ "tags" : ["search"]}  
{ "index": { "_id": "2"}}{ "tags" : ["search", "open_source"] } 
{ "index": { "_id": "3"}}{ "tags" : null }  
{ "index": { "_id": "4"}}{ "tags" :"null"}

再次执行查询为null的数据,就会出现第3、4条数据

post /kaka/_search 
{  
	"query":{   
		 "term": {      "tags": "null"    }  
	},  
	"profile":"true"
}

使用null_value注意点
null_value必须和定义的数据类型匹配,例如long类型的不能定义字符串类型的value_null值

看一下long类型设置了字符串类型value_null会出现什么错误

错误演示,long类型使用了字符串类型的null_value值put kaka{ “mappings”:{ “properties”:{ “tags” :{ “type”:“long”, “null_value”:“null” } } }}

返回错误如下

{  "error": {    "root_cause": [      {        "type": "mapper_parsing_exception",        "reason": "Failed to parse mapping [_doc]: For input string: \"null\""      }    ],    "type": "mapper_parsing_exception",    "reason": "Failed to parse mapping [_doc]: For input string: \"null\"",    "caused_by": {      "type": "number_format_exception",      "reason": "For input string: \"null\""    }  },  "status": 400}

注意了数据类型外,你还需要知道value_null不是任何类型都可以使用的,以下列举的类型都可使用null_value
Array
Boolean
Date
geo_point
ip
keyword
Numeric
point

创建文档document

请求url:

POST localhost:9200/blog1/article/1

请求体:

{

"id":1,

"title":"ElasticSearch是一个基于Lucene的搜索服务器",

"content":"我的地盘听我的"

}

修改文档document

请求url:

POST localhost:9200/blog1/article/1

请求体:

{

"id":1,

"title":"【修改】ElasticSearch是一个基于Lucene的搜索服务器",

"content":"【修改】我的地盘听我的。"

}

删除文档document

请求url:

DELETE localhost:9200/blog1/article/1
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

diff_dba

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值