elasticsearch 查询 - 查询所有数据
查询数据分两种方式 一种是使用head插件 图形化查询 一种是Java接口方式查询
head插件查询 统一使用POST方式 因为POST方式可以传递参数
查询所有数据 方式1 restfull api
方式1 restfull api
GET http://192.168.199.126:9200/film/dongzuo/_search/

查询所有数据方式2 Java接口实现
package com.et.index;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @Author: ETJAVA
* @CreateTime: 2024-04-09 09:18
* @Description: TODO 查询索引
* @Version: 1.0
*/
public class SearchIndex {
private TransportClient client;
private static String host="192.168.199.126";
private static int port=9300; // 程序连接的端口
// 配置settings 集群相关
private static Settings.Builder settings = Settings.builder().put("cluster.name","my-application");
@Before
public void init() throws UnknownHostException {
client = new PreBuiltTransportClient(settings.build())
.addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
}
@After
public void close(){
if(client!=null){
client.close();
}
}
/**
* 查询全部索引
*/
@Test
public void searchAll(){
// prepareSearch 可以同时查询多个索引名称,使用逗号隔开
SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
// 设置查询所有 actionGet
SearchResponse sr=srb.setQuery(QueryBuilders.matchAllQuery()).execute().actionGet();
// 获取返回结果
SearchHits hits=sr.getHits();
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}
}
}
