做一个汽车零售数据的mapping,我们要做的第一份数据,其实汽车信息\
设置mappings
PUT /car_shop { "mappings": { "cars": { "properties": { "brand": { "type": "text", "analyzer": "ik_max_word", "fields": { "raw": { "type": "keyword" } } }, "name": { "type": "text", "analyzer": "ik_max_word", "fields": { "raw": { "type": "keyword" } } } } } } } |
首先的话呢,第一次调整宝马320这个汽车的售价,我们希望将售价设置为32万,用一个upsert语法,如果这个汽车的信息之前不存在,那么就insert,如果存在,那么就update
基于upsert实现最新价格的调整
package com.es.core.senior;
import java.net.InetAddress;
import org.elasticsearch.action.ActionFuture; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.transport.client.PreBuiltTransportClient;
public class UpsertCarInfoApp {
@SuppressWarnings({"unchecked", "resource"}) public static void main(String[] args) throws Exception { Settings settings = Settings.builder() .put("cluster.name", "elasticsearch") .put("client.transport.sniff", true) .build(); TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); //汽车信息的保存-->如果id不存在则进行插入 IndexRequest indexRequest = new IndexRequest("car_shop", "cars", "1") .source(XContentFactory.jsonBuilder() .startObject() .field("brand", "宝马") .field("name", "宝马320") .field("price", 320000) .field("produce_date", "2017-01-01") .endObject());
//信息的更新。如果存在,则进行更新 UpdateRequest updateRequest = new UpdateRequest("car_shop", "cars", "1") .doc(XContentFactory.jsonBuilder() .startObject() .field("price", 310000)//降价 .endObject() ).upsert(indexRequest);
UpdateResponse updateResponse = client.update(updateRequest).get(); System.out.println(updateResponse.getVersion()); client.close();
}
} |
|
查看结果:
GET /car_shop/cars/_search 结果: { "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "car_shop", "_type": "cars", "_id": "1", "_score": 1, "_source": { "brand": "宝马", "name": "宝马320", "price": 320000, "produce_date": "2017-01-01" } } ] } } |
再执行一遍
package com.es.core.senior;
import java.net.InetAddress;
import org.elasticsearch.action.ActionFuture; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.transport.client.PreBuiltTransportClient;
public class UpsertCarInfoApp {
@SuppressWarnings({"unchecked", "resource"}) public static void main(String[] args) throws Exception { Settings settings = Settings.builder() .put("cluster.name", "elasticsearch") .put("client.transport.sniff", true) .build(); TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); //汽车信息的保存-->如果id不存在则进行插入 IndexRequest indexRequest = new IndexRequest("car_shop", "cars", "1") .source(XContentFactory.jsonBuilder() .startObject() .field("brand", "宝马") .field("name", "宝马320") .field("price", 320000) .field("produce_date", "2017-01-01") .endObject());
//信息的更新。如果存在,则进行更新 UpdateRequest updateRequest = new UpdateRequest("car_shop", "cars", "1") .doc(XContentFactory.jsonBuilder() .startObject() .field("price", 310000)//降价 .endObject() ).upsert(indexRequest);
UpdateResponse updateResponse = client.update(updateRequest).get(); System.out.println(updateResponse.getVersion()); client.close();
}
} |
查看结果:
GET /car_shop/cars/_search 结果: { "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "car_shop", "_type": "cars", "_id": "1", "_score": 1, "_source": { "brand": "宝马", "name": "宝马320", "price": 310000, "produce_date": "2017-01-01" } } ] } } |