3
es结构设计的几种优化方式
source link: https://blog.51cto.com/u_16199760/9199900
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
es结构设计的几种优化方式
精选 原创1、不需要查询的字段取消建立索引
es结构中,默认会给所有字段建立索引,占用索引空间
"candidate_person":{
"type": "keyword"
},
"candidate_email":{
"type": "keyword",
"index": false //设置不建立索引
},
2、构建联合索引字段查询
例如我有title(标题)和description(正文),搜索时关键字会匹配这两个
{
"from": 0,
"size": 10,
"query": {
"bool": {
"should": [
{
"match_phrase": {
"title": "中国"
}
},
{
"match_phrase": {
"description": "中国"
}
}
],
"minimum_should_match": 1
}
}
}
在构建索引时将两个字段作为联合索引查
构建方式如下:
"title":{
"type": "text",
"analyzer": "ik_max_word",
"copy_to": "my_search"
},
"description":{
"type": "text",
"analyzer": "ik_max_word",
"copy_to": "my_search"
},
"my_search":{
"type": "text",
"analyzer": "ik_max_word"
}
查询时就变得简单,只需要匹配my_search即可,比两个字段的或查询速度快
{
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"match_phrase": {
"my_search": "中国"
}
}
]
}
}
}
3、选择合适的数据类型
mapping是对索引库中文档的约束,常见的mapping属性包括:
- type:字段数据类型,常见的简单类型有:
- 字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址)
- 数值:long、integer、short、byte、double、float、
- 布尔:boolean
- 日期:date
- 对象:object
- index:是否创建索引,默认为true
- analyzer:使用哪种分词器
- properties:该字段的子字段
4、构建查询语句时尽量写在filter中,must需要计算相关性得分
注:filter的term条件查询不支持text
{
"from": 0,
"size": 10,
"track_total_hits": true,
"query": {
"bool": {
"must": [
{
"match_phrase": {
"evaluate": "A"
}
},
{
"bool": {
"should": [
{
"match_phrase": {
"title": "中国"
}
},
{
"match_phrase": {
"description": "中国"
}
}
],
"minimum_should_match": 1
}
},
{
"match_phrase": {
"zb_type": "工程招标"
}
},
{
"wildcard": {
"category": "*交通运输*"
}
}
],
"should": [
],
"filter": [
{
"term": {
"area": "北京"
}
},
{
"range": {
"publish_date": {
"gt": 1673419272000,
"lt": 1704955272000,
"format": "epoch_millis"
}
}
},
{
"range": {
"show_money": {
"gt": "1000000",
"lt": "10000000"
}
}
}
]
}
}
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK