谷粒商城高级篇之ES进阶检索和映射

进阶检索

1. Search API

GET bank/_search?q=*&sort=account_number:asc  //方法一。设置根据account_number升序搜索
GET /bank/_search
{
  "query": {
    "match_all": {} //匹配所有
  },
  "sort": [
    {
      "account_number": "asc"  //根据account_number升序
    }
  ]
}

GET /bank/_search
{
  "query": {
    "match_all": {} //匹配所有
  },
  "sort": [
    {
      "account_number": "asc"  //根据account_number升序
    }{
      "balance": "desc" //如果account_number相同,则按照balance降序
  ]
}

2. Query DSL

GET /bank/_search
{
  "query": {
    "match_all": {} //匹配所有
  },
  "sort": [
   {
      "balance": "desc" //按照balance降序
   }
  ],
    "from" : 0,
    "size"5 , //从第0条开始,查5条数据
   "_source": ["balance","firstname"] //返回的字段的属性只有 balance和firstname
}

3. match匹配查询

GET /bank/_search
{
  "query": {
    "match": {
      "account_number": "20"   //查询account_number为20的字段
    }
  }
}

GET /bank/_search
{
  "query": {
    "match": {
      "address": "Kings"   //查询包含Kings的所有字段,即模糊查询,即全文检索,会按照评分排序
       
    }
  }
}

4. match_phrase 短语匹配

GET /bank/_search
{
  "query": {
    "match_phrase": {
      "address": "mill road" //查询adderss中包含完整的mill road的所有记录,即包含即可
    }
  }
}
GET /bank/_search
{
  "query": {
    "match": {
      "address.keyword": "132 Gunnison" //这个是完全匹配,address必须是这个字段
    }
  }
}

5. multi_match 多字段匹配

GET /bank/_search
{
  "query": {
    "multi_match": {
      "query": "mill",
      "fields": ["state","address"]  //查询state或者address中包含吗mill的字段
    }
  }
}


6. bool 复杂查询

GET /bank/_search
{
  "query": {
    "bool": {
      "must": [  //必须满足的条件
        {
          "match": {
            "gender": "F"  //gender必须是F
          }
        },
        {
          "match": {
            "age": "40"  //age必须是40
          }
        }
      ],
      "must_not": [  //一定不满足
        {
          "match": {
            "age": "38" //age必须不是38的
          }
        }
      ],
      "should": [  //应该,满足最好,不满足也行,即满足分数更高
        {
          "match": {
            "employer": "Suremax"
          }
        }
      ]
    }
  }
}

7. filter结果过滤

GET /bank/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "gte": 10,   //filter不会计算相关性得分,直接将不满足条件的过滤
            "lte": 20
          }
        }
      }
    }
  }
}

8. term

GET /bank/_search
{
  "query": {
    "term": {
        "age": "30"  //精确字段的使用term,全文检索的字段使用match
    }
  }
}

9. aggregations 执行聚合

GET /bank/_search
{
  "query": {
    "match": {
      "address": "mill"  //address包含mill的
    }
  },
  "aggs": { 			//聚合
    "ageAgg": { 		//聚合名称
      "terms": {		//聚合类型
        "field": "age",  //查询年龄的分布
        "size": 10
      }
    },
    "ageAvg":{  //聚合类型
      "avg": {  
        "field": "age"  //年龄的平均值
      }
    }
  },
  "size": 0 //只看聚合记录
}
GET /bank/_search
{
  "query": {
    "match_all": {}  //查询所有
  },"aggs": {
    "ageAgg": {
      "terms": {
        "field": "age",  //年龄分布的10种情况
        "size": 10
      },
      "aggs": {
        "ageAvg": {
          "avg": {   //在年龄分布前提下,再求balance的平均值
            "field": "balance"
          }
        }
      }
    }
  }
}

//查出所有年龄分布,并且这些年龄段中M的平均薪资和F的平均薪资以及这个年龄段的总体平均薪资
GET /bank/_search
{
  "query": {
    "match_all": {}  //查询所有
  },
  "aggs": {
    "ageAgg": {
      "terms": {
        "field": "age",  //100种年龄分布
        "size": 100
      },
      "aggs": {
        "genderAvg": {
          "terms": {
            "field": "gender.keyword",  //并且性别聚合
            "size": 10
          },
          "aggs": {
            "balanceAvg": {
              "avg": {
                "field": "balance"  //再gender聚合的基础上再对balance聚合
              }
            }
          }
        },
        "ageBalance":{
          "avg": {
            "field": "balance"  //这个年龄段的平均工资
          }
        }
      }
    }
  }
}

映射

映射

GET /bank/_mapping //查询bank下的所有映射

PUT /my-index  //创建索引,指定映射规则
{
  "mappings": {
    "properties": {
      "age":    { "type": "integer" },   
      "email":  { "type": "keyword"  }, 
      "name":   { "type": "text"  }     
    }
  }
}

//添加字段映射
PUT /my-index/_mapping  
{
    "properties": { 
         "employee-id": {  //新增加一个employee-id
          "type": "keyword",
          "index": false //不需要被索引,默认是true,即可以检索这个字段
        }
    }
}
//对于已经存在的映射不能修改

数据迁移

PUT /newbank  //先创建一个新索引
{
  "mappings": {
    "properties": {
      "account_number": {
        "type": "long"
      },
      "address": {
        "type": "text"
      },
      "age": {
        "type": "integer"
      },
      "balance": {
        "type": "long"
      },
      "city": {
        "type": "keyword"
      },
      "email": {
        "type": "keyword"
      },
      "employer": {
        "type": "keyword"
      },
      "firstname": {
        "type": "text"
      },
      "gender": {
        "type": "keyword"
      },
      "lastname": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "state": {
        "type": "keyword"
      }
    }
  }
}
//将老索引的数据进行迁移到新索引
POST _reindex
{
  "source": {
    "index": "bank",  //将bank索引下的account类型迁移到新索引
    "type": "account"
  },
  "dest": {
    "index": "newbank"
  }
}
//新版本都不用type,老数据可以迁移过来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值