欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java使用elasticsearch基礎(chǔ)API使用案例講解

 更新時(shí)間:2021年08月03日 10:12:30   作者:シ風(fēng)箏  
這篇文章主要介紹了Java使用elasticsearch基礎(chǔ)API使用案例講解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

1.依賴

我用的是 springboot 2.2.5.RELEASE 版本,這里只貼出主要依賴:

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
		</dependency>

2.配置+測(cè)試源碼

這里根據(jù)elasticsearch服務(wù)端的地址進(jìn)行配置:

@Configuration
public class ElasticSearchConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("127.0.0.1", 9200, "http")
                ));
        return client;
    }
}

以下是詳細(xì)的測(cè)試代碼:

@SpringBootTest
class EsApiApplicationTests {

	@Autowired
	@Qualifier(value = "restHighLevelClient")
	private RestHighLevelClient client;

	// 1.創(chuàng)建索引
	@Test
	void createIndex() throws IOException {
		// 創(chuàng)建請(qǐng)求
		CreateIndexRequest request = new CreateIndexRequest("yz_index");
		// 執(zhí)行請(qǐng)求,獲取響應(yīng)
		CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
		System.out.println("creatIndex--" + response);
	}

	// 2.獲取索引,判斷是否存在
	@Test
	void getIndex() throws IOException {
		// 創(chuàng)建請(qǐng)求
		GetIndexRequest request = new GetIndexRequest("yz_index");
		// 執(zhí)行請(qǐng)求,獲取響應(yīng)
		boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
		System.out.println("getIndex--exists--" + exists);
	}

	// 3.刪除索引
	@Test
	void deleteIndex() throws IOException {
		// 創(chuàng)建請(qǐng)求
		DeleteIndexRequest request = new DeleteIndexRequest("yz_index");
		// 執(zhí)行請(qǐng)求
		AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
		System.out.println("deleteIndex--" + response);
	}

	// 4.添加文檔
	@Test
	void addDocument() throws IOException {
		// 創(chuàng)建對(duì)象
		User user = new User("Java虛擬機(jī)", 30);
		// 創(chuàng)建索引,即獲取索引
		IndexRequest indexRequest = new IndexRequest("yz_index");
		// 添加規(guī)則 /index/_doc/id
		indexRequest.id("1");
		indexRequest.timeout(TimeValue.timeValueSeconds(1));
		// 存入對(duì)象
		indexRequest.source(JSON.toJSONString(user), XContentType.JSON);
		// 發(fā)送請(qǐng)求
		IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
		System.out.println("addDocument--" + response);
	}

	// 5.獲取文檔是否存在
	@Test
	void getDocument() throws IOException {
		// 創(chuàng)建get請(qǐng)求
		GetRequest getRequest = new GetRequest("yz_index", "1");
		// 不獲取返回的_source的上下文
		getRequest.fetchSourceContext(new FetchSourceContext(false));
		getRequest.storedFields("_none_");
		// 發(fā)送請(qǐng)求
		boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
		System.out.println("addDocument--" + exists);

		// 6.獲取文檔信息
		if (exists) {
			// 創(chuàng)建請(qǐng)求
			GetRequest getRequest1 = new GetRequest("yz_index", "1");
			// 發(fā)送請(qǐng)求
			GetResponse response = client.get(getRequest1, RequestOptions.DEFAULT);
			System.out.println("source--" + response.getSource());
			System.out.println("getDocument--" + response.getSourceAsString());
		}

		// 7.更新文檔信息
		if (exists) {
			// 創(chuàng)建請(qǐng)求
			UpdateRequest updateRequest = new UpdateRequest("yz_index", "1");
			updateRequest.timeout("1s");
			// 修改內(nèi)容
			User user = new User("小米", 10);
			updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
			UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT);
			System.out.println("updateDocument--" + response.status());
		}

		// 8.刪除文檔信息
		if (exists) {
			// 創(chuàng)建請(qǐng)求
			DeleteRequest deleteRequest = new DeleteRequest("yz_index", "1");
			deleteRequest.timeout("1s");
			// 修改內(nèi)容
			DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);
			System.out.println("deleteDocument--" + response.status());
		}
	}

	// 9.批量添加文檔
	@Test
	void batchAddDocument() throws IOException {
		// 批量請(qǐng)求
		BulkRequest bulkRequest = new BulkRequest();
		bulkRequest.timeout("10s");
		// 創(chuàng)建對(duì)象
		ArrayList<User> userArrayList = new ArrayList<>();
		userArrayList.add(new User("小米1", 1));
		userArrayList.add(new User("小米2", 2));
		userArrayList.add(new User("小米3", 3));
		userArrayList.add(new User("小米4", 4));
		userArrayList.add(new User("小米5", 5));
		// 添加請(qǐng)求
		for (int i = 0; i < userArrayList.size(); i++) {
			bulkRequest.add(new IndexRequest("yz_index")
					.id("" + (i + 2))
					.source(JSON.toJSONString(userArrayList.get(i)), XContentType.JSON));
		}
		// 執(zhí)行請(qǐng)求
		BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
		System.out.println("batchAddDocument--" + response.status());
	}

	// 10.查詢
	@Test
	void search() throws IOException {
		// 創(chuàng)建請(qǐng)求
		SearchRequest searchRequest = new SearchRequest("jd_index");
		// 構(gòu)建搜索條件
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "杜伽");// 精確查詢
		searchSourceBuilder.query(termQueryBuilder);
		searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
		searchRequest.source(searchSourceBuilder);
		// 執(zhí)行請(qǐng)求
		SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
		// 解析查詢結(jié)果
		System.out.println("search--getHists--" + JSON.toJSONString(response.getHits()));
		for (SearchHit documentFields : response.getHits()) {
			System.out.println("search--getHist--" + documentFields.getSourceAsMap());
		}
	}
}

