html網(wǎng)站開發(fā)工具有哪些互聯(lián)網(wǎng)登錄的網(wǎng)站名
ES中的查詢操作分為兩種:查詢和過濾。查詢即是之前提到的query查詢,它默認(rèn)會計(jì)算每個(gè)返回文檔的得分,然后根據(jù)得分排序。而過濾只會篩選出符合條件的文檔,并不計(jì)算得分,并且可以緩沖記錄。所以我們在大范圍篩選數(shù)據(jù)時(shí),應(yīng)先使用過濾操作過濾數(shù)據(jù),然后使用查詢匹配數(shù)據(jù)。
1.使用
1.1初始化創(chuàng)建商品索引
#創(chuàng)建商品索引
#id,title,price,created_at,description
PUT /products
{
? "settings": {
? ? "number_of_shards": 1,?
? ? "number_of_replicas": 0
? },
? "mappings": {
? ? "properties": {
? ? ? ? "id":{
? ? ? ? ? "type":"integer"
? ? ? ? },
? ? ? ? "title":{
? ? ? ? ? "type":"keyword"
? ? ? ? },
? ? ? ? "price":{
? ? ? ? ? "type":"double"
? ? ? ? },
? ? ? ? "created_at":{
? ? ? ? ? "type":"date"
? ? ? ? },
? ? ? ? "description":{
? ? ? ? ? "type":"text",
? ? ? ? ? "analyzer": "ik_max_word" #使用ik分詞器
? ? ? ? }
? ? }
? }
}?
1.2插入數(shù)據(jù)
POST /products/_doc/1
{
? "id":1,
? "title":"庫迪咖啡",
? "price":"10.5",
? "created_at":"2024-11-28",
? "description":"庫迪咖啡確實(shí)不錯(cuò)"
}
POST /products/_doc/2
{
? "id":2,
? "title":"瑞星咖啡",
? "price":"9.8",
? "created_at":"2023-11-18",
? "description":"瑞星咖啡我最愛了,好喝"
}
POST /products/_doc/3
{
? "id":3,
? "title":"星巴克",
? "price":"14.5",
? "created_at":"2024-11-18",
? "description":"太苦了,咖啡不好喝"
}?
1.3過濾類型——term
GET products/_search
{
? "query": {
? ? "bool": {
? ? ? "must": [
? ? ? ? {
? ? ? ? ? "term": {
? ? ? ? ? ? "description": {
? ? ? ? ? ? ? "value": "咖啡"
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? }
? ? ? ],
? ? ? "filter": [
? ? ? ? {
? ? ? ? ? "term": {
? ? ? ? ? ? "description": "瑞星"
? ? ? ? ? }
? ? ? ? }
? ? ? ]
? ? }
? }
}?
?
1.4過濾類型——terms
GET products/_search
{
? "query": {
? ? "bool": {
? ? ? "must": [
? ? ? ? {
? ? ? ? ? "match_all": {}
? ? ? ? }
? ? ? ],
? ? ? "filter": [
? ? ? ? {
? ? ? ? ? "terms": {
? ? ? ? ? ? "description": [
? ? ? ? ? ? ? "瑞星",
? ? ? ? ? ? ? "好喝"
? ? ? ? ? ? ]
? ? ? ? ? }
? ? ? ? }
? ? ? ]
? ? }
? }
}
?
1.5過濾類型——range
GET products/_search
{
? "query": {
? ? "bool": {
? ? ? "must": [
? ? ? ? {
? ? ? ? ? "match_all": {}
? ? ? ? }
? ? ? ],
? ? ? "filter": [
? ? ? ? {
? ? ? ? ? "range": {
? ? ? ? ? ? "price": {
? ? ? ? ? ? ? "gte": 10,
? ? ? ? ? ? ? "lte": 20
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? }
? ? ? ]
? ? }
? }
}?
1.6過濾類型——exists
GET products/_search
{
? "query": {
? ? "bool": {
? ? ? "must": [
? ? ? ? {
? ? ? ? ? "match_all": {}
? ? ? ? }
? ? ? ],
? ? ? "filter": [
? ? ? ? {
? ? ? ? ? "exists": {
? ? ? ? ? ? "field": "title" ?#過濾出帶某個(gè)字段的數(shù)據(jù),比如先拿到有title字段的數(shù)據(jù)
? ? ? ? ? }
? ? ? ? }
? ? ? ]
? ? }
? }
}?
1.7過濾類型——ids
GET products/_search
{
? "query": {
? ? "bool": {
? ? ? "must": [
? ? ? ? {
? ? ? ? ? "term": {
? ? ? ? ? ? "description": {
? ? ? ? ? ? ? "value": "好喝"
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? }
? ? ? ],
? ? ? "filter": [
? ? ? ? {
? ? ? ? ? "ids": { ? ? ? ? ?#根據(jù)數(shù)據(jù)id過濾出在ids里面的數(shù)據(jù)
? ? ? ? ? ? "values": [
? ? ? ? ? ? ? "1",
? ? ? ? ? ? ? "2"
? ? ? ? ? ? ]
? ? ? ? ? }
? ? ? ? }
? ? ? ]
? ? }
? }
}?