栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ElasticSearch相关--进阶

ElasticSearch相关--进阶

1.QUERY DSL
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "sort": {
    "account_number": "asc",
    "balance":"desc"
  }
}

GET bank/_search
{
  "query": {
    "match_all": {}
  },
 "sort": [
   {
     "balance": {
       "order": "desc"
     }
   }
 ], 
  "from": 0,
  "size": 20,
  "_source":[ "balance","firstname"]
}

分词匹配 match

GET bank/_search
{
  "query": {
    "match": {
      "firstname": "West"
    }
  }
}

//查到两条,对检索条件分词匹配查询,按照评分进行排序
GET bank/_search
{
  "query": {
    "match": {
      "address": "Kings"
    }
  }
}

地址包含mill或者road都能匹配

短语匹配 match_phrase

GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "mill road"
    }
  }
}

只匹配包含mill road的

多条件查询

GET bank/_search
{
  "query": {
    "multi_match": {
      "query": "mill",
      "fields": [
        "state",
        "address"
      ]
    }
  }
}

bool符合查询

GET bank/_search
{
  "query": {
    "bool": {
      "must": [ 必须满足
        {
          "match": { 
            "gender": "F"
          }
        },
        {
          "match": {
            "address": "Madison"
          }
        }
      ],
      "must_not": [ 必须不满足
        {
          "match": {
            "age": "22"
          }
        }
      ],
      "should": [ 应该满足,不满足也可以查到
        {"match": {
          "balance": "32838"
        }}
      ]
    }
  }
}

filter结果过滤

GET bank/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "gte": 18,
            "lte": 30
          }
        }
      }
    }
  }
}

filter不会返回相关性得分

2.aggregations(执行聚合)
聚合提供了从数据中分组和提取数据的能力。

搜索address中包含mill的所有人的年龄分布以及平均年龄,但不显示这些人详情。

GET bank/_search
{
  "query": {
    "match": {
      "address": "mill"
    }
  },
  "aggs": {
    "ageAgg": {
      "terms": {
        "field": "age",
        "size": 10
      }
    },
    "avgAgg":{
      "avg": {
        "field": "age"
      }
    },
    "balanceAvg":{
      "avg": {
        "field": "balance"
      }
    }
  },
  "size": 0 只看聚合,不看详情
}

按照年龄聚合,并且请求这些年龄段的这些人的平均薪资

GET bank/_search
{
  "query": {
    "match_all": {
    }
  },
  "aggs": {
    "ageAgg": {
      "terms": {
        "field": "age",
        "size": 10},
      "aggs": {
        "ageAvg": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  },
  "size": 0
}

按照年龄聚合,并且这些年龄段中M平均薪资和F的平均薪资以及这个年龄段的总体平均薪资

GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "ageA": {
      "terms": {
        "field": "age",
        "size": 101
      },
      "aggs": {
        "genderA": {
          "terms": {
            "field": "gender.keyword",
            "size": 10
          },
          "aggs": {
            "balanceA": {
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }
  }
}

3.映射

创建一个索引,并配置映射

PUT /my_index
{
  "mappings": {
    "properties": {
      "age":{
        "type": "integer"
      },
      "email":{
        "type": "keyword"
      },
      "name":{
        "type": "text"
      }
    }
  }
}

添加新的字段映射

PUT /my_index/_mapping
{
  "properties":{
    "employee-id":{
      "type":"keyword",
      "index": false
    }
  }
}

修改映射&数据迁移

已经存在的映射字段,不能更新,更新必须创建新的牵引进行数据迁移

数据迁移

数据迁移 bank->newbank
POST _reindex
{
  "source": {
    "index": "bank", 
    "type": "account"
  },
  "dest": {
    "index": "newbank"
  }
}
查询newbank
GET /newbank/_search
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/734661.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号