如何正確的使用ElastchSearch

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)如何正確的使用ElastchSearch,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),大城企業(yè)網(wǎng)站建設(shè),大城品牌網(wǎng)站建設(shè),網(wǎng)站定制,大城網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,大城網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

1. 添加文檔

首次添加文檔時(shí),若索引不存在會(huì)自動(dòng)創(chuàng)建; 借助 kibana 的dev-tools來實(shí)現(xiàn) es 的交互

POST first-index/_doc
{
  "@timestamp": "2021-03-31T01:12:00",
  "message": "GET /search HTTP/1.1 200 1070000",
  "user": {
    "id": "YiHui",
    "name": "一灰灰Blog"
  },
  "addr": {
    "country": "cn",
    "province": "hubei",
    "city": "wuhan"
  },
  "age": 18
}

## 添加兩個(gè)數(shù)據(jù)進(jìn)行測(cè)試
POST first-index/_doc
{
  "@timestamp": "2021-03-31T02:12:00",
  "message": "GET /search HTTP/1.1 200 1070000",
  "user": {
    "id": "ErHui",
    "name": "二灰灰Blog"
  },
  "addr": {
    "country": "cn",
    "province": "hubei",
    "city": "wuhan"
  },
  "age": 19
}

當(dāng)然也可以直接使用 http 進(jìn)行交互,下面的方式和上面等價(jià)(后面都使用 kibanan 進(jìn)行交互,更直觀一點(diǎn))

curl  -X POST 'http://localhost:9200/first-index/_doc?pretty' -H 'Content-Type: application/json' -d '
{
  "@timestamp": "2021-03-31T01:12:00",
  "message": "GET /search HTTP/1.1 200 1070000",
  "user": {
    "id": "YiHui",
    "name": "一灰灰Blog"
  },
  "addr": {
    "country": "cn",
    "province": "hubei",
    "city": "wuhan"
  },
  "age": 18
}'

如何正確的使用ElastchSearch

2. 查詢文檔

2.0 kibana 配置并查詢

除了基礎(chǔ)的查詢語法之外,直接使用 kibana 進(jìn)行查詢,對(duì)于使用方而言,門檻最低;首先配置上面的 es 索引

  • Management -> Stack Management -> Kiabana Index Patterns

  • index pattern name

  • 時(shí)間字段,選擇 @timestamp 這個(gè)與實(shí)際的文檔中的 field 有關(guān)

如何正確的使用ElastchSearch

如何正確的使用ElastchSearch

如何正確的使用ElastchSearch

如何正確的使用ElastchSearch

接下來進(jìn)入Discover 進(jìn)行查詢

如何正確的使用ElastchSearch

比如字段查詢

如何正確的使用ElastchSearch

2.1 查詢所有

不加任何匹配,撈出文檔(當(dāng)數(shù)據(jù)量很多時(shí),當(dāng)然也不會(huì)真的全部返回,也是會(huì)做分頁的)

GET my-index/_search
{
  "query": {
    "match_all": {
    }
  }
}

如何正確的使用ElastchSearch

2.2 term 精確匹配

根據(jù) field 進(jìn)行 value 匹配,忽略大小寫;

