ElasticSearch筆記系列(9)——子條件查詢(上)Query context

上一篇對查詢按照簡單查詢、條件查詢、聚合查詢進行了分類,並逐一介紹了基本用法。正則表達式

本文對條件查詢再進行分類,介紹子條件查詢和複合條件查詢。json

 

條件查詢分類

子條件查詢:特定字段查詢 所指特定值。app

複合條件查詢:以必定的邏輯 組合 子條件查詢。ide

 

子條件查詢

子條件查詢又分爲:Query context 和 Filter Context。ui

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

Query context

Query context 經常使用的查詢包括:.net

全文本查詢:針對文本類型的數據code

字段級別的查詢:針對結構化的數據,好比數字、日期等orm

 

概念樹狀圖

說了這麼多概念,畫個樹吧:blog

 

全文本查詢

全文本查詢 包括:模糊匹配、習語匹配、多個字段的匹配查詢、語法的查詢。

下面,咱們在上一篇文章錄入的的索引和文檔的基礎上繼續進行查詢。具體的索引結構和文檔數據請翻看上一篇文章。

咱們建立了一個rent索引,一個community類型,以及對應的18個文檔。

示例數據是以前寫的文章裏的,這裏不在累述,我把索引定義給出:

{
"state": "open",
"settings": {
"index": {
"creation_date": "1523632189666",
"number_of_shards": "3",
"number_of_replicas": "1",
"uuid": "lUrZ_KHtQcuG33MFNivX-A",
"version": {
"created": "5060899"
},
"provided_name": "rent"
}
},
"mappings": {
"community": {
"properties": {
"creationdate": {
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
"type": "date"
},
"city": {
"type": "keyword"
},
"communityname": {
"type": "text"
},
"age": {
"type": "integer"
}
}
},
"shop": { }
},
"aliases": [ ],
"primary_terms": {
"0": 2,
"1": 2,
"2": 2
},
"in_sync_allocations": {
"0": [
"0RFsy8yXT6WM25oL8n5gew"
,
"QfWzYOCWQYy_QISycd-PNg"
],
"1": [
"MBaaq2YfQRmrR1hZx6qqhQ"
,
"iIxY5sN8Q3m6CR4Q5TxzXw"
],
"2": [
"vEbuzvxzTW2y_mokZWQSig"
,
"VhIOetKsSDqeOnG3HisH_w"
]
}
}

