elasticsearch核心知識--42.多搜尋條件組合查詢,sort以及explain的用法
阿新 • • 發佈:2019-01-29
分為三部分:
第一部分 組合條件查詢
GET /website/article/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "elasticsearch"
}
}
],
"should": [
{
"match": {
"content": "elasticsearch"
}
}
],
"must_not": [
{
"match": {
"author_id": 111
}
}
]
}
}
}
##########關於bool中採用filter range查詢############
bool下主要分為 query查詢類【must,must_not,should】和 filter類【filter】
每個子查詢都會計算一個document針對它的相關度分數,然後bool綜合所有分數,合併為一個分數,當然filter是不會計算分數的
{
"bool": {
"must": { "match": { "title": "how to make millions" }},
"must_not": { "match": { "tag": "spam" }},
"should": [
{ "match": { "tag": "starred" }}
],
"filter": {
"bool": {
"must": [
{ "range": { "date": { "gte": "2014-01-01" }}},
{ "range": { "price": { "lte": 29.99 }}}
],
"must_not": [
{ "term": { "category": "ebooks" }}
]
}
}
}
}
####單獨使用filter時,需要指定constant_score
GET /company/employee/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"age": {
"gte": 30
}
}
}
}
}
}
第二個方面 採用explain 檢查所些的語句是否正確,一般針對多行語法檢查
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]" }
第三方面:如何定製搜尋結果的排序規則 ,這裡如果時對特殊的string field欄位排序 可用查考上一篇
GET /company/employee/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"age": {
"gte": 30
}
}
}
}
},
"sort": [
{
"join_date": {
"order": "asc"
}
}
]
}