查詢語法,形如: `{"query": {"term": {"成員名": {"value": "查詢值"}}}}

  • query, term, value 三個(gè) key 為固定值

  • 成員名: 為待查詢的成員

  • 查詢值: 需要匹配的值

(說明:后面語法中,中文的都是需要替換的,英文的為固定值)

GET first-index/_search
{
  "query": {
    "term": {
      "user.id": {
        "value": "yihui"
      }
    }
  }
}

如何正確的使用ElastchSearch

當(dāng) value 不匹配,或者查詢的 field 不存在,則查不到的對(duì)應(yīng)的信息,如

如何正確的使用ElastchSearch

2.3 terms 多值匹配

term 表示 value 的精確匹配,如果我希望類似value in (xxx)的查詢,則可以使用 terms

語法:

{
	"query": {
		"terms": {
			"成員名": [成員值, 成員值]
		}
	}
}

實(shí)例如

GET first-index/_search
{
  "query": {
    "terms": {
      "user.id": ["yihui", "erhui"]
    }
  }
}

如何正確的使用ElastchSearch

2.4 range 范圍匹配

適用于數(shù)值、日期的比較查詢,如常見的 >, >=, <, <=

查詢語法

{
	"query": {
        "range": {
            "成員名": {
                "gte": "查詢下界" ,
                "lte": "查詢下界"
            }
        }
	}
}
范圍操作符說明
gt大于 >
gte大于等于 >=
lt小于 <
lte小于等于 <=

實(shí)例如下

GET first-index/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 10,
        "lte": 18
      }
    }
  }
}

如何正確的使用ElastchSearch

2.5 字段過濾

根據(jù)是否包含某個(gè)字段來查詢, 主要有兩個(gè) exists 表示要求存在, missing表示要求不存在

查詢語法

{
    "query": {
        "exists/missing": {
            "field": "字段值"
        }
    }
}

實(shí)例如下

GET first-index/_search
{
  "query": {
    "exists": {
      "field": "age"
    }
  }
}

如何正確的使用ElastchSearch

2.6 組合查詢

上面都是單個(gè)查詢條件,單我們需要多個(gè)查詢條件組合使用時(shí),可以使用bool + must/must_not/should來實(shí)現(xiàn)

查詢語法

{
    "query": {
        "bool": {
            "must": [ # 相當(dāng)于and查詢
                "查詢條件1",
                "查詢條件2"
            ],
            "must_not": [ # 多個(gè)查詢條件相反匹配,相當(dāng)與not
                ...
            ],
            "should": [ # 有一個(gè)匹配即可, 相當(dāng)于or
                ...
            ]
        }
    }
}

實(shí)例如下

## user.id = yihui and age < 20
GET first-index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "user.id": {
              "value": "yihui"
            }
          }
        },
        {
          "range": {
            "age": {
              "lt": 20
            }
          }
        }
      ]
    }
  }
}

# !(user.id) = yihui and !(age>20)
GET first-index/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "user.id": {
              "value": "yihui"
            }
          }
        },
        {
          "range": {
            "age": {
              "gt": 20
            }
          }
        }
      ]
    }
  }
}

# user.id = 'yihui' or age>20
GET first-index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "user.id": {
              "value": "yihui"
            }
          }
        },
        {
          "range": {
            "age": {
              "gt": 20
            }
          }
        }
      ]
    }
  }
}

下面截圖以 must_not 輸出示意

如何正確的使用ElastchSearch

說明

  • 前面根據(jù)字段查詢 existing 只能單個(gè)匹配,可以借助這里的組合來實(shí)現(xiàn)多個(gè)的判斷

2.7 match 查詢

最大的特點(diǎn)是它更適用于模糊查詢,比如查詢某個(gè) field 中的字段匹配

語法

{
    "query": {
        "match": {
            "字段名": "查詢值"
        }
    }
}

舉例說明

GET first-index/_search
{
  "query": {
    "match": {
      "user.name": "灰og"
    }
  }
}

如何正確的使用ElastchSearch

說明,如果有精確查詢的需求,使用前面的 term,可以緩存結(jié)果

2.8 multi_match 查詢

更多相關(guān)信息,可以查看: 官網(wǎng)-multi_match 查詢

多個(gè)字段中進(jìn)行查詢

語法

  • type: best_fields 、 most_fieldscross_fields (最佳字段、多數(shù)字段、跨字段)

  • 最佳字段:當(dāng)搜索詞語具體概念的時(shí)候,比如 “brown fox” ,詞組比各自獨(dú)立的單詞更有意義

  • 多數(shù)字段:為了對(duì)相關(guān)度進(jìn)行微調(diào),常用的一個(gè)技術(shù)就是將相同的數(shù)據(jù)索引到不同的字段,它們各自具有獨(dú)立的分析鏈。

  • 混合字段:對(duì)于某些實(shí)體,我們需要在多個(gè)字段中確定其信息,單個(gè)字段都只能作為整體的一部分

{
    "query": {
        "multi_match": {
            "query":                "Quick brown fox",
            "type":                 "best_fields",
            "fields":               [ "title", "body" ],
            "tie_breaker":          0.3,
            "minimum_should_match": "30%"
        }
    }
}

實(shí)例演示

GET first-index/_search
{
  "query": {
    "multi_match": {
      "query": "漢",
      "fields": ["user.id", "addr.city"]
    }
  }
}

如何正確的使用ElastchSearch

上面除了寫上精確的字段之外,還支持模糊匹配,比如所有字段中進(jìn)行匹配

GET first-index/_search
{
  "query": {
    "multi_match": {
      "query": "blog",
      "fields": ["*"]
    }
  }
}
2.9 wildcard 查詢

shell 統(tǒng)配符

  • ?: 0/1 個(gè)字符

  • *: 0/n 個(gè)字符

GET first-index/_search
{
  "query": {
    "wildcard": {
      "user.id": {
        "value": "*Hu?"
      }
    }
  }
}

說明,對(duì)中文可能有問題

2.10 regexp 查詢

正則匹配

GET first-index/_search
{
  "query": {
    "regexp": {
      "user.name": ".*log"
    }
  }
}
2.11 prefix 查詢

前綴匹配

GET first-index/_search
{
  "query": {
    "prefix": {
      "user.name": "一"
    }
  }
}
2.12 排序

查詢結(jié)果排序,根據(jù) sort 來指定

{
	"sort": [
        {
          "成員變量": {
            "order": "desc"
          }
        }
  	]
}

實(shí)例如下

GET first-index/_search
{
  "query":{
    "match_all": {}
  },
  "sort": [
    {
      "@timestamp": {
        "order": "desc"
      }
    }
  ]
}
2.13 更多

更多操作姿勢(shì),可以在官方文檔上獲取

官方教程

3. 刪除文檔

需要根據(jù)文檔 id 進(jìn)行指定刪除

DELETE first-index/_doc/gPYLh4gBF9fSFsHNEe58

如何正確的使用ElastchSearch

刪除成功

如何正確的使用ElastchSearch

4.更新文檔

4.1 覆蓋更新

使用 PUT 來實(shí)現(xiàn)更新,同樣通過 id 進(jìn)行

  • 覆蓋更新

  • version 版本會(huì)+1

  • 如果 id 對(duì)應(yīng)的文檔不存在,則新增

PUT first-index/_doc/f_ZFhngBF9fSFsHNte7f
{
  "age": 28
}

如何正確的使用ElastchSearch

4.2 增量更新

采用 POST 來實(shí)現(xiàn)增量更新

  • field 存在,則更新

  • field 不存在,則新增

POST first-index/_update/gvarh4gBF9fSFsHNuO49
{
  "doc": {
    "age": 25
  }
}

如何正確的使用ElastchSearch

此外還可以采用 script 腳本更新

  • 在原來的 age 基礎(chǔ)上 + 5

POST first-index/_update/gvarh4gBF9fSFsHNuO49
{
  "script": "ctx._source.age += 5"
}

上述就是小編為大家分享的如何正確的使用ElastchSearch了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章題目:如何正確的使用ElastchSearch
鏈接地址:http://muchs.cn/article2/jpijoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、品牌網(wǎng)站制作服務(wù)器托管、搜索引擎優(yōu)化、品牌網(wǎng)站建設(shè)、域名注冊(cè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作