es基础-查询

一,simple string query和string query

string query 中query 字符串中可以使用 and or not等逻辑连接词,simple string query 中不行,会将其作为普通字符串处理。

在simple string query中可以指定"default_operator"指定逻辑运算符,也可以字query 字符串中用特殊符号表示逻辑运算关系,如 “beautiful +mind” 表示"beautiful and mind"。

# 可以在query字符串中指定查询字段,AND要全大写,default_field可以省略
GET movies/_search
{
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "title:Beautiful AND Mind"
    }
  }
}
# "query": "Beautiful +Mind",
GET movies/_search
{
   "query": {
     "simple_query_string": {
       "query": "Beautiful -Mind",
       "fields": ["title"]
     }
   }
}

一,聚合查询

1,bucket

类似于sql的group

2,metric

类似于sql的count

POST kibana_sample_data_flights/_search
{
  "aggs": {
      "ml": {
        "terms": {
          "field":"DestCountry"
        },
        "aggs": {
          "ag_et": {
            "avg": {
              "field":"AvgTicketPrice"
            }
          },
          "max_t":{
             "max":{
                "field":"AvgTicketPrice"
             }
          }
      }
    }
  }
}

二,结构化查询

对数字、时间、数据字典式文本的查询称之为结构化查询。

1,term查询

termd查询,对查询条件不分词,对关键词进行模糊查询

POST lcy_test/_search
{
  "query": {
    "term": {
      "name": {
        "value": "lcy"
      }
    }
  }
}

不算分filter查询,提高效率
filter缓存原理bitset

POST lcy_test/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "name": "lcy"
        }
      },
      "boost": 1.2
    }
  }
}

2,range查询

数值的range查询。

POST kibana_sample_data_ecommerce/_search
{
  "query": {
    "range": {
      "taxful_total_price": {
        "gte": 10,
        "lte": 20
      }
    }
  },
  "from":1,
  "size": 2
}

时间的range查询。

POST kibana_sample_data_ecommerce/_search
{
  "query": {
    "range": {
      "order_date": {
        "gte": "now-1M"
      }
    }
  },
  "from":1,
  "size": 2
}

exists查询。

// exists查询
POST kibana_sample_data_ecommerce/_search
{
  "query": {
    "exists": {
      "field": "name"
    }
  }
}

多值查询。用关键字去查一个有多个值的字段时,只用多值字段包含关键字,就能匹配成功。

三,全文搜索

先分词,在查询,再算分

四,bool查询

POST blog/_bulk
{"index":{"_id":1}}
{"content":"Apple employee loves apple juice and apple pie"}
{"index":{"_id":2}}
{"content":"apple mac"}
{"index":{"_id":3}}
{"content":"apple phone and apple pad"}

bool查询

GET blog/_search
POST blog/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "_id": {
              "value": "1"
            }
          }
        }
      ]
    }
  }
}

must 和must not查询

POST blog/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "apple"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "content": "pie"
          }
        }
      ]
    }
  }
}

bool boosting查询,调整算分参数

POST blog/_search
{
  "query": {
    "boosting": {
      "positive": {
        "match": {
          "content": "apple"
        }
      },
      "negative": {
        "match": {
          "content": "pie"
        }
      },
      "negative_boost": 0.2
    }
  }
}

单字段多值查询 disjuncition max query 、 tie breaker

// disjunction query
POST blog/_search
{
  "query": {
    "dis_max": {
      "queries": [
         {"match":{
           "title":"cat love"
         }
           
         } ,
         {"match":{
           "title":"cat love"
         }
         } 
      ],
      "tie_breaker": 0.7
    }
  }
}

多字段单值查询 disjuncition max query 、 tie breaker

// disjunction query
POST blog/_search
{
  "query": {
    "dis_max": {
      "queries": [
         {"match":{
           "title":"cat love"
         }
           
         } ,
         {"match":{
           "title":"cat love"
         }
         } 
      ],
      "tie_breaker": 0.7
    }
  }
}

多字段单值查询 multi query

multi query 分为best_fields,most_fields,cross_fields

POST blog/_search
{
  "explain": true, 
    "query": {
      "multi_match": {
        "query": "love cat",
        "type": "best_fields", 
        "fields": ["title","content"]
      }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小手追梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值