倒排索引
- ES采用倒排索引的方式提升查询效率
- GET _search
{
“query”: {
“match_all”: {}
}
}
PUT /lib/
{
“settings”: {
“number_of_shards”: 3,
“number_of_replicas”: 0
}
}
GET /lib/_settings
GET _all/_settings
添加文档
PUT /lib/user/1
{
“first_name”: “tom”,
“last_name”:“mao”
}
GET /lib/user/
不指定id使用post
POST /lib/user/
{
“first_name”: “tom”,
“last_name”:“mao”
}
修改使用新的文档进行覆盖
PUT /lib/user/1
{
“first_name”: “99999999”,
“last_name”:“mao”
}
GET /lib/user/1
直接修改不覆盖使用post
POST /lib/user/1/_update
{
“doc”:{
“first_name”: 77777
}
}
GET /lib/user/1
删除文档
DELETE /lib/user/1
GET /lib/user/1
批量获取文档
PUT /lib/user/3
{
“name”: 3
}
GET /lib/user/1
GET /_mget
{
“docs”:[
{
“_index”: “lib”,
“_type”: “user”,
“_id”:1
},
{
“_index”: “lib”,
“_type”: “user”,
“_id”:2
}
,
{
“_index”: “lib”,
“_type”: “user”,
“_id”:3
}
]
}
简化写法
GET /lib/user/_mget
{
“ids”: [1,2,3]
}
bulk批量操作
# 批量操作bulk
POST /lib2/books/_bulk
{"index":{"_id":1}}
{"title": "java","price":55}
{"index":{"_id":2}}
{"title":"html5","price":45}
{"index":{"_id":3}}
{"title":"php","price":30}
{"index":{"_id":4}}
{"title":"python","price":30}
GET /lib2/books/_mget
{
"ids":[1,2,3,4]
}
版本控制
乐观锁的方式实现版本控制
mapping
日期类型和数字类型默认是不分词的字符串类型是默认分词的
mapping规定数据类型和属性
手动创建mapping
基本查询
ES7执行报错ES6可以执行ES7需要增加include_type_name: true 配置
PUT /lib5
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"user":{
"properties":{
"name": {"type":"text"},
"address": {"type":"text"},
"age": {"type":"integer"},
"interests": {"type":"text"},
"birthday": {"type":"date"}
}
}
}
}
GET /lib3/user/_search?q=interests:duanlian&sort=age:desc
GET /lib3/user/_search/
{
"query": {
"terms": {
"name": ["lishi","zhaoliu"]
}
}
}
match会对查询条件进行分词
GET /lib3/user/_search/
{
"query": {
"match": {
"name": "lishi zhaoliu"
}
}
}
-----------
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.2039728,
"hits" : [
{
"_index" : "lib3",
"_type" : "user",
"_id" : "3",
"_score" : 1.2039728,
"_source" : {
"name" : "lishi",
"address" : "he long jiang sheng tie ling shi",
"age" : 50,
"birthday" : "1900-10-14",
"interests" : "xi huan hejiu,biancheng"
}
},
{
"_index" : "lib3",
"_type" : "user",
"_id" : "1",
"_score" : 0.6931471,
"_source" : {
"name" : "zhaoliu",
"address" : "he long jiang sheng tie ling shi",
"age" : 50,
"birthday" : "1900-10-14",
"interests" : "xi huan hejiu,duanlian,lvyou"
}
}
]
}
}
多属性匹配
GET /lib3/user/_search/
{
"query": {
"multi_match": {
"query":"lishi",
"fields": ["name","interests"]
}
}
}
排序
前缀匹配/范围匹配
边界值
模糊查询
高亮显示
Filter查询
不计算相关性,同事可以缓存比query查询要快
价格是125并且id是123的并且价格不是30的
范围过滤
不为空的
聚合查询
GET /lib3/user/_search
{
"size": 0,
"aggs": {
"age_sum": {
"avg": {
"field": "age"
}
}
}
}
再求平均年龄
复合查询
解析ES分布式架构
更新文档对于并发问题的处理
相关度分数计算
基于scroll技术的滚动搜索
java中使用
查询
Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
TransportAddress port = new TransportAddress(InetAddress.getByName("127.0.0.1"),9300);
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(port);
GetResponse res = client.prepareGet("lib3", "user", "1").execute().actionGet();
System.out.println(res);
client.close();
增加
public static void main(String[] args) throws Exception {
Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
TransportAddress port = new TransportAddress(InetAddress.getByName("127.0.0.1"),9300);
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(port);
XContentBuilder doc = XContentFactory.jsonBuilder()
.startObject()
.field("id","100")
.field("title","java设计模式")
.endObject();
IndexResponse response = client.prepareIndex("index1","blog","11").setSource(doc).get();
System.out.println(response);
client.close();
}
删除
public static void main(String[] args) throws Exception {
Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
TransportAddress port = new TransportAddress(InetAddress.getByName("127.0.0.1"),9300);
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(port);
DeleteResponse response = client.prepareDelete("index1", "blog", "10").get();
System.out.println(response);
client.close();
}
更新
public static void main(String[] args) throws Exception {
Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
TransportAddress port = new TransportAddress(InetAddress.getByName("127.0.0.1"),9300);
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(port);
UpdateRequest request = new UpdateRequest();
request.index("index1").type("blog").id("11").doc(XContentFactory.jsonBuilder().startObject().field("title","python").endObject());
UpdateResponse response = client.update(request).get();
System.out.println(response);
client.close();
}
更新或插入
Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
TransportAddress port = new TransportAddress(InetAddress.getByName("127.0.0.1"),9300);
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(port);
IndexRequest request = new IndexRequest();
request.index("index1").type("blog").id("12").source(XContentFactory.jsonBuilder().startObject().field("title","python999999").endObject());
UpdateRequest request2 = new UpdateRequest();
request2.index("index1").type("blog").id("12").doc(XContentFactory.jsonBuilder().startObject().field("title","python11111").endObject()).upsert(request);
UpdateResponse response = client.update(request2).get();
System.out.println(response);
client.close();
批量查询
public static void main(String[] args) throws Exception {
Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
TransportAddress port = new TransportAddress(InetAddress.getByName("127.0.0.1"),9300);
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(port);
MultiGetResponse response = client.prepareMultiGet().add("index1", "blog", "12").add("index1", "blog", "10").get();
for (MultiGetItemResponse respons : response.getResponses()) {
System.out.println(respons.getResponse());
}
client.close();
}
批量添加
public static void main(String[] args) throws Exception {
Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
TransportAddress port = new TransportAddress(InetAddress.getByName("127.0.0.1"),9300);
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(port);
BulkRequestBuilder bulk = client.prepareBulk("index1", "blog");
IndexRequest request = new IndexRequest();
request.source(XContentFactory.jsonBuilder().startObject().field("title","88888888").endObject());
IndexRequest request2 = new IndexRequest();
request2.source(XContentFactory.jsonBuilder().startObject().field("title","88888888").endObject());
bulk.add(request).add(request2);
bulk.execute().actionGet();
client.close();
}
查询所有
public static void main(String[] args) throws Exception {
Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
TransportAddress port = new TransportAddress(InetAddress.getByName("127.0.0.1"),9300);
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(port);
MatchAllQueryBuilder query = QueryBuilders.matchAllQuery();
SearchResponse response = client.prepareSearch("index1").setQuery(query).setSize(3).get();
response.getHits().forEach(item->{
System.out.println(item.getSourceAsString());
});
client.close();
}
public static void main(String[] args) throws Exception {
Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
TransportAddress port = new TransportAddress(InetAddress.getByName("127.0.0.1"),9300);
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(port);
MatchQueryBuilder query = QueryBuilders.matchQuery("title", "python11111");
SearchResponse response = client.prepareSearch("index1").setQuery(query).setSize(3).get();
response.getHits().forEach(item->{
System.out.println(item.getSourceAsString());
});
client.close();
}
范围查询
public static void main(String[] args) throws Exception {
Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
TransportAddress port = new TransportAddress(InetAddress.getByName("127.0.0.1"),9300);
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(port);
QueryBuilder query = QueryBuilders.rangeQuery("age").from("10").to("60");
SearchResponse response = client.prepareSearch("lib3").setQuery(query).setSize(3).get();
response.getHits().forEach(item->{
System.out.println(item.getSourceAsString());
});
client.close();
}
前缀查询
模糊查询
type查询
最大值
public static void main(String[] args) throws Exception {
Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
TransportAddress port = new TransportAddress(InetAddress.getByName("127.0.0.1"),9300);
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(port);
AggregationBuilder agg = AggregationBuilders.max("age").field("age");
SearchResponse response = client.prepareSearch("lib3").addAggregation(agg).get();
Max max = response.getAggregations().get("age");
System.out.println(max.getValue());
client.close();
}
queryString
组合查询
不计算相关度
聚合查询
分组聚合
最少匹配两个才被查出来
GET /movie/_search
{
"query": {
"match": {
"title":{
"query": "basketball love aliens",
"operator": "or",
"minimum_should_match": 2
}
}
}
}
短语查询不会进行分词
优化查询增加title的权重
中文构建和查询使用不同的分词器
通过空格进行分词
增加fielddata字段可以增加聚合功能
距离计算
重新构建分词器
POST /product/_update_by_query
{
"query":{
"bool":{
"must":[
{"term":{"name":"米"}},
{"term":{"name":"菲"}}
]
}
}
}