elasticsearch中数据的插入与查询

时间:2022-11-17 13:21:11
public class ClientFactory {
private String clusterName="elasticsearch";
private String addressIp="192.168.88.122";
private boolean isClient=true;
private Node node;
Client client;
static Log log = LogFactory.getLog(ClientFactory.class);

public ClientFactory(){
node = org.elasticsearch.node.NodeBuilder.nodeBuilder().clusterName(clusterName).client(isClient).node();
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName).put("tclient.transport.sniff", true).build();
client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(addressIp, 9300));
}

public ClientFactory(String clusterName, boolean isClient){
this.clusterName=clusterName;
this.isClient=isClient;
//node = org.elasticsearch.node.NodeBuilder.nodeBuilder().clusterName(clusterName).client(isClient).node();
//Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName).put("tclient.transport.sniff", true).build();
//client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(addressIp, 9300));
}

public synchronized Client client(){
return client;
}

public String getClusterName() {
return clusterName;
}

public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}

public boolean isClient() {
return isClient;
}

public void setClient(boolean isClient) {
this.isClient = isClient;
}

@PreDestroy
public void stop(){
node.stop();
}
}

插入模块:

 public void insertElasticSearch(Map<String, Object> map) {
Client client=factory.client();
//(索引名,类型名)
IndexRequestBuilder builder=client.prepareIndex("company", "info");

if(map!=null){
String json=JSON.toJSONString(map);
IndexResponse response = builder.setId(String.valueOf(map.get("id"))).setSource(json).execute().actionGet();
}
}

查询模块:

public void  queryInfo(){

List<Map<String,String>> infoList=new ArrayList<Map<String,String>>();

Client client=factory.client();
String str ="yyx,pjx,xyx,hrx,mls,lxs,yys";
String ts = str.replaceAll(" +", " ");//将多个空格转换为一个空格," +"表示多个
String sp[] = str.split(",");
int len=sp.length;
// QueryBuilder queryBuilder = QueryBuilders.disMaxQuery()
// .add(QueryBuilders.termsQuery("filed", sp).minimumMatch(len));

// Operator operator=null;
// QueryBuilder queryBuilder = QueryBuilders.disMaxQuery()
// .add(QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("filed", "中国").operator(operator.AND))
// .add(QueryBuilders.boolQuery().must(QueryBuilders.termsQuery("name", sp)));

SearchResponse response = client.prepareSearch("company")
.setTypes("info")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(queryBuilder)
// .addHighlightedField("title")
// .setHighlighterPreTags("</span style=\"color:red\">")
// .setHighlighterPostTags("</span>")
// .addSort("date", SortOrder.DESC)
// .setFrom((pageNo - 1) * pageSize).setSize(pageSize)
.setExplain(true)
.execute()
.actionGet();

SearchHits searchHits = response.getHits();
long s = searchHits.getTotalHits();
System.out.println("total: "+s);

SearchHit[] hits = searchHits.getHits();
Object title=null;
for (int i = 0; i < hits.length; i++) {
SearchHit hit = hits[i];
title= hit.getSource().get("companyName");
System.out.println("company: "+title);
}
/**
for (int i = 0; i < hits.length; i++) {
SearchHit hit = hits[i];
Map<String, HighlightField> result = hit.highlightFields();
JSONObject json=(JSONObject) JSONObject.toJSON(result);
if(json.containsKey("title")){
HighlightField titleField = result.get("title");
Text[] titleTexts = titleField.fragments();

for(Text text : titleTexts){
title=text;
}
}else{
title= hit.getSource().get("title");
//System.out.println(hit.getSource().get("title")+" "+ hit.getSource().get("date")+" "+hit.getId());
}

System.out.println(title);
Map item=new HashMap();
item.put("title", title);
infoList.add(item);
}
**/

}