本文主要介绍一下关于ES的boolean类型,希望对大家理解和使用ES有帮助。
Boolea n Field
是接受JSON
true
或者false
的值,也接受可以被转为true
或者false
的字符串
例子:
curl -X PUT "localhost:9200/my-index-000001?pretty" -H 'Content-Type: application/json' -d'
{
"mappings": {
"properties": {
"is_published": {
"type": "boolean"
}
}
}
}
'
curl -X POST "localhost:9200/my-index-000001/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"is_published": "true"
}
'
curl -X GET "localhost:9200/my-index-000001/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"is_published": true
}
}
}
'
通过上面的例子可以看出,创建了一个名为my-index-000001
的文档,映射关系中有一个type
为boolean
的field
为is_published
。
下面是一个term
查询,匹配is_published
为true
的文档。
curl -X POST "localhost:9200/my-index-000001/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"is_published": true
}
'
curl -X POST "localhost:9200/my-index-000001/_doc/2?pretty" -H 'Content-Type: application/json' -d'
{
"is_published": false
}
'
curl -X GET "localhost:9200/my-index-000001/_search?pretty" -H 'Content-Type: application/json' -d'
{
"aggs": {
"publish_state": {
"terms": {
"field": "is_published"
}
}
},
"script_fields": {
"is_published": {
"script": {
"lang": "painless",
"source": "doc[\u0027is_published\u0027].value"
}
}
}
}
'
下面是所列的参数列表是boolean
field
所接受的: