GET _search
{
"query": {
"match_all": {}
}
}
# 创建索引库
PUT /heima
{
"mappings": {
"properties": {
"info": {
"type": "text",
"analyzer": "ik_smart"
},
"email":{
"type": "keyword",
"index": false
},
"name":{
"type": "object",
"properties": {
"firstname":{
"type":"keyword",
"index": false
},
"lastname":{
"type":"keyword",
"index": false
}
}
}
}
}
}
# 查询
GET /heima
PUT /heima/_mapping
{
"properties": {
"age":{
"type" : "integer",
"index" : false
}
}
}
POST /heima/_doc/1
{
"info": "黑马程序员",
"email":"ssda@qq",
"name":{
"firstname":"云",
"lastname":"赵"
}
}
# 查询
GET /heima/_doc/1
#修改
PUT /heima/_doc/3
{
"info":"hhh"
}
#局部修改
POST /heima/_update/1
{
"doc":{
"email":"haajajaj@qq.com"
}
}
# 业务部分
PUT /hotel
{
"mappings": {
"properties": {
"id":{
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "ik_max_word"
, "copy_to": "all"
},
"address":{
"type": "keyword",
"index": false
, "copy_to": "all"
},
"price":{
"type": "integer"
, "copy_to": "all"
},
"score":{
"type": "integer"
, "copy_to": "all"
},
"brand":{
"type": "keyword"
},
"city":{
"type": "keyword"
},
"startName":{
"type": "keyword"
},
"business":{
"type": "keyword"
, "copy_to": "all"
},
"location":{
"type": "geo_point"
, "copy_to": "all"
},
"pic":{
"type": "keyword",
"index": false
},
"all":{
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
DELETE /hotel
GET /hotel
GET /hotel/_doc/56912
GET /hotel/_search
GET /hotel/_search
{
"query": {
"match_all": {}
}
}
GET /hotel/_search
{
"query": {
"match": {
"all": "维也纳"
}
}
}
GET /hotel/_search
{
"query": {
"multi_match": {
"query": "维也纳",
"fields": ["name","business"]
}
}
}
GET /hotel/_search
{
"query": {
"term": {
"city": {
"value": "上海s"
}
}
}
}
GET /hotel/_search
{
"query": {
"range": {
"price": {
"gt": 2688,
"lt": 3000
}
}
}
}
GET /hotel/_search
{
"query": {
"geo_distance":{
"distance":"15km",
"location":"30.921659, 121.575572"
}
}
}
# 控制打分
GET /hotel/_search
{
"query": {
"function_score": {
"query": {
"match": {
"all": "酒店"
}
},
"functions": [
{
"filter": {
"term": {
"brand": "如家"
}
},
"weight": 1.2
}
],
"boost_mode": "multiply"
}
}
}
#复合查询
GET /hotel/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "如家"
}
}
],
"must_not": [
{
"range": {
"price": {
"gte": 600
}
}
}
],
"filter": [
{
"geo_distance": {
"distance": "10km",
"location": {
"lat": 31.21,
"lon": 121.5
}
}
}
]
}
}
}
#排序 -- 普通排序
GET /hotel/_search
{
"query": {
"match": {
"all": "酒店"
}
},
"sort": [
{
"score": "desc"
},
{
"price": "asc"
}
]
}
#排序 -- 地理位置
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 31.034661,
"lon": 121.612282
},
"order": "asc",
"unit": "km"
}
}
]
}
#分页查询
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "asc"
}
}
],
"from": 180,
"size": 200
}
#高亮显示
GET /hotel/_search
{
"query": {
"match": {
"all": "如家"
}
},
"highlight": {
"fields": {
"name": {
"require_field_match": "false"
}
}
}
}
实际操作
查询:
package cn.itcast.hotel;
import cn.itcast.hotel.service.IHotelService;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
/**
* @author li zongyang
* @date 2022/8/8
*/
@SpringBootTest
public class SearchDocumentTest {
@Resource
private IHotelService service;
private RestHighLevelClient restHighLevelClient;
//private RestClient restClient;
@BeforeEach
void setUp() {
HttpHost host = new HttpHost("127.0.0.1", 9200, "http");
HttpHost host1 = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder builder = RestClient.builder(host, host1);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("", ""));
builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider));
this.restHighLevelClient = new RestHighLevelClient(builder);
//this.restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
}
@Test
public void getDocumentByMatchAll() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchAllQuery());
search(request);
}
@Test
public void getDocumentByMatch() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchQuery("name", "维也纳"));
search(request);
}
@Test
public void getDocumentOther() throws IOException {
SearchRequest request = new SearchRequest("hotel");
//request.source().query(QueryBuilders.termQuery("name","酒店")); //词条查询
request.source().query(QueryBuilders.rangeQuery("price").gte(100).lte(200)); //范围查询
search(request);
}
//复合查询
@Test
public void getDocumentWithBool() throws IOException {
SearchRequest request = new SearchRequest("hotel");
BoolQueryBuilder query = QueryBuilders.boolQuery();
query.must(QueryBuilders.matchQuery("name", "维也纳"));
query.filter(QueryBuilders.rangeQuery("price").gte("300").lte("600"));
request.source().query(query); //范围查询
search(request);
}
//分页和排序
@Test
public void getDocumentWithSortAndPage() throws IOException {
int page = 1;
int size = 5;
SearchRequest request = new SearchRequest("hotel");
request.source().from((page - 1) * size).size(size).sort("price", SortOrder.ASC);
search(request);
}
//高亮
@Test
public void getDocumentWithHighLighter() throws IOException {
SearchRequest request = new SearchRequest("hotel");
//搜索条件
request.source().query(QueryBuilders.matchQuery("all", "如家"));
//高亮
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("name").requireFieldMatch(false);
request.source().highlighter(highlightBuilder);
searchWithHighLighter(request);
}
private void search(SearchRequest request) throws IOException {
SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT);
long value = search.getHits().getTotalHits().value;
System.err.println("总条数" + value);
SearchHit[] hits = search.getHits().getHits();
for (SearchHit hit : hits) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
System.err.println(sourceAsMap);
}
}
private void searchWithHighLighter(SearchRequest request) throws IOException {
SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT);
long value = search.getHits().getTotalHits().value;
System.err.println("总条数" + value);
SearchHit[] hits = search.getHits().getHits();
for (SearchHit hit : hits) {
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
if (!highlightFields.isEmpty() && !sourceAsMap.isEmpty()) {
sourceAsMap.put("name", highlightFields.get("name").getFragments()[0].string());
System.err.println(hit.getSourceAsMap());
}
}
}
}
索引操作:
package cn.itcast.hotel;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
/**
* @author li zongyang
* @date 2022/8/5
*/
@SpringBootTest
public class HotelTest {
private RestHighLevelClient restHighLevelClient;
//private RestClient restClient;
@BeforeEach
void setUp() {
HttpHost host = new HttpHost("127.0.0.1", 9200, "http");
HttpHost host1 = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder builder = RestClient.builder(host,host1);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("", ""));
builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider));
this.restHighLevelClient = new RestHighLevelClient(builder);
//this.restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
}
@Test
void initRest(){
//System.out.println(restClient);
System.out.println(restHighLevelClient);
}
@Test
public void createHotelIndex() throws IOException {
String index = "{\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"id\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"name\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" , \"copy_to\": \"all\"\n" +
" },\n" +
" \"address\":{\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" },\n" +
" \"price\":{\n" +
" \"type\": \"integer\"\n" +
" , \"copy_to\": \"all\"\n" +
" },\n" +
" \"score\":{\n" +
" \"type\": \"integer\"\n" +
" , \"copy_to\": \"all\"\n" +
" },\n" +
" \"brand\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"city\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"startName\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"business\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"location\":{\n" +
" \"type\": \"geo_point\"\n" +
" , \"copy_to\": \"all\"\n" +
" },\n" +
" \"pic\":{\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" },\n" +
" \"all\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
CreateIndexRequest request = new CreateIndexRequest("hotel");
request.source(index, XContentType.JSON);
restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
}
@Test
public void delIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
restHighLevelClient.indices().delete(request,RequestOptions.DEFAULT);
}
@Test
public void getIndexExit() throws IOException {
GetIndexRequest request = new GetIndexRequest("hotel");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.err.println(exists);
}
@Test
public void setHotelTest() {
}
}
文档操作
package cn.itcast.hotel;
import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
/**
* @author li zongyang
* @date 2022/8/5
*/
@SpringBootTest
public class HotelDetailTest {
@Resource
private IHotelService service;
private RestHighLevelClient restHighLevelClient;
//private RestClient restClient;
@BeforeEach
void setUp() {
HttpHost host = new HttpHost("127.0.0.1", 9200, "http");
HttpHost host1 = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder builder = RestClient.builder(host, host1);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("", ""));
builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider));
this.restHighLevelClient = new RestHighLevelClient(builder);
//this.restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
}
@Test
void initRest() {
//System.out.println(restClient);
System.out.println(restHighLevelClient);
}
@Test
public void testAddDocument() throws IOException {
Hotel hotel = service.getById(56912L);
HotelDoc hotelDoc = new HotelDoc(hotel);
IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
restHighLevelClient.index(request, RequestOptions.DEFAULT);
}
@Test
public void getDocument() throws IOException {
GetRequest request = new GetRequest("hotel", "56912");
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
String source = response.getSourceAsString();
HotelDoc hotelDoc = JSON.parseObject(source, HotelDoc.class);
if (hotelDoc != null) {
Hotel hotel = new Hotel(hotelDoc);
System.out.println(hotel);
}
}
@Test
public void updateDocument() throws IOException {
UpdateRequest request = new
UpdateRequest("hotel", "56912");
request.doc(
"address", "月华路2000号",
"city", "天堂"
);
restHighLevelClient.update(request, RequestOptions.DEFAULT);
}
@Test
public void delDocument() throws IOException {
DeleteRequest request = new
DeleteRequest("hotel", "56912");
restHighLevelClient.delete(request, RequestOptions.DEFAULT);
}
@Test
public void importDocuments() throws IOException {
List<Hotel> list = service.list();
BulkRequest add = new BulkRequest();
for (Hotel hotel : list) {
HotelDoc hotelDoc = new HotelDoc(hotel);
add.add(
new IndexRequest("hotel").id(hotelDoc.getId().toString()).
source(JSON.toJSONString(hotelDoc), XContentType.JSON));
}
BulkResponse bulk = restHighLevelClient.bulk(add, RequestOptions.DEFAULT);
boolean b = bulk.hasFailures();
System.err.println(b);
}
@Test
public void setHotelTest() {
}
}