ElasticSearch 基础 2

================================== 高级查询 ===========================

========== 子条件查询 ===========

_score: 标记匹配程度, 旨在判断目标文档和查询条件匹配的有多好

POST http://127.0.0.1:9200/book/_search
{
"query": {
"match": {
"title": "ElasticSearch入门"
}
}
}
模糊匹配: 会把包含ElasticSearch和入门的关键词的文档数据都查询出来
{
"query": {
"match_phrase": {
"title": "ElasticSearch入门"
}
}
}
习语匹配: 精准查询
{
"query": {
"multi_match": {
"query": "瓦力",
"fileds": ["author", "title"]
}
}
}
multi_match: 多字段模糊匹配查询
author和title: 只要包含瓦力就查询出来

=========== 语法查询 =============
{
"query": {
"query_string": {
"query": "ElasticSearch AND 大法"
}
}
}

query_string: 代表语法查询
query: 指定查询内容
查询出同时含有ElasticSearch和大法
{
"query": {
"query_string": {
"query": "(ElasticSearch AND 大法) OR Python"
}
}
}
查询出同时含有ElasticSearch和大法, 或者含有Python的文档
{
"query": {
"query_string": {
"query": "瓦力 OR ElasticSearch",
"fields": ["title", "author"]
}
}
}
利用query_string 查询多个字段
title或者author 包含瓦力或者ElasticSearch 去不查询出来

=========== 结构化查询 =============
{
"query": {
"term": {
"word_count": 1000
}
}
}
term: 具体项
查询出word_count为1000 的文档
{
"query": {
"range": {
"word_count": {
"gte": 1000,
"lte": 2000
}
}
}
}
range: 范围查询
查询出word_count大于等于1000小于等于2000的文档数据
日期也可以比较大小 可以用now关键词

============= filter 查询 ============

只判断文档是否满足条件 只有yes活着no
{
"query": {
"bool": {
"filter": {
"term": {
"word_count": 1000
}
}
}
}
}
word_count为1000的文档才会返回
filter: 过滤, es会对结果进行缓存,相对query较快,需要结合bool来使用

============= 复合条件查询 结合query和filter ==============

============= 固定分数查询 =========
POST http://127.0.0.1:9200/_search
{
"query": {
"match":{
"title":"ElasticSearch"
}
}
}
_score:分数不一样 es回个模糊匹配一个分数

{
"query": {
"constant_score":{
"filter":{
"match":{
"title": "ElasticSearch"
}
}
}
}
}
constant_score: 固定分数查询
刚刚的书籍都出来了, 但是_score都是1
{
"query": {
"constant_score":{
"filter":{
"match":{
"title": "ElasticSearch"
}
},
"boost": 2
}
}
}
boost:指定分数为2, es会做一下缓存

固定分数查询: 不支持match 支持filter

====== bool 查询 ======
{
"query":{
"bool":{
"should": [
{
"match":{
"author": "瓦力"
}

},
{
"match":{
"title": "ElasticSearch"
}
}
]
}
}
}

should:应当满足,者的关系,只要满足其中一个就可以
{
"query":{
"bool":{
"must": [
{
"match":{
"author": "瓦力"
}

},
{
"match":{
"title": "ElasticSearch"
}
}
]
}
}
}

must: 必须同时满足才返回
must_not: 排除

{
"query":{
"bool":{
"must": [
{
"match":{
"author": "瓦力"
}

},
{
"match":{
"title": "ElasticSearch"
}
}
],
"filter":[
{
"term":{
"word_count": 1000
}
}
]
}
}
}
must filter 综合运用
原文地址:https://www.cnblogs.com/zhangboblogs/p/9826623.html