标签:
使用如下语句创建一个名字为:user_document,别名为user的索引
{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
},
"mappings" : {
"user_1" : {
"properties" : {
"Identity" :
{
"type" : "string",
"index" : "not_analyzed"
},
"Name" :
{
"type" : "string"
},
"Age" :
{
"type" : "integer"
},
"Birthday" :
{
"format": "dateOptionalTime",
"type": "date"
},
"Sex" :
{
"type" : "boolean"
}
}
}
},
"aliases" : {
"user" : {}
}
}
并新增10条测试数据
GET _search { "query": { "match_all": {} } }
匹配索引中的全部文档,默认得分为1.0
GET _search { "query": { "match": { "Name":"陈楚生" } } }
结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.8677558,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 1.8677558,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcPTKOpxK9K1OdHXH",
"_score": 0.6856395,
"_source": {
"Identity": "360501199001016212",
"Name": "陈楚河",
"Age": 40,
"Birthday": "1978-05-09T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcnK7OpxK9K1OdHXL",
"_score": 0.14631948,
"_source": {
"Identity": "360501199001016214",
"Name": "陈乔恩",
"Age": 36,
"Birthday": "1980-05-09T14:12:12",
"Sex": false
}
}
]
}
}
Match查询字符串时,会将文本内容分词并做Or操作
GET _search
{
"query":
{
"multi_match": {
"query": "陈楚生",
"fields": ["Name","Identity"]
}
}
}
同Match查询,但是可以同时查多个字段
GET _search
{
"query":
{
"query_string": {
"default_field": "Name",
"query": "陈楚生"
}
}
}
结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.8677558,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 1.8677558,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcPTKOpxK9K1OdHXH",
"_score": 0.6856395,
"_source": {
"Identity": "360501199001016212",
"Name": "陈楚河",
"Age": 40,
"Birthday": "1978-05-09T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcnK7OpxK9K1OdHXL",
"_score": 0.14631948,
"_source": {
"Identity": "360501199001016214",
"Name": "陈乔恩",
"Age": 36,
"Birthday": "1980-05-09T14:12:12",
"Sex": false
}
}
]
}
}
分词查询,也可以想同时查询多个字段
GET _search
{
"query":
{
"term": {
"Identity": {
"value": "360501199001016210",
"boost": 2
}
}
}
}
结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 2.5040774,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 2.5040774,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
}
]
}
}
Term查询一般用来做精确查询,即查询关键字需要和被查询字段值完全一致才会被查询出来。
所以如果当被查询的字段是一个String类型时,该类型需要被显示标记为not_analyzed,
否则字段内容会先被分词器分成许多个词条,导致term查询不出结果。
可以使用Match 或Query_String查询会被分词的字段。
GET _search
{
"query":
{
"terms": {
"Age": [
35,30
]
}
}
}
结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.8853251,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 0.8853251,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDc4xqOpxK9K1OdHXN",
"_score": 0.8853251,
"_source": {
"Identity": "360501199001016214",
"Name": "唐嫣",
"Age": 30,
"Birthday": "1986-05-09T14:12:12",
"Sex": true
}
}
]
}
}
查询一个值数组范围内的所有文档
GET _search
{
"query":
{
"range": {
"Age": {
"from": 25,
"to": 35
}
}
}
}
结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 1,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDch_LOpxK9K1OdHXK",
"_score": 1,
"_source": {
"Identity": "360501199001016214",
"Name": "赵默笙",
"Age": 28,
"Birthday": "1988-05-09T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDc4xqOpxK9K1OdHXN",
"_score": 1,
"_source": {
"Identity": "360501199001016214",
"Name": "唐嫣",
"Age": 30,
"Birthday": "1986-05-09T14:12:12",
"Sex": true
}
}
]
}
}
范围查询
标签:
原文地址:http://www.cnblogs.com/Jabben/p/5683060.html