一、复杂查询
1.1、复杂条件搜索
# 复杂条件搜索
GET tindex/_doc/_search
{
"query":{
"match":{
"name": "wells"
}
}
}
1.2、指定输出字段
# 指定输出字段
GET /tindex/_doc/_search
{
"query": {
"match":{
"name": "wells"
}
},
"_source": ["name", "age"]
}
1.3、排序
# 排序
GET /tindex/_doc/_search
{
"query": {
"match": {
"name": "wells"
}
},
"_source": ["name", "age"],
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
1.4、分页查询
# 分页查询
GET /tindex/_doc/_search
{
"query": {
"match": {
"name": "wells"
}
},
"from": 0,
"size": 1
}
1.5、布尔值查询
1.5.1、must (and)
GET /tindex/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "wells"
}
},
{
"match": {
"age": 17
}
}
]
}
}
}
1.5.2、should (or)
# bool: should 查询 where name=wells or age = 17
GET /tindex/_doc/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "wells"
}
},
{
"match": {
"age": 17
}
}
]
}
}
}
1.5.3、must_not (not)
# bool: must_not 查询 where name != wells
GET /tindex/_doc/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"name": "wells"
}
},
{
"match": {
"name": "tom"
}
}
]
}
}
}
# 或者 bool: must_not 查询 where name != wells
GET /tindex/_doc/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
# 通过空格隔开,设置多个值
"name": "wells tom"
}
}
]
}
}
}
1.5.4、过滤器filter
# filter
GET /tindex/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "wells"
}
}
],
"filter": {
"range": {
"age": {
"lt": 30,
"gt": 18
}
}
}
}
}
}
1.6、匹配多个值
# 匹配多个值
GET /tindex/_doc/_search
{
"query": {
"match": {
"tags": "man 技术"
}
}
}
二、精确查询
两个类型:
- text:会被分词
- keyword:不会被分词
通过 _analyze 对词进行分析查看,text与keyword都是fields的类型,通过类型可以区分是否精确查询
# text 与 keyword
GET _analyze
{
"analyzer": "standard",
"text": "wells学es"
}
# text 与 keyword
GET _analyze
{
"analyzer": "keyword",
"text": "wells学es"
}
关于分词:
- term:查询是直接通过倒排索引指定的词条进行精确查找的,不会进行分词
- match:使用分词器进行解析,再进行查询
# term 与 match
GET /tindex/_doc/_search
{
"query": {
"match": {
"tags": "技术宅男"
}
}
}
# term 与 match
GET /tindex/_doc/_search
{
"query": {
"term": {
"tags": "技术宅男"
}
}
}
2.1、精确匹配多个值
2.2、高亮
2.2.1、默认高亮
高亮
GET /tindex/_doc/_search
{
"query": {
"match": {
"tags": "技术"
}
},
"highlight":{
"fields":{
"name":{}
}
}
}
2.2.2、自定义高亮
GET /tindex/_doc/_search
{
"query": {
"match": {
"tags": "技术"
}
},
"highlight":{
"pre_tags": "<p color='red'>",
"post_tags": "</p>",
"fields":{
"name":{}
}
}
}