谷粒商城ES进阶和映射(十七)

110、全文检索-ElasticSearch-进阶-两种查询方式-121 全文检索-ElasticSearch-映射-修改映射&数据迁移

讲的也比较简单,就简单记录一下

GET /bank/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "account_number": "asc"
    }
  ]
}


GET /bank/_search?q=*&sort=account_number:asc

GET /bank/_search
{
  "query": {
    "match": {
      "account_number": "20"
    }
  }
}

GET /bank/_search
{
  "query": {
    "match": {
      "address": "Kings"
    }
  }
}

GET /bank/_search
{
  "query": {
    "match": {
      "address": "mill lane"
    }
  }
}

##会断检索条件进行分词匹配

GET /bank/_search
{
  "query": {
    "match_phrase": {
      "address": "132 Gunnison"
    }
  }
}
#注意,这个keyword是完全匹配
GET /bank/_search
{
  "query": {
    "match": {
      "address.keyword": " 132 Gunnison"
    }
  }
}

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


GET /bank/_search?pretty
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "gender": "F"
          }
        },
        {
          "match": {
            "age": "40"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "state": "ID"
          }
        }
      ],
      "should": [
        {
          "match": {
            "employer": "Suremax"
          }
        }
      ]
    }
  }
}

#使用filter不会计算相关性得分
GET /bank/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
  }
}


GET /bank/_search
{
  "query": {
    "term": {
        "age": "30"
    }
  }
}

GET /bank/_search
{
  "query": {
    "match": {
      "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",
        "size": 10
      },
      "aggs": {
        "ageAvg": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}


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



##新增映射规则
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
    }
  }
}

#进行数据迁移
GET /bank/_mapping

GET /newbank/_mapping

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


POST _reindex
{
  "source": {
    "index": "bank",
    "type": "account"
  },
  "dest": {
    "index": "newbank"
  }
}

GET /newbank/_search

 

posted @ 2020-06-16 00:54  天宇轩-王  阅读(382)  评论(0编辑  收藏  举报