帶條件查詢
GET test_mapping_manual1/_search?q=name:hello
GET test_mapping_manual1/_search?from=0&size=3&sort=age:asc
嘗試了text類型排序需要特別處理下. "reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [name] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
這種查詢方式是創(chuàng)建索引的字段都會被檢索
GET test_mapping_manual1/_search?q=hell
關(guān)閉字段索引,在mapping中設(shè)置
"cloName": {
"type": "text",
"index": false
}
全文檢索 Fulltext search
match
GET test_mapping_manual1/_search
{
"query": {
"match": {
"name": "John Smith"
}
}
}
# name包含John或者Smith都會被檢索出來即這里對搜索詞做了分詞,且對source data也做了分詞
這種把條件放到j(luò)son體中
GET test_mapping_manual1/_search
{
"query": {
"match": {
"name": "keyword1 keyword2 keyword2"
}
}
}
# 上邊查詢的含義是任何包含關(guān)鍵字 keyword1 或者 keyword2 或者keyword3都會查詢出來
# 前提是設(shè)置好分詞器,如果中文使用英文分詞器可能會存在問題
# 查詢結(jié)構(gòu)順序按照_score進(jìn)行排序,內(nèi)部排序算法是bm25
查詢結(jié)果會統(tǒng)計(jì)命中幾個關(guān)鍵字
match_all
# 查詢所有全部數(shù)據(jù)
GET test_mapping_manual1/_search
{
"query": {
"match_all": {
}
}
}
# 等同于
GET test_mapping_manual1/_search
# 查詢集群所有數(shù)據(jù)
GET _search
multi_match
# 這段含義是在字段name和desc中查找包含"John或者IT的內(nèi)容的
GET test_mapping_manual1/_search
{
"query": {
"multi_match": {
"query": "John IT",
"fields": ["name","desc"]
}
}
}
# 語義: 默認(rèn)分詞器的對`John Smith`的分詞結(jié)果為`John`和`Smith`
# name.last 中包含 `John` 或者 `Smith`
# OR
# name.first 中包含 `John` 或者 `Smith`
# 如果設(shè)置了"operator": "and",則中間 OR 的關(guān)系變?yōu)?AND
GET teacher/_search
{
"query": {
"multi_match": {
"query": "John Smith",
"type": "most_fields",
"fields": [
"name.last",
"name.first"
]
// ,"operator": "and"
}
}
}
match_phrase
短語搜索
短語搜索內(nèi)部原理是先分詞,分詞之后需要命中每個分詞,并且順序要一直
# 這段含義先把name查詢條件分詞為John 和Tim
# 然后去source中把name字段的內(nèi)容分詞
# 分詞之后需要同時命中John 和Tim并且順序要一致
GET test_mapping_manual1/_search
{
"query": {
"match_phrase": {
"name": "John Tim"
}
}
}
驗(yàn)證分詞
GET _analyze
{
"analyzer": "standard",
"text": "John Sam desc IT"
}
exact match
使用關(guān)鍵字term
term不會對搜索詞分詞
記住分詞分兩個部分,搜索詞分詞和源數(shù)據(jù)分詞
keyword是不對source data的值分詞
match_phase是分詞的
所以重點(diǎn)來了:文章來源:http://www.zghlxwxcb.cn/news/detail-578602.html
如果搜索詞部分詞,但是source data內(nèi)容分詞了,這時是無法查詢數(shù)據(jù)的,因?yàn)榻ㄋ饕臅r候索引已經(jīng)分詞了; 另外還需要注意一點(diǎn)一個字段要被搜索到一定要被索引到文章來源地址http://www.zghlxwxcb.cn/news/detail-578602.html
GET test_mapping_manual1/_search
{
"query": {
"term": {
"name": "John Smith"
}
}
}
# 測試數(shù)據(jù)1
POST test_mapping_manual1/_doc
{
"name": "John Smith",
"age": 18,
"desc": "IT phone"
}
# 此案例無法查詢出數(shù)據(jù)
到了這里,關(guān)于Elasticsearch ES 簡單查詢 Query String Search 入門的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!