到此這篇關(guān)于Java使用elasticsearch基礎(chǔ)API使用案例講解的文章就介紹到這了,更多相關(guān)Java使用elasticsearch基礎(chǔ)API內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • javaSE基礎(chǔ)java自定義注解原理分析

    javaSE基礎(chǔ)java自定義注解原理分析

    這篇文章主要介紹了javaSE基礎(chǔ)對(duì)java自定義注解原理分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多度進(jìn)步,早日升職加薪
    2021-10-10
  • Java實(shí)現(xiàn)LeetCode(報(bào)數(shù))

    Java實(shí)現(xiàn)LeetCode(報(bào)數(shù))

    這篇文章主要介紹了Java實(shí)現(xiàn)LeetCode(報(bào)數(shù)),本文通過使用java實(shí)現(xiàn)leetcode的報(bào)數(shù)題目和實(shí)現(xiàn)思路分析,需要的朋友可以參考下
    2021-06-06
  • MyBatis-Plus中公共字段的統(tǒng)一處理的實(shí)現(xiàn)

    MyBatis-Plus中公共字段的統(tǒng)一處理的實(shí)現(xiàn)

    在開發(fā)中經(jīng)常遇到多個(gè)實(shí)體類有共同的屬性字段,這些字段屬于公共字段,本文主要介紹了MyBatis-Plus中公共字段的統(tǒng)一處理的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • java簡(jiǎn)單實(shí)現(xiàn)計(jì)算器

    java簡(jiǎn)單實(shí)現(xiàn)計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了java簡(jiǎn)單實(shí)現(xiàn)計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • 簡(jiǎn)單了解springboot的jar包部署步驟

    簡(jiǎn)單了解springboot的jar包部署步驟

    這篇文章主要介紹了springboot的jar包部署步驟解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • java面向?qū)ο笤O(shè)計(jì)原則之單一職責(zé)與依賴倒置原則詳解

    java面向?qū)ο笤O(shè)計(jì)原則之單一職責(zé)與依賴倒置原則詳解

    這篇文章主要介紹了java面向?qū)ο笤O(shè)計(jì)原則之單一職責(zé)與依賴倒置原則的分析詳解,有需要的朋友可以借鑒參考下,希望可以有所幫助,祝大家多多進(jìn)步早日升職加薪
    2021-10-10
  • SpringCloud Zuul的使用簡(jiǎn)介

    SpringCloud Zuul的使用簡(jiǎn)介

    這篇文章主要介紹了SpringCloud Zuul的使用簡(jiǎn)介,幫助大家更好的理解和學(xué)習(xí)使用Spring Cloud,感興趣的朋友可以了解下
    2021-04-04
  • MyBatis/mybatis-plus項(xiàng)目打印SQL的方法實(shí)現(xiàn)

    MyBatis/mybatis-plus項(xiàng)目打印SQL的方法實(shí)現(xiàn)

    SpringBoot項(xiàng)目中,經(jīng)常需要打印SQL語句及其參數(shù),本文就來介紹一下MyBatis/mybatis-plus項(xiàng)目打印SQL的方法實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • Spring Boot自動(dòng)注入的原理分析

    Spring Boot自動(dòng)注入的原理分析

    這篇文章主要給大家分析介紹了關(guān)于Spring Boot自動(dòng)注入的原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Spring Boot自具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • JAVA中實(shí)現(xiàn)鏈?zhǔn)讲僮鳎ǚ椒ㄦ湥┑暮?jiǎn)單例子

    JAVA中實(shí)現(xiàn)鏈?zhǔn)讲僮鳎ǚ椒ㄦ湥┑暮?jiǎn)單例子

    這篇文章主要介紹了JAVA中實(shí)現(xiàn)鏈?zhǔn)讲僮鞯睦?模仿jQuery的方法鏈實(shí)現(xiàn),需要的朋友可以參考下
    2014-04-04

最新評(píng)論