导引——第一条ElasticSearch语句
测试分词器
POST /_analyze
{"text":"黑马程序员学习java太棒了","analyzer": "ik_smart"
}
概念
语法规则
HTTP_METHOD /index/_action/ID
HTTP_METHOD 是 HTTP 请求的方法,常见的包括 GET、POST、PUT、DELETE 等。
/index 是索引的名称。
/_action/ID 是操作的动作和文档的 ID。动作可以是 _search、_update、_delete 等,而 ID 则是文档的唯一标识符
HTTP_METHOD请求
GET:用于检索文档或执行搜索操作
GET /index/_search
PUT:用于创建新文档、执行搜索、执行批量操作等
PUT /index/_doc/ID
{ "field1": "value1" }
POST:创建(全量更新)或更新文档,如果文档已存在,则更新文档的全部内容
这里Elasticsearch 会自动生成文档的唯一标识符(ID),当然你也能申明指定的ID
POST /index/_doc
{ "field1": "value1" }
DELETE: 用于删除
DELETE /index/_doc/ID
ACTION 行为
在 Elasticsearch 中,_action 部分用于指定操作的动作。以下是一些常见的 _action 及其用途:
_search: 执行搜索操作,用于检索文档。
POST /index/_search
_count: 返回匹配查询条件的文档数量,但不返回文档本身。
POST /index/_count
_update: 更新文档的部分或全部内容。
POST /index/_update/ID
_delete: 删除指定 ID 的文档。
DELETE /index/_doc/ID
_bulk: 批量操作,可以一次执行多个索引、更新、删除等操作。
POST /index/_bulk
{ "index": { "_id": "1" } }
{ "field1": "value1" }
{ "update": { "_id": "2" } }
{ "doc": { "field2": "value2" } }
{ "delete": { "_id": "3" } }
_msearch: 执行多个搜索请求
POST /index/_msearch
{ }
{ "query": { "match_all": { } } }
INDEX
类似于MySQL的database
文档
类似于MySQL表中的一行
实战
索引操作
黑马的索引库
# 创建索引库
PUT /heima
{"mappings": {"properties": {"info":{"type":"text","analyzer": "ik_smart"},"email":{"type":"keyword","index": "false"}}}
}
酒店的索引库
# 酒店的mapping
PUT /hotel
{"mappings": {"properties": {"id":{"type": "keyword"},"name":{"type": "text","analyzer": "ik_max_word","copy_to": "all"},"address":{"type":"keyword","index": false},"price":{"type":"integer"},"score":{"type":"integer"},"brand":{"type": "keyword"},"city":{"type":"keyword"},"star_name":{"type": "keyword"},"business":{"type": "keyword","copy_to": "all"},"location":{"type":"geo_point"},"pic":{"type": "keyword","index": false},"all":{"type": "text","analyzer": "ik_max_word"}}}
文档操作
POST /heima/_doc/1
{"info":"黑马程序员Java学生","email":"zy@itcast.cn","name":{"firstName":"刘","lastName":"小小"}
}
GET /heima/_doc/1
查询功能拓展
全搜索
GET /hotel/_search
{"query": {"match_all": {}}
}
GET /hotel/_search
{"query": {"match": {"all": "如家"}}
}
精确搜索
GET /hotel/_search
{"query": {"term": {"city": "上海"}}
}
价格范围搜索
让用户能够先看到如家,给如家额外增加10分
# function score查询
GET /hotel/_search
{"query": {"function_score": {"query": {"match": {"all": "外滩"}},"functions": [{"filter": {"term": {"brand": "如家"}},"weight": 10},{"filter": {"term": {"brand": "7天"}},"weight": 5}],"boost_mode": "sum"}}
}
复合查询
Java中Match的核心四步骤
1、准备request
2、准备DSL请求参数语句
3、发送请求得到响应
4、处理响应
注意:只有DSL会改变
request.source().query(QueryBuilders.multiMatchQuery("外滩如家", "name", "brand", "city"));
request.source().query(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("city", "杭州")).filter(QueryBuilders.rangeQuery("price").lte(250)));
测试类
@Test
void testMatch() throws IOException {// 1.准备requestSearchRequest request = new SearchRequest("hotel");// 2.准备请求参数DSL// request.source().query(QueryBuilders.matchQuery("all", "外滩如家"));request.source().query(QueryBuilders.multiMatchQuery("外滩如家", "name", "brand", "city"));// 3.发送请求,得到响应SearchResponse response = client.search(request, RequestOptions.DEFAULT);// 4.结果解析handleResponse(response);
}
private void handleResponse(SearchResponse response) {//解析响应SearchHits searchHits = response.getHits();// 4.1.总条数long total = searchHits.getTotalHits().value;System.out.println("总条数:" + total);// 4.2.获取文档数组SearchHit[] hits = searchHits.getHits();// 4.3.遍历for (SearchHit hit : hits) {// 4.4.获取sourceString json = hit.getSourceAsString();// 4.5.反序列化,非高亮的HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);// 4.6.处理高亮结果// 1)获取高亮mapMap<String, HighlightField> map = hit.getHighlightFields();// 2)根据字段名,获取高亮结果HighlightField highlightField = map.get("name");// 3)获取高亮结果字符串数组中的第1个元素if (highlightField != null) {String hName = highlightField.getFragments()[0].toString();// 4)把高亮结果放到HotelDoc中hotelDoc.setName(hName);} else {// 处理未找到高亮字段的情况hotelDoc.setName("No Highlight");}// 4.7.打印System.out.println(hotelDoc);}
}