Kibana 全文检索操作

IT知识
381
0
0
2022-05-06
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"}}}
}