Press "Enter" to skip to content

ElasticSearch(四)【高级查询】

本站内容均来自兴趣收集,如不慎侵害的您的相关权益,请留言告知,我们将尽快删除.谢谢.

 

四、高级查询

 

上一篇文章《ElasticSearch – 索引、映射、文档

 

说明

 

ES中提供了一种强大的检索数据方式,这种检索方式称之为Query DSL
,Query DSL是利用Rest API传递JSON格式的请求体(Request Body)数据
与ES进行交互,这种方式的丰富查询语法让ES检索变得更强大,更简洁

 

语法

 

### _doc可选,查询效果都一样
# GET /索引名/_doc/_search {
 json格式请求体数据}
# GET /索引名/_search {
 json格式请求体数据}

 

4.1 分词与不分词查询

 

查询所有文档记录

 

### match_all查询文档中所有记录
GET /product/_doc/_search
{
 
  "query": {
 
    "match_all": {
 }
  }
}

 

 

 

term关键词分词查询

 

### 关键词分词查询 term
GET /product/_doc/_search
{
 
  "query": {
 
    "term": {
 
      "description": {
 
        "value": "blog"
      }
    }
  }
}
【注意】
映射类型说明
- integer: 搜索时不分词
- double: 搜索时不分词
- date: 搜索时不分词
- keyword: 搜索时需要输入全部内容,不分词,就像SQL当中的eq
- text: 中文单字分词(一个字一个字进行分隔),英文单词分词(me、like、you...)

 

 

 

小结:

在ES中的Mapping Type
中keyword、date、integer、long、double、boolean、这些类型不分词,只有text类型分词
通过使用term查询得知,es默认使用标准分词器
(StandardAnalyzer),中文单字分词,英文单词分词

4.2 类型关键字查询

 

range关键字范围查询

 

### 范围查询 rangegte ===> 大于等于 lte ===> 小于等于
GET /product/_doc/_search
{
 
  "query": {
 
    "range": {
 
      "price": {
 
        "gte": 0,
        "lte": 5
      }
    }
  }
}

 

prefix关键字前缀查询

 

### 前缀查询 prefix
GET /product/_doc/_search
{
 
  "query": {
 
    "prefix": {
 
      "title": {
 
        "value": "vin"
      }
    }
  }
}

 

wildcard关键字通配符查询

 

### 通配符查询 wildcard
# ?用来匹配一个任意字符
# *用来匹配多个任意字符
GET /product/_doc/_search
{
 
  "query": {
 
    "wildcard": {
 
      "description": {
 
        "value": "*blog"
      }
    }
  }
}

 

ids关键字多id查询

 

### 多id查询 ids
GET /product/_doc/_search
{
 
  "query": {
 
    "ids": {
 
      "values": [1,2,3]
    }
  }
}

 

fuzzy关键字模糊查询

 

### 模糊查询 fuzzy
GET /product/_doc/_search
{
 
  "query": {
 
    "fuzzy": {
 
      "description": "blog"
    }
  }
}
【注意】
fuzzy 模糊查询,最大模糊错误必须在[0,2]之间
- 搜索关键字长度为2不允许存在模糊
- 搜索关键字长度为3-5允许一次模糊
- 搜索关键字长度大于5允许最大两次模糊

 

bool关键字布尔查询

 

### 布尔查询 bool
GET /product/_doc/_search
{
 
  "query": {
 
    "bool": {
 
      "must": [
        {
 
          "term": {
 
            "price": {
 
              "value": 1.7
            }
          }
        }
        ]
    }
  }
}
GET /product/_doc/_search
{
 
  "query": {
 
    "bool": {
 
      "must": [
        {
 
          "ids": {
 
            "values": [1,2,3,4,5,6,7]
          }
        }
        ]
    }
  }
}
【注意】
bool可以用来组合多个条件实现复杂查询
- must: 相当于&&同时成立
- should: 相当于||成立一个就行
- must_not: 相当于!非,不能满足任何一个

 

multi_match关键字多字段查询

 

### 多字段查询 multi_match
GET /product/_doc/_search
{
 
  "query": {
 
    "multi_match": {
 
      "query": "vinjcent",
      "fields": ["title","description"]
    }
  }
}
【注意】
先根据mapping字段类型分词("title"、"description"),将查询条件分词之后进行查询该字段(中文单字分词、英文单词分词),如果该字段不分词就会将查询条件作为整体进行查询

 

query_string关键字默认字段查询

 

### 默认字段查询 query_string
# title为keyword类型,不分词(需要全部输入内容符合)
GET /product/_doc/_search
{
 
  "query": {
 
    "query_string": {
 
      "default_field": "title",
      "query": "vinjcent"
    }
  }
}
# description为text类型,分词(根据部分输入内容)
GET /product/_doc/_search
{
 
  "query": {
 
    "query_string": {
 
      "default_field": "description",
      "query": "blog"
    }
  }
}
【注意】
根据字段的类型,将查询条件进行分词后进行查询

 

highlight关键字高亮查询

 

### 高亮查询 highlight
# 例如百度搜索spring
GET /product/_doc/_search
{
 
  "query": {
 
    "query_string": {
 
      "default_field": "description",
      "query": "blog"
    }
  },
  "highlight": {
 
    "fields": {
 
      "*": {
 }
    }
  }
}
【注意】
可以让符合条件的文档中的关键词高亮,高亮并没有修改原始的文档,只是单独将高亮结果单独拿出来
# 自定义高亮标签tags,开启多个字段高亮require_field_match
GET /product/_doc/_search
{
 
  "query": {
 
    "query_string": {
 
      "default_field": "description",
      "query": "blog"
    }
  },
  "highlight": {
 
    "pre_tags": ["<span style='color:red;'>"],
    "post_tags": ["</span>"],
    "require_field_match": "false",
    "fields": {
 
      "*": {
 }
    }
  }
}

 

 

 

 

 

size关键字条数查询

 

### 条数查询 size
GET /product/_doc/_search
{
 
  "query": {
 
    "match_all": {
 }
  },
  "size": 2
}
【注意】
指定查询结果中返回指定条数目.默认返回值10条

 

from关键字分页查询

 

### 分页查询 from size
GET /product/_doc/_search
{
 
  "query": {
 
    "match_all": {
 }
  },
  "from": 0,
  "size": 2
}
【注意】
用来指定起始返回位置,和size关键字连用可实现分页效果

 

sort关键字排序

 

### 排序 sort
GET /product/_doc/_search
{
 
  "query": {
 
    "match_all": {
 }
  },
  "from": 0,
  "size": 2,
  "sort": [
      {
 
        "id": {
 
          "order": "desc"
        }
      }
    ]
}
【注意】
desc/asc

 

_source关键字返回指定字段

 

### 返回指定字段 _source
GET /product/_doc/_search
{
 
  "query": {
 
    "match_all": {
 }
  },
  "from": 0,
  "size": 2,
  "sort": [
      {
 
        "id": {
 
          "order": "desc"
        }
      }
    ],
  "_source": ["id","title","description"]
}
【注意】
是一个数组,在数组中用来指定展示哪些字段

 

 

Be First to Comment

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注