elasticsearch基础(gulimall高级学习的)


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



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


##全文检索,按照评分进行排序,会对检索条件进行分词匹配
GET /bank/_search
{
  "query": {
    "match": {
      "balance": 16418
    }
  }
}

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


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

##多字段匹配会进行分词
GET /bank/_search
{
  "query": {
    "multi_match": {
      "query": "mill Movico", 
      "fields": ["address","city"]
    }
  }
}

##bool查询 组合查询,must:必须满足;must:必须不满足;should:满不满足都可以,满足了分数就会更高
GET bank/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "gender": "M"
        }},
        {
          "match": {
            "address": "mill"
          }
        }
      ],
      "must_not": [
        {"match": {
          "age": "18"
        }}
      ],
      "should": [
        {"match": {
          "lastname": "Wallace"
        }}
      ]
    }
  }
}


## filter 不会贡献相关性得分
GET bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "range": {
            "age": {
              "gte": 18,
              "lte": 30
            }
        }}
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 18,
            "lte": 25
          }
        }
      }
    }
  }
}
GET bank/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "gte": 18,
            "lte": 30
          }
        }
      }
    }
  }
}


## 精确值用term,全文检索就有match
GET bank/_search
{
  "query": {
    "term": {
      "age": 28
    }
  }
}

##match_phrase 不会对查询条件进行分词, 查询时只要匹配一部分即可
GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "789 Madison"
    }
  }
}

##精确匹配,必须和查询条件值一样才可以 即不会对查询条件进行分词, 查询时必须完全匹配
GET bank/_search
{
  "query": {
    "match": {
      "address.keyword": "789 Madison"
    }
  }
}


##搜索address中包含mill的所有人的年龄分布以及平均年龄,但不显示这些人的详情
GET bank/_search
{
  "query": {
    "match": {
      "address": "mill"
    }
  },
  "aggs": {
    "ageAgg": {
      "terms": {
        "field": "age",
        "size": 10
      }
    },
    "ageAvg": {
      "avg": {
        "field": "age"
      } 
    },
    "balanceAvg": {
      "avg": {
        "field": "balance"
      }
    }
  },
  "size": 0
}


##按照年龄聚合,并且求这些年龄段的这些人的平均薪资(子聚合)
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "ageAgg": {
      "terms": {
        "field": "age",
        "size": 100
      },
      "aggs": {
        "ageAvg": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}

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

##查看映射
GET bank/_mapping

##创建索引是指定类型
PUT /my_index
{
  "mappings": {
    "properties": {
      "age": {"type": "integer"},
      "email": {"type": "keyword"},
      "name": {"type": "text"}
    }
  }
}

##修改映射(映射作名词就是对应数据库的列的属性)"index" 默认是true,表示可以被检索,false表示不能被检索,作为冗余字段 ##(这里的不能被检索是否是指不能被当作检索条件,自己验证)  这里相当于添加了一个列的(映射)
PUT /my_index/_mapping
{
  "properties": {
    "emloyee-id": {
      "type": "keyword",
      "index": false
    }
  }
}

GET /bank/_search

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

##执行数据迁移  ES7以后的版本不在建议使用类型了,自己会有一个默认类型,—doc
POST _reindex
{
  "source": {
    "index": "bank",
    "type": "account"
  },
  "dest": {
    "index": "newbank"
  }
}


GET /newbank/_search


##分词
POST _analyze
{
  "analyzer": "standard",
  "text": ["I love you yty."]
}

##可以识别正文的ik分词器,自定义词库用Nginx配置,放在Nginx默认的文件html
POST _analyze
{
  "analyzer": "ik_max_word",
  "text": ["乔碧萝殿下,我爱你"]
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值