package com.it.es;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.script.mustache.SearchTemplateRequestBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import javax.xml.bind.SchemaOutputResolver;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import static org.elasticsearch.index.query.QueryBuilders.*;
/**
* @author zhuxingyu
*
*/
public class UpsertCarInfoApp {
private static TransportClient client;
/**
* @param args
*/
public static void main(String[] args) throws Exception {
//client集群自動探查client.transport.sniff使用上述的settings配置刻蟹,將client.transport.sniff設(shè)置為true即可打開集群節(jié)點(diǎn)自動探查功能
Settings setting = Settings.builder().put("cluster.name", "elasticsearch")
.put("client.transport.sniff", true).build();
client = new PreBuiltTransportClient(setting)
.addTransportAddresses(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
// upsert();
// multiGet();
//bulk();
//scroll();
// template();
// query();
geoPoint();
client.close();
}
/**
* 區(qū)域定位查詢
*/
private static void geoPoint() {
/**
* 1午衰、引入依賴
* <dependency>
* <groupId>org.locationtech.spatial4j</groupId>
* <artifactId>spatial4j</artifactId>
* <version>0.6</version>
* </dependency>
*
* <dependency>
* <groupId>com.vividsolutions</groupId>
* <artifactId>jts</artifactId>
* <version>1.13</version>
* <exclusions>
* <exclusion>
* <groupId>xerces</groupId>
* <artifactId>xercesImpl</artifactId>
* </exclusion>
* </exclusions>
* </dependency>
* 2、設(shè)置mapping
* POST /car_shop/_mapping/shops
* {
* "properties": {
* "pin": {
* "properties": {
* "location": {
* "type": "geo_point"
* }
* }
* }
* }
* }
*
* 3眷篇、設(shè)置一個4s店
* PUT /car_shop/shops/1
* {
* "name": "上海至全寶馬4S店",
* "pin" : {
* "location" : {
* "lat" : 40.12,
* "lon" : -71.34
* }
* }
* }
*/
//第一個需求:搜索兩個坐標(biāo)點(diǎn)組成的一個區(qū)域
QueryBuilder queryBuilder = geoBoundingBoxQuery("pin.location").setCorners(40.73, -74.1, 40.01, -71.12);
SearchResponse response = client.prepareSearch("car_shop").setTypes("shops")
.setQuery(queryBuilder).get();
for (SearchHit searchHit : response.getHits().getHits()) {
System.out.println(searchHit.getSourceAsString());
}
System.out.println("====================================================");
//第二個需求:指定一個區(qū)域萎河,由三個坐標(biāo)點(diǎn),組成
List<GeoPoint> points = new ArrayList<GeoPoint>();
points.add(new GeoPoint(40.73, -74.1));
points.add(new GeoPoint(40.01, -71.12));
points.add(new GeoPoint(50.56, -90.58));
response = client.prepareSearch("car_shop")
.setTypes("shops")
.setQuery(QueryBuilders.geoPolygonQuery("pin.location", points))
.get();
for (SearchHit searchHit : response.getHits().getHits()) {
System.out.println(searchHit.getSourceAsString());
}
System.out.println("====================================================");
//第三個需求:搜索距離當(dāng)前位置在200公里內(nèi)的4s店
response = client.prepareSearch("car_shop")
.setTypes("shops")
.setQuery(QueryBuilders.geoDistanceQuery("pin.location")
.point(40, -70)
.distance(200, DistanceUnit.KILOMETERS))
.get();
for (SearchHit searchHit : response.getHits().getHits()) {
System.out.println(searchHit.getSourceAsString());
}
}
/**
* 多種查詢
*/
private static void query() {
System.out.println("=====================檢索===============================");
SearchResponse searchResponse = client.prepareSearch("car_shop").setTypes("cars")
.setQuery(QueryBuilders.matchQuery("brand", "寶馬")).get();
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
System.out.println(searchHit.getSourceAsString());
}
System.out.println("========================多字段檢索============================");
searchResponse = client.prepareSearch("car_shop").setTypes("cars")
.setQuery(QueryBuilders.multiMatchQuery("奔馳", "brand", "name")).get();
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
System.out.println(searchHit.getSourceAsString());
}
System.out.println("========================termQuery過慮============================");
searchResponse = client.prepareSearch("car_shop").setTypes("cars")
.setQuery(QueryBuilders.termQuery("brand.raw", "華晨寶馬")).get();
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
System.out.println(searchHit.getSourceAsString());
}
System.out.println("========================前綴批配============================");
searchResponse = client.prepareSearch("car_shop").setTypes("cars")
.setQuery(QueryBuilders.prefixQuery("brand", "寶")).get();
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
System.out.println(searchHit.getSourceAsString());
}
System.out.println("========================多種條件的組合搜索========================");
QueryBuilder queryBuilder = boolQuery().must(matchQuery("brand", "寶馬"))
.mustNot(termQuery("name.raw", "寶馬318"))
.should(termQuery("produce_date", "2017-01-01"))
.filter(rangeQuery("price").gte("280000").lt("400000000"));
SearchResponse response = client.prepareSearch("car_shop").setTypes("cars")
.setQuery(queryBuilder).get();
for (SearchHit searchHit : response.getHits().getHits()) {
System.out.println(searchHit.getSourceAsString());
}
}
/**
* es模板調(diào)用
*/
private static void template() {
Map<String, Object> scriptParams = new HashMap();
scriptParams.put("from", 0);
scriptParams.put("size", 1);
scriptParams.put("brand", "奔馳");
//在es安裝目錄下\config\scripts\page_query_by_brand.mustache文件蕉饼,放入腳本
/**
*
*{
* "from": {{from}},
* "size": {{size}},
* "query": {
* "match": {
* "brand.keyword": "{{brand}}"
* }
* }
* }
*/
SearchResponse searchResponse = new SearchTemplateRequestBuilder(client).setScript("page_query_by_brand")
.setScriptType(ScriptType.FILE).setScriptParams(scriptParams)
.setRequest(new SearchRequest("car_shop").types("cars"))
.get().getResponse();
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
System.out.println(searchHit.getSourceAsString());
}
}
/**
* 使用Scroll批量查詢虐杯,可以實(shí)現(xiàn)文件導(dǎo)出
*/
private static void scroll() {
SearchResponse searchResponse = client.prepareSearch("car_shop").setTypes("cars")
.setQuery(QueryBuilders.termQuery("brand.raw", "奔馳"))//brand.raw是用的不分詞類型 keyword
.setScroll(new TimeValue(60000)).setSize(2).get();//setSize一次查詢多少條
int batchCount = 0;
do {
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
System.out.println("batch:" + ++batchCount);
System.out.println(searchHit.getSourceAsString());
// 每次查詢一批數(shù)據(jù),比如1000行昧港,然后寫入本地的一個excel文件中
// 如果說你一下子查詢幾十萬條數(shù)據(jù)擎椰,不現(xiàn)實(shí),jvm內(nèi)存可能都會爆掉
}
System.out.println("-----------------");
searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()).setScroll(new TimeValue(60000))
.execute().actionGet();
} while (searchResponse.getHits().getHits().length != 0);
}
/**
* bulk實(shí)現(xiàn)增刪改
* @throws IOException
*/
private static void bulk() throws IOException {
//新增
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
IndexRequestBuilder indexRequestBuilder = client.prepareIndex("car_shop", "cars", "3");
indexRequestBuilder.setSource(XContentFactory.jsonBuilder().startObject().field("brand", "奔馳")
.field("name", "奔馳C200")
.field("price", 350000)
.field("produce_date", "2017-01-20")
.field("sale_price", 320000)
.field("sale_date", "2017-01-25").endObject());
bulkRequestBuilder.add(indexRequestBuilder);
//修改
UpdateRequestBuilder update = client.prepareUpdate("car_shop", "cars", "1").setDoc(
XContentFactory.jsonBuilder().startObject().field("price", "100000000").endObject()
);
bulkRequestBuilder.add(update);
DeleteRequestBuilder delete = client.prepareDelete("car_shop", "sales", "3");
bulkRequestBuilder.add(delete);
BulkResponse bulkResponse = bulkRequestBuilder.get();
for (BulkItemResponse bulkItemResponse : bulkResponse.getItems()) {
System.out.println(bulkItemResponse.getResponse());
}
}
/**
* 一次查多個
*/
private static void multiGet() {
MultiGetResponse multiGetResponses = client.prepareMultiGet().add("car_shop", "cars", "1").add("car_shop", "cars", "2").get();
for (MultiGetItemResponse itemResponse : multiGetResponses) {
GetResponse response = itemResponse.getResponse();
if (response.isExists()) {
System.out.println(response.getSourceAsString());
}
}
}
/**
*upsert實(shí)現(xiàn)汽車最新價(jià)格的調(diào)整
* @throws IOException
* @throws InterruptedException
* @throws ExecutionException
*/
private static void upsert() throws Exception {
IndexRequest indexRequest = new IndexRequest("car_shop", "cars", "2").source(
XContentFactory.jsonBuilder()
.startObject().field("brand", "寶馬")
.field("name", "寶馬c200")
.field("price", 350000)
.field("produce_date", "2017-01-01")
.endObject());
System.out.println("index end:");
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());
}
}
三十香到、Elasticsearch的Java API使用
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門稽坤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來丈甸,“玉大人,你說我怎么就攤上這事尿褪∧览蓿” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵杖玲,是天一觀的道長顿仇。 經(jīng)常有香客問我,道長摆马,這世上最難降的妖魔是什么臼闻? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮囤采,結(jié)果婚禮上述呐,老公的妹妹穿的比我還像新娘。我一直安慰自己蕉毯,他們只是感情好乓搬,可當(dāng)我...
- 文/花漫 我一把揭開白布思犁。 她就那樣靜靜地躺著,像睡著了一般进肯。 火紅的嫁衣襯著肌膚如雪激蹲。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼着降,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了拗军?” 一聲冷哼從身側(cè)響起任洞,我...
- 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎发侵,沒想到半個月后交掏,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡刃鳄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年盅弛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片叔锐。...
- 正文 年R本政府宣布步责,位于F島的核電站返顺,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏蔓肯。R本人自食惡果不足惜遂鹊,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蔗包。 院中可真熱鬧稿辙,春花似錦、人聲如沸气忠。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽旧噪。三九已至吨娜,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間淘钟,已是汗流浹背宦赠。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像铁瞒,于是被迫代替她去往敵國和親妙色。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 首先,我使用的方式是高級客戶端(restHighLevelClient)芍碧,也就是java api的方式做了批量同步...
- 說明 在明確了ES的基本概念和使用方法后煌珊,我們來學(xué)習(xí)如何使用ES的Java API.本文假設(shè)你已經(jīng)對ES的基本概念...
- 一、實(shí)體(Entity) @Document(indexName = "my_index", type = "ne...
- Elasticsearch是基于Lucene的一個分布式全文搜索引擎泌豆。它有很多優(yōu)點(diǎn)定庵,比如分布式文件存儲,所有字段都...
- 世事多紛擾敛滋,你總要學(xué)會,一個人成長兴革。 從混沌中醒來绎晃。 每個人都希望自己不曾孤單,身邊有相愛的人悉心相伴杂曲。 可是人生...