1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| package top.lrshuai.es.dao.impl;
import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException;
import org.apache.log4j.Logger; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexResponse; 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.UpdateResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.RangeQueryBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
import top.lrshuai.es.dao.PersonDao; import top.lrshuai.es.entity.Person;
@Component public class PersonDaoImpl implements PersonDao{
@Autowired private TransportClient transportClient; private Logger log = Logger.getLogger(getClass()); private String index = "test"; private String type = "person";
@Override public String save(Person person) { try { XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); builder.field("name", person.getName()); builder.field("age", person.getAge()); builder.field("sex", person.getSex()); builder.field("birthday", person.getBirthday()); builder.field("introduce", person.getIntroduce()); builder.endObject(); IndexResponse response = this.transportClient.prepareIndex(index, type) .setSource(builder).get(); return response.getId(); } catch (IOException e) { e.printStackTrace(); log.error(e.getMessage(), e); } return null; }
@Override public String update(Person person) { UpdateRequest request = new UpdateRequest(index, type, person.getId()); try { XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); if (person.getName() != null) { builder.field("name", person.getName()); } if (person.getSex() != null) { builder.field("sex", person.getSex()); } if (person.getIntroduce() != null) { builder.field("introduce", person.getIntroduce()); } if (person.getBirthday() != null) { builder.field("birthday", person.getBirthday()); } if (person.getAge() > 0) { builder.field("age", person.getAge()); } builder.endObject(); request.doc(builder); UpdateResponse response = transportClient.update(request).get(); return response.getId(); } catch (IOException | InterruptedException | ExecutionException e) { log.error(e.getMessage(), e); } return null; } @Override public String deltele(String id) { DeleteResponse response = transportClient.prepareDelete(index, type, id).get(); return response.getId(); } @Override public Object find(String id) { GetResponse response =transportClient.prepareGet(index, type, id).get(); System.out.println("response="+response); Map<String, Object> result = response.getSource(); if(result != null) { result.put("_id", response.getId()); } return result; } @Override public Object query(Person person) { List<Map<String,Object>> result = new ArrayList<>();; try { BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery(); if (person.getName() != null) {
boolBuilder.should(QueryBuilders.matchQuery("name", person.getName())); } if (person.getIntroduce() != null) {
boolBuilder.should(QueryBuilders.matchQuery("introduce", person.getIntroduce())); } if(person.getAge() > 0) { RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age"); rangeQuery.from(person.getAge()); rangeQuery.to(person.getAge()+10); boolBuilder.filter(rangeQuery); } SearchRequestBuilder builder = transportClient.prepareSearch(index) .setTypes(type) .setSearchType(SearchType.QUERY_THEN_FETCH) .setQuery(boolBuilder) .setFrom(0) .setSize(10); log.info(String.valueOf(builder)); SearchResponse response = builder.get(); response.getHits().forEach((s)->result.add(s.getSource())); } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage(), e); } return result; } }
|