Elasticsearch的高級查詢

#Elasticsearch的高級查詢緩存

子條件查詢

特定字段查詢指定特定的值url

Query context

在查詢的過程當中,除了判斷文檔是否知足條件以外,ES還會計算一個_score來標識匹配的程度,旨在判斷目標文檔和查詢條件匹配有多好。spa

經常使用查詢

全文本查詢

針對文本類型數據code

模糊匹配
url:http://localhost:9200/book/_doc/_search
請求體以下:
{
	"query":{
		"match":{
			"title":"Elasticsearch入門"
		}
	}
}
複製代碼

返回的結果以下:索引

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 0.3439677,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.3439677,
                "_source": {
                    "author": "王五",
                    "word_count": 2000,
                    "title": "Elasticsearch入門",
                    "publish_date": "2018-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "4",
                "_score": 0.3439677,
                "_source": {
                    "author": "趙六",
                    "word_count": 100000,
                    "title": "Elasticsearch進階",
                    "publish_date": "2017-11-11"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "10",
                "_score": 0.26592463,
                "_source": {
                    "author": "jack",
                    "word_count": 5000,
                    "title": "Elasticsearch的深刻理解",
                    "publish_date": "2018-05-09"
                }
            }
        ]
    }
}
複製代碼

模糊匹配的問題:模糊匹配會把相關詞的匹配的此都返回來了。ip

詞語匹配
url:http://localhost:9200/book/_doc/_search
請求體:{
	"query":{
		"match_phrase":{
			"title":"Elasticsearch入門"
		}
	}
}
複製代碼

返回結果:文檔

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.6459458,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "3",
                "_score": 2.6459458,
                "_source": {
                    "author": "王五",
                    "word_count": 2000,
                    "title": "Elasticsearch入門",
                    "publish_date": "2018-10-01"
                }
            }
        ]
    }
}
複製代碼

######多字段匹配(multi_match)string

url:http://localhost:9200/book/_doc/_search
請求體:
{
	"query":{
		"multi_match":{
			"query":"李啓明",
			"fields":["author","title"]
		}
	}
}
複製代碼

返回結果it

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 3.5740404,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "2",
                "_score": 3.5740404,
                "_source": {
                    "author": "李四",
                    "word_count": 2000,
                    "title": "李啓明的成長記",
                    "publish_date": "2018-06-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "9",
                "_score": 3.0162745,
                "_source": {
                    "author": "旺仔",
                    "word_count": 2000,
                    "title": "李啓明回憶錄",
                    "publish_date": "2017-06-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "8",
                "_score": 1.5404451,
                "_source": {
                    "author": "李啓明",
                    "word_count": 2000,
                    "title": "旺仔回憶錄",
                    "publish_date": "2017-06-02"
                }
            }
        ]
    }
}
複製代碼

######語法查詢(query_string)io

url:http://localhost:9200/book/_doc/_search
請求體:
{
	"query":{
		"query_string":{
			"query":"李啓明 OR Elasticsearch",
			"fields":["author","title"]
			
		}
	}
}
複製代碼

返回結果:

