Kibana 全文检索操作

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"
        }
      }
    }
}

常用操作

# 索引的相关操作(索引只有新增、查询和删除操作,没有修改操作)
# 查看 es 中的索引 ?v 表示返回数据的标题
GET _cat/indices?v

# 创建索引 由于默认创建的索引主和从分片都在一台机器上,都为一,导致索引为黄色
PUT /products

# 自定义索引
PUT /orders
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  }
}
# 删除索引
DELETE /products





# mapping 映射的相关操作,索引一旦创建,便无法修改
# 创建商品索引 products,指定 mapping{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"
      }
    }
  }
}

# 查看索引映射信息 mapping
GET /products/_mapping





# 文档的相关操作
# 指定文档id _id
POST /products/_doc/1
{
  "id":1,
  "title":"iphone 14",
  "price":5599.99,
  "created_at":"2022-05-17",
  "description":"iphone 14 屏幕采用 6.2 英寸 OLED 屏幕"
}
# 自动生成文档id
POST /products/_doc
{
  "title":"iphone 14 pro",
  "price":5599.99,
  "created_at":"2022-05-17",
  "description":"iphone 14 pro 屏幕采用 6.2 英寸 120hz 刷新 OLED 屏幕"
}
# 查看文档内容
GET /products/_doc/1
# 删除文档内容
DELETE /product/_doc/1
# 更新文档,会删除原始文档,从新添加
PUT /products/_doc/1
{
  "title":"iphone 13"
}
# 更新文档,基于指定的字段进行更新
POST /products/_doc/1/_update
{
  "doc":{
    "title":"iphone 13"
  }
}
# 批量操作 _bulk
# index 插入操作,update 更新操作,批量操作不允许对数据格式化,需要在一行
POST /products/_doc/_bulk
{"index":{"_id":2}}
{"id":12,"title":"面包","price":16.5,"created_at":"2022-05-18","description":"面包真好吃"}
{"index":{"_id":3}}
{"id":13,"title":"牛奶","price":69,"created_at":"2022-05-18","description":"牛奶真好喝"}
# 文档批量操作 添加 更新 删除
POST /products/_doc/_bulk
{"index":{"_id":4}}
{"id":4,"title":"瓜子","price":12.5,"created_at":"2022-05-18","description":"瓜子真好吃"}
{"update":{"_id":3}}
{"doc":{"title":"鱼豆腐"}}
{"delete":{"_id":2}}





# 高级查询
# 查询所有
GET /products/_search
{
  "query": {
    "match_all": {}
  }
}
# term 基于关键词查询,标准分词器对中文是按字分的
# keyword integer double date ip 不分词 适用全部内容匹配搜索
# text 默认 es 标准分词器 中文单字分词 英文单词分词
# 1.在 es 中,除了 text 类型分词,其余类型都不分词
# 2.在 es 中,默认使用的是标准分词器(StandardAnalyzer) 中文单字分词 英文 单词分词
GET /products/_search
{
  "query": {
    "term": {
      "description": {
        "value": "好"
      }
    }
  }
}
# 范围查询 range
GET /products/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 12,
        "lte": 5599
      }
    }
  }
}
# prefix 前缀查询
GET /products/_search
{
  "query": {
    "prefix": {
      "title": {
        "value": "ipho"
      }
    }
  }
}
# wildcard 通配符查询 ?用来匹配一个任意字符 *用来匹配多个任意字符
GET /products/_search
{
  "query": {
    "wildcard": {
      "description": {
        "value": "iph*"
      }
    }
  }
}
# ids 多 id 查询
GET /products/_search
{
  "query": {
    "ids": {
      "values": [
        "1",
        "2",
        "4"
      ]
    }
  }
}
# fuzzy 模糊查询
# 最大模糊错误必须在 02 之间
# 搜索关键词长度为 2,不允许存在模糊
# 搜索关键词长度为 35,允许一次模糊
# 搜索关键词长度大于 5,允许最大两次模糊
GET /products/_search
{
  "query": {
    "fuzzy": {
      "description": "iphone"
    }
  }
}
# bool 布尔查询
# must:相当于 && 同时成立
# should:相当于 || 成立一个就行
# must_not:相当于 ! 不能满足任何一个条件
GET /products/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "title": {
              "value": "iphone 13"
            }
          }
        },
        {
          "ids": {
            "values": [1]
          }
        }
      ]
    }
  }
}
# multi_mutch 多字段查询
# 注意:字段类型分词,将查询条件分词之后进行查询该字段 如果该字段不分词就会即哪个查询条件作为整体查询
GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "iphone 13 好",
      "fields": ["title","description"]
    }
  }
}
# query_string 默认字段分词查询
# 注意:查询字段分词就将查询条件分词查询 查询字段不分词将条件不分词查询
GET /products/_search
{
  "query": {
    "query_string": {
      "default_field": "description",
      "query": "面包屏幕"
    }
  }
}

# highlight 高亮查询
# 自定义高亮:可以在 hightlight 中使用 pre_tags 和 post_tags
# require_field_match 开启多个字段高亮
GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "iphone 好",
      "fields": ["title","description"]
    }
  },
  "highlight": {
    "pre_tags": "<span style='color:red;'>",
    "post_tags": "</span>",
    "require_field_match": "false", 
    "fields": {
      "*":{}
    }
  }
}
# size 返回指定条数
# from 用来指定起始位置
# 分页查询 from 和 size 关键字连用可以实现分页效果
GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "size": 3,
  "from": 0
}
# sort 自定排序字段
# 使用排序后会干预查询的分数导致 _score 为 null
# _source 返回指定的字段
GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ],
  "_source": ["id","title","description"]
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
247
粉丝
18
喜欢
217
收藏
62
排名:731
访问:9753
私信
所有博文
社区赞助商