4:elasticsearch 서비스 구현
8625 단어 elasticsearch
import java.util.List;
import java.util.Map;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.engine.DocumentMissingException;
import org.elasticsearch.index.engine.VersionConflictEngineException;
public class IIndexServiceIml implements IIndexService {
@Override
public boolean insertOne(SearchBean t) {
// TODO Auto-generated method stub
try {
Client client = ClientHelper.getClient();
IndexRequestBuilder inserter = client.prepareIndex(t.getIndexName(),t.getType(),t.getId());
inserter.setSource(SearchBeanUtil.toInsertMap(t));
inserter.execute().actionGet();
return true;
} catch (Exception e) {
// TODO: handle exception
}
return false;
}
@Override
public boolean insertList(List lists) {
try {
Client client = ClientHelper.getClient();
BulkRequestBuilder bulk = new BulkRequestBuilder(client);
for (SearchBean b : lists) {
IndexRequestBuilder inserter = client.prepareIndex(b.getIndexName(),
b.getType(),b.getId());
inserter.setSource(SearchBeanUtil.toInsertMap(b));
bulk.add(inserter);
}
BulkResponse actionGet = bulk.execute().actionGet();
if (actionGet.hasFailures()) {
return false;
}
return true;
} catch (Exception e) {
}
return false;
}
@Override
public boolean deleteOne(SearchBean t) {
Client client = ClientHelper.getClient();
try {
DeleteRequestBuilder deleter = client.prepareDelete(t.getIndexName(), t.getType(),t.getId());
DeleteResponse actionGet = deleter.execute().actionGet();
if (!actionGet.isFound()) {
return true;
}
} catch (Exception e) {
return false;
}
return true;
}
@Override
public boolean deleteList(List lists) {
try {
Client client = ClientHelper.getClient();
BulkRequestBuilder bulk = new BulkRequestBuilder(client);
for (SearchBean t : lists) {
DeleteRequestBuilder deleter = client.prepareDelete(t.getIndexName(), t.getType(),t.getId());
bulk.add(deleter);
}
BulkResponse actionGet = bulk.execute().actionGet();
if (actionGet.hasFailures()) {
return false;
}
return true;
} catch (Exception e) {
}
return false;
}
@Override
public boolean updateOne(SearchBean t) {
Client client = ClientHelper.getClient();
try {
client.prepareUpdate(t.getIndexName(),t.getType(), t.getId())
.setId( t.getId()).setDoc(SearchBeanUtil.toUpdateMap(t)).execute()
.actionGet();
return true;
} catch (DocumentMissingException e) {// , true
e.printStackTrace();
return true;
} catch (VersionConflictEngineException e) {
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean udateList(List lists) {
try {
Client client = ClientHelper.getClient();
BulkRequestBuilder bulk = new BulkRequestBuilder(client);
for (SearchBean t : lists) {
UpdateRequestBuilder update=client.prepareUpdate(t.getIndexName(),t.getType(), t.getId())
.setId( t.getId()).setDoc(SearchBeanUtil.toUpdateMap(t));
bulk.add(update);
}
BulkResponse actionGet = bulk.execute().actionGet();
if (actionGet.hasFailures()) {
return false;
}
return true;
} catch (Exception e) {
}
return false;
}
@Override
public List search(String jsonQuery, String indexName, int page, int size) {
try {
int start = page maps) {
boolean fa=false;
try {
Client client = ClientHelper.getClient();
UpdateRequest resuest=new UpdateRequest(indexName, type,id);
resuest.script(script);
resuest.scriptParams(maps);
client.update(resuest).actionGet();
fa=true;
} catch (Exception e) {
fa=false;
}
return fa;
}
}
public class SearchBean {
public SearchBean(String indexName, String type, String id) {
super();
this.indexName = indexName;
this.type = type;
this.id = id;
}
private String indexName;//
private String type;//
private String id;// ID
private String name;
private String password;
public String getIndexName() {
return indexName;
}
public void setIndexName(String indexName) {
this.indexName = indexName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
public class SearchBeanUtil {
/**
* filedName get
*
* @param fildeName
* @return
* @throws Exception
*/
private static String getMethodName(String fildeName) throws Exception {
byte[] items = fildeName.getBytes();
if (((char) items[0]) >= 'a' && ((char) items[0]) <= 'z') {
items[0] = (byte) ((char) items[0] - 32);
}
return new String(items);
}
public static Map toUpdateMap(SearchBean bean) {
Class> type = bean.getClass();
Map returnMap = new HashMap();
try {
Field[] fields = type.getDeclaredFields();
for (int i = 0; i toInsertMap(SearchBean bean){
Map returnMap = new HashMap();
try {
Class> type = bean.getClass();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i > lists=new ArrayList<>();
for(SearchHit hit:result.getHits()){
lists.add(hit.getSource());
}
return lists;
}
public static List buildSearchFields(SearchHits result){
List
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
kafka connect e elasticsearch를 관찰할 수 있습니다.No menu lateral do dashboard tem a opção de connectors onde ele mostra todos os clusters do kafka connect conectados atu...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.