{
    "took": 16,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 3.5740404,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "2",
                "_score": 3.5740404,
                "_source": {
                    "author": "李四",
                    "word_count": 2000,
                    "title": "李啓明的成長記",
                    "publish_date": "2018-06-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "9",
                "_score": 3.0162745,
                "_source": {
                    "author": "旺仔",
                    "word_count": 2000,
                    "title": "李啓明回憶錄",
                    "publish_date": "2017-06-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "8",
                "_score": 1.5404451,
                "_source": {
                    "author": "李啓明",
                    "word_count": 2000,
                    "title": "旺仔回憶錄",
                    "publish_date": "2017-06-02"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.3439677,
                "_source": {
                    "author": "王五",
                    "word_count": 2000,
                    "title": "Elasticsearch入門",
                    "publish_date": "2018-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "4",
                "_score": 0.3439677,
                "_source": {
                    "author": "趙六",
                    "word_count": 100000,
                    "title": "Elasticsearch進階",
                    "publish_date": "2017-11-11"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "10",
                "_score": 0.26592463,
                "_source": {
                    "author": "jack",
                    "word_count": 5000,
                    "title": "Elasticsearch的深刻理解",
                    "publish_date": "2018-05-09"
                }
            }
        ]
    }
}
複製代碼
字段級別的查詢

針對結構化數據,如數字,日期等。

######範圍查詢

url:http://localhost:9200/book/_doc/_search
請求體:
{
	"query":{
		"range":{
			"word_count":{
				"gte":1000,
				"lte":2000
				
			}
		}
	}
}
複製代碼

返回結果:

{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "author": "李四",
                    "word_count": 2000,
                    "title": "李啓明的成長記",
                    "publish_date": "2018-06-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "author": "王五",
                    "word_count": 2000,
                    "title": "Elasticsearch入門",
                    "publish_date": "2018-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "author": "張三",
                    "word_count": 1000,
                    "title": "旺仔的成長記",
                    "publish_date": "2018-06-02"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "8",
                "_score": 1,
                "_source": {
                    "author": "李啓明",
                    "word_count": 2000,
                    "title": "旺仔回憶錄",
                    "publish_date": "2017-06-02"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "9",
                "_score": 1,
                "_source": {
                    "author": "旺仔",
                    "word_count": 2000,
                    "title": "李啓明回憶錄",
                    "publish_date": "2017-06-01"
                }
            }
        ]
    }
}
複製代碼

Filter context

在查詢過程種,只判斷該文檔是否知足條件,只有Yes或者No.filter須要結婚bool一塊兒使用,filter查詢因爲es會作緩存,索引在查詢速度上會快過query查詢

url:http://localhost:9200/book/_doc/_search
請求體:
{
"query":{
	"bool":{
		"filter":{
			"term":{
				"word_count":1000
			}
		}
		
	}
}
}
複製代碼

返回結果:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "1",
                "_score": 0,
                "_source": {
                    "author": "張三",
                    "word_count": 1000,
                    "title": "旺仔的成長記",
                    "publish_date": "2018-06-02"
                }
            }
        ]
    }
}
複製代碼

複合條件查詢

以必定的邏輯組合子條件查詢

固定分數查詢(只支持filter和bool查詢)

url:http://localhost:9200/book/_doc/_search
reuqest body:

{
	"query":{
		"constant_score":{
			"filter":{
				"match":{
					"title":"Elasticsearch"
				}
			},
			#能夠設置分數
			"boost":2
			
		}
	}
}
複製代碼

返回結果:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 2,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "3",
                "_score": 2,
                "_source": {
                    "author": "王五",
                    "word_count": 2000,
                    "title": "Elasticsearch入門",
                    "publish_date": "2018-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "4",
                "_score": 2,
                "_source": {
                    "author": "趙六",
                    "word_count": 100000,
                    "title": "Elasticsearch進階",
                    "publish_date": "2017-11-11"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "10",
                "_score": 2,
                "_source": {
                    "author": "jack",
                    "word_count": 5000,
                    "title": "Elasticsearch的深刻理解",
                    "publish_date": "2018-05-09"
                }
            }
        ]
    }
}
複製代碼

布爾查詢

{ "query":{ "bool":{ "must_not":{ "term":{ "title":"Elasticsearch" } } } } }

...more

{
	"query":{
		"bool":{
			"must_not":{
				"term":{
					"title":"Elasticsearch"
				}
			},
			"filter":[
				{
					"match":{
						"author":"李啓明"
					}
				}
				]
		}
	}
}
複製代碼

返回結果:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "8",
                "_score": 0,
                "_source": {
                    "author": "李啓明",
                    "word_count": 2000,
                    "title": "旺仔回憶錄",
                    "publish_date": "2017-06-02"
                }
            }
        ]
    }
}
複製代碼
相關文章
相關標籤/搜索