es常用操作
免费chatgpt使用网址 http://ffff.chat:2023 在设置中设置userID
1. 新建一个索引,索引名为account
PUT account
{
}
2. 创建索引映射文件
其中text类型,es会自动分词,另外添加一个属性字段keyword,保存原来不分词的字段
PUT account/_mapping
{
"properties": {
"account_name": {
"type": "text",
"analyzer": "ik_smart",
"fields":
{
"keyword":
{
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
3. 添加一条记录doc,并且设置id
PUT account/_doc/1
{
"account_name": "程序员的早餐"
}
PUT account/_doc/4
{
"account_name": "早餐的程序员"
}
4. 查询记录
GET account/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"account_name.keyword": "早餐的程序员"
}
},
{
"match": {
"account_name": "早餐的程序员"
}
}
]
}
}
}
5 多个字段匹配查询
GET question/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "杭州"
}
},
{
"match": {
"content": "杭州"
}
}
]
}
}
}
6 提高权重
GET question/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": {
"query": "北京",
"boost": 2
}
}
},
{
"match": {
"content": {
"query": "北京",
"boost": 1
}
}
}
]
}
}
}

浙公网安备 33010602011771号