ES Elasticsearch 基础增删改查操作 高亮查询

本文详细介绍了Elasticsearch(ES)的基本操作,包括查询所有数据、按ID获取、更新、新增和删除文档。此外,还涵盖了ES的关键词、模糊、精准和高亮查询等高级搜索技巧,例如使用bool查询实现and、or、not逻辑,以及范围查询和排序。同时,文章提及了如何进行完全匹配查询和高亮显示搜索结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、ES基本增删改查

1、查询全部数据

GET /books/books/_search

2、查询某一条数据

# 注意 12 代表的是ES中的 _id 
GET /books/books/12

3、更新某一条数据 (注意 : 字段需要写全 ,更新操作是将本条数据清除重新添加所给字段)

# 注意 12 代表的是ES中的 _id 
POST /books/books/12/_update
{
  "doc": {
    "id": 12,
    "title": "西游记",
    "summary": "修改某一条数据"
	}
}

4、新增一条数据

# 注意 12 代表的是ES中的 _id 
PUT /books/books/12
{
    "id": 12,
    "title": "西游记",
    "summary": "西游记",
}

5、删除一条数据

# 注意 12 代表的是ES中的 _id 
DELETE /books/books/12

二、ES基本查询

1、普通关键词分词检索 默认根据匹配程度分数排序

GET /books/books/_search
{
  "query": {
    "match": {
      "title": "社会保障制度与经济发展"
    }
  }
}

2、普通关键词检索之过滤字段 只查询title和pubdate字段

GET /books/books/_search
{
  "query": {
    "match": {
      "title": "社会保障"
    }
  },
  "_source": ["title","pubdate"] 
}

3、普通关键词检索之排序 按照id倒排,正序是 asc

GET /books/books/_search
{
  "query": {
    "match": {
      "title": "社会保障"
    }
  },
  "_source": ["title","pubdate","id"],
  "sort": [
      {
        "id": {
          "order": "desc"
        }
      }
   ]
}

4、普通关键词检索之分页 “from”: 0,“size”: 20

GET /books/books/_search
{
  "query": {
    "match": {
      "title": "社会保障"
    }
  },
  "_source": ["title","pubdate","id"],
  "sort": [
    {
      "id": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 20
}

三、ES模糊查询

1、模糊查询- and 多个条件同时满足分词检索查询

相当于mysql 的 select * from books where title like “%西方经济学%” and publisher like “%河南科学技术出版社%”

GET /books/books/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "西方经济学"
          }
        },
        {
          "match": {
            "publisher": "河南科学技术出版社"
          }
        }
      ]
    }
  }
}

2、模糊查询- or 多个条件满足其一分词检索查询

相当于mysql 的 select * from books where title like “%西方经济学%” or publisher like “%河南科学技术出版社%”

GET /books/books/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "河南科学技术出版社"
          }
        },
        {
          "match": {
            "publisher": "河南科学技术出版社"
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 20
}

3、模糊查询-不包括指定关键字检索 多个条件同时满足分词检索查询

GET /books/books/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "title": "河南科学技术出版社"
          }
        },
        {
          "match": {
            "publisher": "河南科学技术出版社"
          }
        }
      ]
    }
  }
}

4、模糊查询-范围查询 并排序

GET /books/books/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "性爱的艺术"
          }
        }
      ], 
      "filter": {
        "range": {
          "id": {
            "gt": 399995,
            "lt": 400000
          }
        }
      }
    }
  },
  "sort": [
    {
      "id": {
        "order": "desc"
      }
    }
  ]
}

四、精准查询

1、通过 GET /books 命令查看一下title的类型,发现是:text 还有keyword,这样的话做到完全匹配需要加上 title.keyword

"title" : {
	"type" : "text",
	"fields" : {
		"keyword" : {
			"type" : "keyword",
			"ignore_above" : 256
		}
	}
},
GET /books/books/_search
{
  "query": {
    "term": {
      "title.keyword": {
        "value": "在共和国的旗帜下"
      }
    }
  }
}

五、高亮查询

GET /books/books/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "南流河北流河"
          }
        }
      ]
    }
  },
  "highlight": {
    "pre_tags": [
          "<em class=\"c_color\">"
      ],
      "post_tags": [
        "</em>"
      ],
    "fields": {
      "title":{}
    }
  }
}s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值