基本語法
{
QUERY_NAME:{
ARGUMENT:VALUE,
ARGUMENT:VALUE,
...
}
}
{
QUERY_NAME:{
FIELD_NAME:{
ARGUMENT:VALUE,
ARGUMENT:VALUE,
...
}
}
}
示例
GET /_search //查詢所有的,默認(rèn)只會顯示10條document
{
"query":{
"match_all": {}
}
}
GET /test_index/test_type/_search //查詢字段“test_field”疮薇,包含“test”
{
"query": {
"match": {
"test_field": "test"
}
}
}
組合搜索多個條件
//模擬數(shù)據(jù)
PUT /website/article/1
{
"title":"my elasticsearch article",
"content":"es is very good",
"author_id":110
}
PUT /website/article/2
{
"title":"my hadoop article",
"content":"hadoop is very bad",
"author_id":111
}
PUT /website/article/3
{
"title":"my elasticsearch article",
"content":"es is very bad",
"author_id":112
}
//查詢 title必須包含elasticsearch劝堪,content可以不包含elasticsearch恬砂,author_id必須不為110
GET /website/article/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "elasticsearch"
}
}
],
"should": [
{
"match": {
"content":"elasticsearch"
}
}
],
"must_not": [
{
"match": {
"author_id": 110
}
}
]
}
}
}
//查詢title,content包含elasticsearch
GET /website/article/_search
{
"query": {
"multi_match": {
"query": "elasticsearch",
"fields": ["title","content"]
}
}
}
//查詢title,content必須包含elasticsearch芥颈,并且author_id必須不為110的
GET /website/article/_search
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "elasticsearch",
"fields": ["title","content"]
}
}
],
"must_not": [
{
"match": {
"author_id": 110
}
}
]
}
}
}
定位不合法的搜索以及原因
//以這樣的語法憋槐,來判斷一個查詢json串是否合法
GET /test_index/test_type/_validate/query?explain
{
"query":{
"math":{
"test_field":"test"
}
}
}
//出錯返回值
{
"valid": false,
"error": "org.elasticsearch.common.ParsingException: no [query] registered for [math]"
}
//正確返回值
{
"valid": true,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"explanations": [
{
"index": "test_index",
"valid": true,
"explanation": "+test_field:test #(#_type:test_type)"
}
]
}
filter與query的對比
//模擬數(shù)據(jù)
PUT /company/employee/1
{
"address":{
"country":"china",
"provice":"beijing",
"city":"beijing"
},
"name":"jack",
"age":28,
"join_date":"2017-01-01"
}
PUT /company/employee/2
{
"address":{
"country":"china",
"provice":"jiangsu",
"city":"nanjing"
},
"name":"tom",
"age":30,
"join_date":"2016-01-01"
}
PUT /company/employee/3
{
"address":{
"country":"china",
"provice":"shanxi",
"city":"xian"
},
"name":"marry",
"age":35,
"join_date":"2015-01-01"
}
//查詢年齡大于等于30姚建,同時join_date必須是2016-01-01
GET /company/employee/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"join_date": "2016-01-01"
}
}
],
"filter": {
"range": {
"age": {
"gte": 30
}
}
}
}
}
}
filter與query的對比
- filter呐舔,僅僅只是按照搜索條件過濾出需要的數(shù)據(jù)而已币励,不計算任何相關(guān)度分?jǐn)?shù),對相關(guān)度沒有任何影響
- query珊拼,會去計算每個document相對于搜索條件的相關(guān)度食呻,并按照相關(guān)度進(jìn)行排序
- 一般來說,如果是在進(jìn)行搜素,需要將最匹配搜索條件的數(shù)據(jù)先返回仅胞,那么用query每辟;如果只是要根據(jù)一些條件篩選出一部分?jǐn)?shù)據(jù),不關(guān)注其排序干旧,那么用filter
- 如果你希望越符合這些搜索條件的document越排在前面返回渠欺,那么這些搜索條件就要放在query中;如果不希望一些搜索條件來影響你的document排序椎眯,那么就放在filter中即可
- filter不需要計算相關(guān)度分?jǐn)?shù)挠将,不需要按照相關(guān)度分?jǐn)?shù)進(jìn)行排序,同時還有內(nèi)置的自動cache最常使用filter的數(shù)據(jù)编整;query則相反捐名,要計算相關(guān)度分?jǐn)?shù),還需要按照分?jǐn)?shù)排序闹击,而且無法cache結(jié)果。所以成艘,filter的速度會更快些