elasticsearch-查询
使用如下语句创建一个名字为:user_document,别名为user的索引
PUT:http://localhost:9200/user_document
{
"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条测试数据
1、Match All 查询
GET _search
{
"query":
{
"match_all": {}
}
}
匹配索引中的全部文档,默认得分为1.0
2、Match 查询
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操作
3、Multi Match 查询
GET _search
{
"query":
{
"multi_match": {
"query": "陈楚生",
"fields": ["Name","Identity"]
}
}
}
同Match查询,但是可以同时查多个字段
4、Query String 查询
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
}
}
]
}
}
分词查询,也可以想同时查询多个字段
5、Term 查询
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查询会被分词的字段。
6、Terms 查询
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
}
}
]
}
}
查询一个值数组范围内的所有文档
7、Range 查询
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
}
}
]
}
}
范围查询
8、filter 过滤器
GET _search
{
"query": {
"filtered": {
"query": {
"bool": {
"should": [
{
"bool": {
"must":
{
"query_string": {
"default_field": "Name",
"query": "陈楚生"
}
},
"must_not":
{
"term": {
"Age": {
"value": 40
}
}
}
}
},
{
"bool": {
"should":
{
"term": {
"Identity": {
"value": "360501199001016211"
}
}
}
}
}
]
}
},
"filter": {
"range": {
"Age": {
"from": 30,
"to": 35
}
}
}
}
}
}
结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.7757143,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 0.7757143,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
}
]
}
}
filter过滤器只会用于过滤文档,不会影响文档的得分情况
9、Constant Score 查询
GET _search
{
"query": {
"constant_score": {
"filter": {
"query": {
"query_string": {
"default_field": "Name",
"query": "陈楚生"
}
}
},
"boost": 1.2
}
}
}
结果
{
"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": "AVYDcPTKOpxK9K1OdHXH",
"_score": 1,
"_source": {
"Identity": "360501199001016212",
"Name": "陈楚河",
"Age": 40,
"Birthday": "1978-05-09T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcnK7OpxK9K1OdHXL",
"_score": 1,
"_source": {
"Identity": "360501199001016214",
"Name": "陈乔恩",
"Age": 36,
"Birthday": "1980-05-09T14:12:12",
"Sex": false
}
}
]
}
}
该查询出来的文档得分是一个恒定的分数1
10、Bool 查询
GET _search
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"query_string": {
"default_field": "Name",
"query": "陈楚生"
}
}
],
"must_not": [
{
"term": {
"Age": {
"value": 40
}
}
}
]
}
},
{
"bool": {
"should": [
{
"term": {
"Identity": {
"value": "360501199001016211"
}
}
}
]
}
}
]
}
}
}
结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.7757143,
"hits": [
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDbtj0OpxK9K1OdHXF",
"_score": 0.7757143,
"_source": {
"Identity": "360501199001016210",
"Name": "陈楚生",
"Age": 35,
"Birthday": "1981-07-25T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcHILOpxK9K1OdHXG",
"_score": 0.6971515,
"_source": {
"Identity": "360501199001016211",
"Name": "何家劲",
"Age": 52,
"Birthday": "1964-07-25T14:12:12",
"Sex": false
}
},
{
"_index": "user_document",
"_type": "user_1",
"_id": "AVYDcnK7OpxK9K1OdHXL",
"_score": 0.060769252,
"_source": {
"Identity": "360501199001016214",
"Name": "陈乔恩",
"Age": 36,
"Birthday": "1980-05-09T14:12:12",
"Sex": false
}
}
]
}
}
bool 查询用于组合不同的查询条件,其中可以有以下参数:must、must_not、should、filter
must:相当于and,Must中的查询条件最终会做一个and操作
must_not:对每一个查询子句做取反操作。
should:相当于or,should中的所有的子句做or操作。
elasticsearch-查询的更多相关文章
- elasticsearch 查询(match和term)
elasticsearch 查询(match和term) es中的查询请求有两种方式,一种是简易版的查询,另外一种是使用JSON完整的请求体,叫做结构化查询(DSL). 由于DSL查询更为直观也更为简 ...
- Func<T,T>应用之Elasticsearch查询语句构造器的开发
前言 之前项目中做Elasticsearch相关开发的时候,虽然借助了第三方的组件PlainElastic.Net,但是由于当时不熟悉用法,而选择了自己拼接查询语句.例如: string queryG ...
- ElasticSearch查询 第五篇:布尔查询
布尔查询是最常用的组合查询,不仅将多个查询条件组合在一起,并且将查询的结果和结果的评分组合在一起.当查询条件是多个表达式的组合时,布尔查询非常有用,实际上,布尔查询把多个子查询组合(combine)成 ...
- 利用kibana插件对Elasticsearch查询
利用kibana插件对Elasticsearch查询 Elasticsearch是功能非常强大的搜索引擎,使用它的目的就是为了快速的查询到需要的数据. 查询分类: 基本查询:使用Elasticsear ...
- ElasticSearch查询 第四篇:匹配查询(Match)
<ElasticSearch查询>目录导航: ElasticSearch查询 第一篇:搜索API ElasticSearch查询 第二篇:文档更新 ElasticSearch查询 第三篇: ...
- elasticsearch查询语句总结
query 和 filter 的区别请看:https://www.cnblogs.com/bainianminguo/articles/10396956.html Filter DSL term 过 ...
- (转载)elasticsearch 查询(match和term)
原文地址:https://www.cnblogs.com/yjf512/p/4897294.html elasticsearch 查询(match和term) es中的查询请求有两种方式,一种是简易版 ...
- ElasticSearch查询 第三篇:词条查询
<ElasticSearch查询>目录导航: ElasticSearch查询 第一篇:搜索API ElasticSearch查询 第二篇:文档更新 ElasticSearch查询 第三篇: ...
- ElasticSearch查询 第二篇:文档更新
<ElasticSearch查询>目录导航: ElasticSearch查询 第一篇:搜索API ElasticSearch查询 第二篇:文档更新 ElasticSearch查询 第三篇: ...
- ElasticSearch查询 第一篇:搜索API
<ElasticSearch查询>目录导航: ElasticSearch查询 第一篇:搜索API ElasticSearch查询 第二篇:文档更新 ElasticSearch查询 第三篇: ...
随机推荐
- 签名 cd
http://blog.sina.com.cn/s/blog_618199e60101uc7w.html
- FastJSON 使用
FastJSON是一个Java语言编写的高性能,功能完善,完全支持http://json.org的标准的JSON库.多了不说了,百度一下一大把. 在此,简单的总结一下自己用过,测试过的方法. 如果使用 ...
- 阐述程序员如何实现IT职业定位
[动力IT职业生涯规划课]阐述程序员如何实现IT职业定位 2016年9月16日下午17:00,南通动力IT教育创始人阙海忠先生,亲自向动力IT学员传授其十八年IT从业经验. 一个程序员的完美人生定位 ...
- JSP开发模式2(计算器)
CalculatorBean———————————————————————————————————————————— public class CalculatorBean { private ...
- python积累
python积累 一.逐渐积累 python逐渐积累 http://www.cnblogs.com/lx63blog/articles/6051526.html python积累_2 http://w ...
- java 零碎知识点
1. 字符串有整型的相互转换 1 2 String a = String.valueOf(2); //integer to numeric string int i = Integer.pars ...
- html之如何让文字两端对齐
text-align: justify;/*英文*/ text-align-last: justify;/*中英文*/ text-align-last: justify;亲测有效(chrome)
- [OC笔记] Category分类之见解
用过别的语言做过开发的同学都知道,如果你想扩充一个类,就应该去继承这个类.但是OC里面有更好的方法,那就是分类. 那什么是分类呢?就是在不改变原先类,我们可以在其中添加咱们自定义的方法,这样和同事合作 ...
- 逐个访问URL的每个查询字符串参数
下面介绍一个函数,用于处理location.search的结果,以解析查询字符串,然后返回包含所有参数的一个对象. 比如 www.baidu.com?q=javascript&num=10 ...
- Zookeeper的基础知识
1.Zookeeper是什么? 引用官方的说法:“Zookeeper是一个高性能,分布式的,开源分布式应用协调服务.它提供了简单原始的功能,分布式应用可以基于它实现更高级 的服务.它被设计为易于编程, ...