elasticsearch 版本 6.3.0
##### 获取全部索引
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
public class SampleProgram{
public static void main(String[] args) {
String hostname = "localhost";
String clusterName = "elasticsearch";
Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.sniff", true).put("cluster.name", clusterName).build();
Client esclient = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(hostname, 9300));
String[] indexList = esclient.admin().cluster().prepareState().execute().actionGet().getState().getMetaData().concreteAllIndices();
System.out.println("Index List:");
for (String index : indexList) {
System.out.println(index);
}
}
}
#### 根据索引获取全部type
private static List<String> getAllTypeByIndex(TransportClient esClient,String indexSTR) {
List<String> typeList = new ArrayList<String>();
try {
GetMappingsResponse res = esClient.admin().indices().getMappings(new GetMappingsRequest().indices(indexSTR)).get();
ImmutableOpenMap<String, MappingMetaData> mapping = res.mappings().get(indexSTR);
for (ObjectObjectCursor<String, MappingMetaData> c : mapping) {
typeList.add(c.key);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return typeList;
}
#### 根据索引和类型获取全部字段
public ResponseData getIndexPropertiesByName(RequestData requestData) {
ResponseData responseData = new ResponseData();
List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
String indexSTR = requestData.getParam().get("index").toString(); // 获取索引字段
List<String> tempTypeList = getAllTypeByIndex(esClient,indexSTR);
String tempType = tempTypeList.get(0);
List<String> fieldList = new ArrayList<String>();
ClusterState cs = esClient.admin().cluster().prepareState().setIndices(indexSTR).execute().actionGet().getState();
IndexMetaData imd = cs.getMetaData().index(indexSTR);
MappingMetaData mdd = imd.mapping(tempType);
Map<String, Object> map = null;
map = mdd.getSourceAsMap();
fieldList = getList("", map);
for (String field : fieldList) {
Map fieldMap=new HashMap();
fieldMap.put("property",field);
list.add(fieldMap);
}
responseData.setData(list);
return responseData;
}
private static List<String> getList(String fieldName, Map<String, Object> mapProperties) {
List<String> fieldList = new ArrayList<String>();
Map<String, Object> map = (Map<String, Object>) mapProperties.get("properties");
Set<String> keys = map.keySet();
for (String key : keys) {
if (((Map<String, Object>) map.get(key)).containsKey("type")) {
fieldList.add(fieldName + "" + key);
} else {
List<String> tempList = getList(fieldName + "" + key + ".", (Map<String, Object>) map.get(key));
fieldList.addAll(tempList);
}
}
return fieldList;
}
#### 根据条件和过滤条件查询,并且高亮过滤条件字段
// 设置高亮
HighlightBuilder hiBuilder = new HighlightBuilder().field("*"); // "*" 代表全部过滤字段
hiBuilder.preTags("<eam>"); // 这里可以改标签
hiBuilder.postTags("</eam>");
// bool 层级关系 看图1
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.queryStringQuery("张三")); // 查询条件
List<QueryBuilder> filter = boolQueryBuilder.filter();
filter.add( QueryBuilders.rangeQuery("age",gt(20)) ) // 年龄大于20
filter.add( QueryBuilders.rangeQuery("monthlyPay",gt(20)) )// 月资大于 2000
SearchResponse response = esClient.prepareSearch(index) // 索引
.setTypes(type) // type
.setQuery(boolQueryBuilder) // 过滤条件
.setFrom(0).setSize(20) // 0-20 条数据
.highlighter(hiBuilder) // 高亮
.execute().actionGet();
SearchHits hits = response.getHits();
for(int i = 0; i < hits.getHits().length; i++) {
System.out.println(hits.getHits()[i].getSourceAsString());
}
// 意思是查询,全部张三有关的,并且年龄大于20和月薪大于2000的
#### 图1