Kibana 全文檢索操作

HuDu發表於2021-07-08
GET product/_search?q=*&sort=price:desc

GET product/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 2, 
  "_source": ["id","title"]
}


# match 模糊查詢,會對檢索條件進行分詞匹配
GET product/_search
{
  "query": {
    "match": {
      "title": "小米華為"
    }
  }
}

GET product/_search
{
  "query": {
    "match": {
      "price": "2999"
    }
  }
}

# match_phrase 短語匹配,檢索條件為一個整體,不會被分詞

GET product/_search
{
  "query": {
    "match_phrase": {
      "title": "小米華為"
    }
  }
}

# multi_match 多欄位匹配 只要指定的欄位中包含搜尋關鍵詞,查詢條件會被分詞進行查詢

GET product/_search
{
  "query": {
    "multi_match": {
      "query": "小米華為",
      "fields": ["title","category"]
    }
  }
}

# 複合查詢 must must_not should 具體使用自行查詢

GET product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
          "title": "小米"
          }
        },
        {
          "match": {
            "price": "3999"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "category": "小米"
          }
        }
      ],
      "should": [
        {
          "match": {
            "title": "一加"
          }
        }
      ]
    }
  }
}

# filter 結果過濾,但是 filter 不會貢獻文件相關性得分,可用作最後結果的過濾

GET product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "price": {
              "gte": 3999,
              "lte": 6999
            }
          }
        }
      ]
    }
  }
}

GET product/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "price": {
              "gte": 3999,
              "lte": 5999
            }
          }
        }
      ]
    }
  }
}

# term 推薦查詢精確欄位時使用

GET product/_search
{
  "query": {
    "match": {
      "title": "小米6"
    }
  }
}
# 查詢不出結果,因為資料匯入時已被分詞,無法完成匹配
GET product/_search
{
  "query": {
    "term": {
      "title": "小米6"
    }
  }
}
# keyword 精確匹配效果相類似於 match_phrase,區別在於這個keyword必須所有欄位都相同,但match_phrase 不需要
GET product/_search
{
  "query": {
    "match": {
      "title.keyword": "小米6"
    }
  }
}


# 執行聚合

# 搜尋 title 中包含小米的價格分佈以及平均幾價格
GET product/_search
{
  "query": {
    "match": {
      "title": "小米"
    }
  },
  "aggs": {
    "priceAgg": {
      "terms": {
        "field": "price",
        "size": 10
      }
    },
    "priceAvg":{
      "avg": {
        "field": "price"
      }
    }
  },
  "size": 0
}

# 按照價格分佈

GET product/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "priceAgg": {
      "terms": {
        "field": "price",
        "size": 100
      },
      "aggs": {
        "ageAvg": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

GET product/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "ageAgg": {
      "terms": {
        "field": "age",
        "size": 100
      },
      "aggs": {
        "genderAgg": {
          "terms": {
            "field": "gender.keyword",
            "size": 10
          },
          "aggs": {
            "balanceAvg": {
              "avg": {
                "field": "balance"
              }
            },
            "ageBalanceAvg":{
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }
  }
}

# 檢視索引資訊
GET product/_mapping
GET /my-index/_mapping

# 建立索引指定對映
PUT /my-index
{
  "mappings": {
    "properties": {
      "age":{
        "type": "integer"
      },
      "email":{
        "type": "keyword"
      },
      "name":{
        "type": "text"
      }
    }
  }
}

# 索引新增欄位對映 索引加上 _mapping
PUT /my-index/_mapping
{
  "properties": {
    "employee-id":{
      "type":"keyword",
      "index":false
    }
  }
}

# 無法修改索引,如果實在要修改,只能進行資料遷移
GET /product/_mapping

PUT /newproduct
{
  "mappings": {
    "properties": {
      "category" : {
          "type" : "keyword"
        },
        "id" : {
          "type" : "long"
        },
        "images" : {
          "type" : "text"
        },
        "price" : {
          "type" : "double"
        },
        "title" : {
          "type" : "text"
        }
    }
  }
}

GET /newproduct/_mapping
# 檢視索引型別
GET /product/_search
# 資料遷移
POST _reindex
{
  "source": {
    "index": "product"
  },
  "dest": {
    "index": "newproduct"
  }
}

# 6.0之前老版本遷移
POST _reindex
{
  "source": {
    "index": "product",
    "type": "_doc"
  },
  "dest": {
    "index": "newproduct"
  }
}
# 分詞器
# 預設
POST _analyze
{
  "analyzer": "standard",
  "text": ["中華人民共和國"]
}

# ik
POST _analyze
{
  "analyzer": "ik_smart",
  "text": ["中華人民共和國"]
}

POST _analyze
{
  "analyzer": "ik_max_word",
  "text": ["中華人民共和國"]
}

# 自定義
# 無法分詞
POST _analyze
{
  "analyzer": "ik_max_word",
  "text": ["喬碧羅"]
}
  1. 先設定 nginx 對映檔案
  2. 修改 ik config 配置檔案
vim /opt/elasticsearch-7.13.2/plugins/ik/config/IKAnalyzer.cfg.xml

Kibana 全文檢索操作

再次測試發現可以對需要進行分詞的資料進行分詞

# 多值查詢
GET /file_mapping/_search
{
  "query": {
    "terms": {
      "id": [
        "95ikwoZZODUFeN4Px5Pty7KXSON6wp",
        "63n2Es8pJGXejgZ8frP7knLsNmi5bw"
      ]
    }
  }
}

# 萬用字元匹配查詢
GET /file_mapping/_search
{
  "query": {
    "regexp": {
      "fileName": "1*"
    }
  }
}

# 建立索引
PUT /file_mapping
{
  "mappings" : {
      "properties" : {
        "expansionNum" : {
          "type" : "integer"
        },
        "fileExtension" : {
          "type" : "keyword"
        },
        "fileName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "analyzer" : "ik_max_word",
          "search_analyzer": "ik_smart", 
          "fielddata" : true
        },
        "fileSize" : {
          "type" : "keyword",
          "index" : false
        },
        "id" : {
          "type" : "keyword"
        },
        "isDir" : {
          "type" : "boolean"
        },
        "lastModified" : {
          "type" : "date",
          "format" : "date_optional_time||epoch_millis||yyyy-MM-dd HH:mm:ss"
        },
        "parentId" : {
          "type" : "keyword"
        },
        "size" : {
          "type" : "long"
        },
        "uploadTime" : {
          "type" : "date",
          "format" : "date_optional_time||epoch_millis||yyyy-MM-dd HH:mm:ss"
        },
        "userId" : {
          "type" : "integer"
        }
      }
    }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章