(本文出自oschina博主happybks的博文:https://my.oschina.net/happyBKs/blog/1799387)

模糊匹配

模糊匹配,使用match關鍵詞,對文本類型字段進行匹配時,會按照條件值中的各個字或者說分詞進行匹配。

POST http://localhost:9200/rent/community/_search

{
	"query":{
		"match":{
			"communityname":"中海萬錦城(二期)"
		}
	}
}

結果除了包含中海萬錦城(二期)、包括其餘communityname字段包含「中」「海」「萬」等字的文檔。

由於這是模糊匹配,徹底同樣的、包含中海的、包含一個萬字文檔,匹配的程度不一樣,_score的分數也不一樣。返回結果按照匹配度,即_score分數由高到低(可是_score大於0,若等於0表示徹底不匹配就不會被模糊查詢到)排列。

查詢結果

{
    "took": 253,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 11,
        "max_score": 10.104993,
        "hits": [
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZGp3aoILHeA4gRvWM",
                "_score": 10.104993,
                "_source": {
                    "communityname": "中海萬錦城(二期)",
                    "city": "上海閘北不夜城",
                    "age": 6,
                    "creationdate": "2012-01-01"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZGbCzoILHeA4gRvWL",
                "_score": 3.335333,
                "_source": {
                    "communityname": "中海悅府",
                    "city": "上海松江泗涇",
                    "age": 4,
                    "creationdate": "2014-01-01"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZHR2loILHeA4gRvWN",
                "_score": 2.0010364,
                "_source": {
                    "communityname": "中海紫御豪庭",
                    "city": "上海普陀長征",
                    "age": 6,
                    "creationdate": "2012-01-01"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZHsN6oILHeA4gRvWO",
                "_score": 1.6676666,
                "_source": {
                    "communityname": "象嶼鼎城",
                    "city": "上海浦東川沙",
                    "age": 5,
                    "creationdate": "2013-01-01"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZDcK7oILHeA4gRvWB",
                "_score": 1.2871116,
                "_source": {
                    "communityname": "萬科藍山",
                    "city": "上海浦東曹路",
                    "age": 14,
                    "creationdate": "2004-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZCaoSoILHeA4gRvV-",
                "_score": 0.91251767,
                "_source": {
                    "communityname": "萬科朗潤園",
                    "city": "上海",
                    "age": 12,
                    "creationdate": "2006-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZEXNioILHeA4gRvWD",
                "_score": 0.7981695,
                "_source": {
                    "communityname": "萬科白馬花園",
                    "city": "上海莘閔別墅區",
                    "age": 15,
                    "creationdate": "2003-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZC1HYoILHeA4gRvV_",
                "_score": 0.7981695,
                "_source": {
                    "communityname": "萬科優詩美地",
                    "city": "上海閔行七寶",
                    "age": 19,
                    "creationdate": "1999-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLY7wnkoILHeA4gRvV8",
                "_score": 0.36413702,
                "_source": {
                    "communityname": "萬科陽光苑",
                    "city": "上海",
                    "age": 10,
                    "creationdate": "2008-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZDK3-oILHeA4gRvWA",
                "_score": 0.36413702,
                "_source": {
                    "communityname": "萬科清林徑",
                    "city": "上海浦東新區新場鎮",
                    "age": 6,
                    "creationdate": "2012-01-01 00:00:00"
                }
            }
        ]
    }
}

 

習語匹配

若是你但願將查詢條件中的值做爲一個固定的習語進行匹配,而不是根據匹配程度進行模糊匹配,可使用習語匹配。關鍵詞是match_phrase

POST http://localhost:9200/rent/community/_search

{
	"query":{
		"match_phrase":{
			"communityname":"中海萬錦城(二期)"
		}
	}
}

查詢結果:

此時查詢結果咱們發現只一條了。

{
    "took": 568,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 10.104993,
        "hits": [
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZGp3aoILHeA4gRvWM",
                "_score": 10.104993,
                "_source": {
                    "communityname": "中海萬錦城(二期)",
                    "city": "上海閘北不夜城",
                    "age": 6,
                    "creationdate": "2012-01-01"
                }
            }
        ]
    }
}

 

 

多個字段的匹配查詢

多個字段的模糊匹配查詢 須要用到關鍵詞multi_match,而後裏面套query和fields關鍵詞,指明查詢條件文本和須要匹配哪些字段。

POST http://localhost:9200/rent/community/_search

{
	"query":{
		"multi_match":{
			"query":"山江",
			"fields":["communityname","city"]
		}
	}
}

查詢結果:

能夠看到小區名稱或者地址信息中包含「山」或「江」的小區文檔都被模糊匹配查詢到了。

{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 1.7792181,
        "hits": [
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZEy0foILHeA4gRvWE",
                "_score": 1.7792181,
                "_source": {
                    "communityname": "金地藝境(寶山)",
                    "city": "上海寶山區",
                    "age": 6,
                    "creationdate": "2012-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLDhHMpoILHeA4gRvV7",
                "_score": 1.7792181,
                "_source": {
                    "communityname": "世茂濱江花園",
                    "city": "上海",
                    "age": 9,
                    "creationdate": "2009-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZDcK7oILHeA4gRvWB",
                "_score": 1.2871116,
                "_source": {
                    "communityname": "萬科藍山",
                    "city": "上海浦東曹路",
                    "age": 14,
                    "creationdate": "2004-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZFA5EoILHeA4gRvWF",
                "_score": 1.0751344,
                "_source": {
                    "communityname": "金地藝境(松江)",
                    "city": "上海松江區",
                    "age": 3,
                    "creationdate": "2015-01-01 00:00:00"
                }
            }
        ]
    }
}

 

 

語法查詢

所謂語法查詢,就是根據必定的語法規則進行的查詢。常常在Kibana中用,是作數據搜索用的,支持通配符、範圍查詢、布爾查詢、正則表達式等。語法十分豐富。關鍵詞query_string。

邏輯與AND

有多個條件能夠用邏輯與,關鍵詞AND

POST http://localhost:9200/rent/community/_search

{
	"query":{
		"query_string":{
			"query":"萬科 AND 大道"
		}
	}
}

結果:

{
    "took": 386,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 2.9101574,
        "hits": [
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZD_9YoILHeA4gRvWC",
                "_score": 2.9101574,
                "_source": {
                    "communityname": "萬科公園大道",
                    "city": "上海老閔行",
                    "age": 3,
                    "creationdate": "2015-01-01 00:00:00"
                }
            }
        ]
    }
}

 

邏輯或OR

若是是邏輯或,關鍵詞OR。而且,語法條件能夠組合。

POST http://localhost:9200/rent/community/_search

{
	"query":{
		"query_string":{
			"query":"(萬科 AND 大道) OR 象嶼"
		}
	}
}

結果:

{
    "took": 324,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 4.0250216,
        "hits": [
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZHsN6oILHeA4gRvWO",
                "_score": 4.0250216,
                "_source": {
                    "communityname": "象嶼鼎城",
                    "city": "上海浦東川沙",
                    "age": 5,
                    "creationdate": "2013-01-01"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZD_9YoILHeA4gRvWC",
                "_score": 2.9101574,
                "_source": {
                    "communityname": "萬科公園大道",
                    "city": "上海老閔行",
                    "age": 3,
                    "creationdate": "2015-01-01 00:00:00"
                }
            }
        ]
    }
}

 

多個字段的語法匹配

這個和多字段模糊匹配時的關鍵詞同樣,須要用到fields關鍵詞,而後在列表裏把所涉及的字段名稱給出。

POST http://localhost:9200/rent/community/_search

{
	"query":{
		"query_string":{
			"query":"公園 OR 閔行",
			"fields":["communityname","city"]
		}
	}
}

查詢結果:

{
    "took": 53,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 2.1502688,
        "hits": [
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZD_9YoILHeA4gRvWC",
                "_score": 2.1502688,
                "_source": {
                    "communityname": "萬科公園大道",
                    "city": "上海老閔行",
                    "age": 3,
                    "creationdate": "2015-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZCaoSoILHeA4gRvV-",
                "_score": 1.1690899,
                "_source": {
                    "communityname": "萬科朗潤園",
                    "city": "上海",
                    "age": 12,
                    "creationdate": "2006-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZEXNioILHeA4gRvWD",
                "_score": 1.0225905,
                "_source": {
                    "communityname": "萬科白馬花園",
                    "city": "上海莘閔別墅區",
                    "age": 15,
                    "creationdate": "2003-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLDhHMpoILHeA4gRvV7",
                "_score": 1.0225905,
                "_source": {
                    "communityname": "世茂濱江花園",
                    "city": "上海",
                    "age": 9,
                    "creationdate": "2009-01-01 00:00:00"
                }
            }
        ]
    }
}

 

 

字段級別的查詢

字段級別的查詢,或者說結構化查詢,在query下的關鍵詞須要用term。

特定值查詢

好比查詢房齡爲10年的小區:

POST http://localhost:9200/rent/community/_search

{
	"query":{
		"term":{
			"age":10
		}
	}
}

結果:

{
    "took": 197,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLY7wnkoILHeA4gRvV8",
                "_score": 1,
                "_source": {
                    "communityname": "萬科陽光苑",
                    "city": "上海",
                    "age": 10,
                    "creationdate": "2008-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZF4FAoILHeA4gRvWI",
                "_score": 1,
                "_source": {
                    "communityname": "保利西子灣",
                    "city": "上海松江大學城",
                    "age": 10,
                    "creationdate": "2008-01-01"
                }
            }
        ]
    }
}

 

這時,咱們嘗試像以前模糊匹配的示例那樣在字段級別查詢裏查詢「萬科」

注意:關鍵詞是字段級別查詢的關鍵詞term。

POST http://localhost:9200/rent/community/_search

{
	"query":{
		"term":{
			"communityname":"萬科"
		}
	}
}

結果,發現查詢結果的數量是0。而以前用match作全文本查詢時,查詢「萬科」,會獲得一大推communityname的值中包含「萬」或「科」字的文檔。

{
    "took": 14,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}

那咱們在嘗試一下輸入完整的字段值。若是咱們將term中條件communityname字段的值設置爲一個徹底存在的值,例如:萬科公園大道

{
	"query":{
		"term":{
			"communityname": "萬科公園大道"
		}
	}
}

結果發現,依然沒有返回結果。由於commmunityname字段是text文本類型。不適用於字段級別的查詢。

若是咱們換成city字段,就沒有問題:

{
	"query":{
		"term":{
			"city": "上海老閔行"
		}
	}
}

結果,返回了對應的結果,由於city字段是keyword類型:

{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1.2039728,
        "hits": [
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZD_9YoILHeA4gRvWC",
                "_score": 1.2039728,
                "_source": {
                    "communityname": "萬科公園大道",
                    "city": "上海老閔行",
                    "age": 3,
                    "creationdate": "2015-01-01 00:00:00"
                }
            }
        ]
    }
}

 

範圍查詢

字段級別的查詢,還能夠查詢特定字段必定範圍值內的文檔有哪些。這種涉及字段值範圍的條件,須要用到關鍵詞range。範圍描述的關鍵詞與mongo中的十分相似。

關鍵詞 含義 數學符號
gt 大於 >   (
gte 大於等於 ≥    [
lt 小於 <   )
lte 小於等於 ≤   ]

咱們來查一下,房齡在5到7年(含5年,不含7年)的小區有哪些:age的字段類型是integer

{
	"query":{
		"range":{
			"age": {
				"gte":5,
				"lt":7
			}
		}
	}
}

查詢結果:

{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 6,
        "max_score": 1,
        "hits": [
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZDK3-oILHeA4gRvWA",
                "_score": 1,
                "_source": {
                    "communityname": "萬科清林徑",
                    "city": "上海浦東新區新場鎮",
                    "age": 6,
                    "creationdate": "2012-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZGp3aoILHeA4gRvWM",
                "_score": 1,
                "_source": {
                    "communityname": "中海萬錦城(二期)",
                    "city": "上海閘北不夜城",
                    "age": 6,
                    "creationdate": "2012-01-01"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZEy0foILHeA4gRvWE",
                "_score": 1,
                "_source": {
                    "communityname": "金地藝境(寶山)",
                    "city": "上海寶山區",
                    "age": 6,
                    "creationdate": "2012-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZHsN6oILHeA4gRvWO",
                "_score": 1,
                "_source": {
                    "communityname": "象嶼鼎城",
                    "city": "上海浦東川沙",
                    "age": 5,
                    "creationdate": "2013-01-01"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZFV2doILHeA4gRvWG",
                "_score": 1,
                "_source": {
                    "communityname": "金地藝華年",
                    "city": "上海浦東航頭",
                    "age": 5,
                    "creationdate": "2013-01-01"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZHR2loILHeA4gRvWN",
                "_score": 1,
                "_source": {
                    "communityname": "中海紫御豪庭",
                    "city": "上海普陀長征",
                    "age": 6,
                    "creationdate": "2012-01-01"
                }
            }
        ]
    }
}

 

日期字段範圍查詢

字段級別的範圍查詢同時還支持日期類型:

字段級別查詢:「看吧,全部類型我都支持,我就不支持你文本text類型,誰讓你和全文本類型打得火熱」)

POST http://localhost:9200/rent/community/_search

{
	"query":{
		"range":{
			"creationdate": {
				"gte":"2011-12-31",
				"lt":"2014-01-01"
			}
		}
	}
}

返回結果:

{
    "took": 358,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 6,
        "max_score": 1,
        "hits": [
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZDK3-oILHeA4gRvWA",
                "_score": 1,
                "_source": {
                    "communityname": "萬科清林徑",
                    "city": "上海浦東新區新場鎮",
                    "age": 6,
                    "creationdate": "2012-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZGp3aoILHeA4gRvWM",
                "_score": 1,
                "_source": {
                    "communityname": "中海萬錦城(二期)",
                    "city": "上海閘北不夜城",
                    "age": 6,
                    "creationdate": "2012-01-01"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZEy0foILHeA4gRvWE",
                "_score": 1,
                "_source": {
                    "communityname": "金地藝境(寶山)",
                    "city": "上海寶山區",
                    "age": 6,
                    "creationdate": "2012-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZHsN6oILHeA4gRvWO",
                "_score": 1,
                "_source": {
                    "communityname": "象嶼鼎城",
                    "city": "上海浦東川沙",
                    "age": 5,
                    "creationdate": "2013-01-01"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZFV2doILHeA4gRvWG",
                "_score": 1,
                "_source": {
                    "communityname": "金地藝華年",
                    "city": "上海浦東航頭",
                    "age": 5,
                    "creationdate": "2013-01-01"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZHR2loILHeA4gRvWN",
                "_score": 1,
                "_source": {
                    "communityname": "中海紫御豪庭",
                    "city": "上海普陀長征",
                    "age": 6,
                    "creationdate": "2012-01-01"
                }
            }
        ]
    }
}

 

若是要表示如今,可使用關鍵詞now。

好比查詢2015年至今的新小區:

POST http://localhost:9200/rent/community/_search

{
	"query":{
		"range":{
			"creationdate": {
				"gte":"2015-01-01",
				"lt":"now"
			}
		}
	}
}

結果:

{
    "took": 80,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 1,
        "hits": [
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZD_9YoILHeA4gRvWC",
                "_score": 1,
                "_source": {
                    "communityname": "萬科公園大道",
                    "city": "上海老閔行",
                    "age": 3,
                    "creationdate": "2015-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZFA5EoILHeA4gRvWF",
                "_score": 1,
                "_source": {
                    "communityname": "金地藝境(松江)",
                    "city": "上海松江區",
                    "age": 3,
                    "creationdate": "2015-01-01 00:00:00"
                }
            },
            {
                "_index": "rent",
                "_type": "community",
                "_id": "AWLZGGD2oILHeA4gRvWJ",
                "_score": 1,
                "_source": {
                    "communityname": "保利艾廬",
                    "city": "上海浦東新區周浦鎮",
                    "age": 3,
                    "creationdate": "2015-01-01"
                }
            }
        ]
    }
}
相關文章
相關標籤/搜索