请求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类型不支持*/ } } } } 请求url:
DELETE localhost:9200/test 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" } } 使用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会出现什么错误
返回错误如下
{ "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
请求url:
POST localhost:9200/blog1/article/1 请求体:
{ "id":1, "title":"ElasticSearch是一个基于Lucene的搜索服务器", "content":"我的地盘听我的" } 请求url:
POST localhost:9200/blog1/article/1 请求体:
{ "id":1, "title":"【修改】ElasticSearch是一个基于Lucene的搜索服务器", "content":"【修改】我的地盘听我的。" } 请求url:
DELETE localhost:9200/blog1/article/1