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
| import com.fengxuechao.FoodieSearchApp; import com.fengxuechao.es.pojo.Stu; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.SortBuilder; import org.elasticsearch.search.sort.SortOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.SearchHit; import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.document.Document; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.*; import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors;
@RunWith(SpringRunner.class) @SpringBootTest(classes = FoodieSearchApp.class) public class ESTest {
@Autowired private ElasticsearchRestTemplate esTemplate;
@Test public void createIndexStu() {
Stu stu = new Stu(); stu.setStuId(1007L); stu.setName("iron man"); stu.setAge(22); stu.setMoney(1000.8f); stu.setSign("I am iron man"); stu.setDescription("I have a spider man");
IndexQuery indexQuery = new IndexQueryBuilder().withObject(stu).build(); esTemplate.index(indexQuery, esTemplate.getIndexCoordinatesFor(stu.getClass())); }
@Test public void deleteIndexStu() { esTemplate.indexOps(Stu.class).delete(); }
@Test public void updateStuDoc() {
Map<String, Object> sourceMap = new HashMap<>(); sourceMap.put("name", "spider man"); sourceMap.put("money", 99.8f); sourceMap.put("age", 33);
IndexRequest indexRequest = new IndexRequest(); indexRequest.source(sourceMap);
UpdateQuery updateQuery = UpdateQuery.builder("1004") .withDocument(Document.from(sourceMap)) .build();
esTemplate.update(updateQuery, IndexCoordinates.of("stu")); }
@Test public void getStuDoc() { Stu stu = esTemplate.get("1004", Stu.class);
System.out.println(stu); }
@Test public void deleteStuDoc() { esTemplate.delete("1002", IndexCoordinates.of("stu")); }
@Test public void searchStuDoc() {
Pageable pageable = PageRequest.of(0, 2);
NativeSearchQuery query = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.matchQuery("description", "spider man")) .withPageable(pageable) .build(); SearchHits<Stu> hits = esTemplate.search(query, Stu.class, esTemplate.getIndexCoordinatesFor(Stu.class)); System.out.println("检索后的总分页数目为:" + hits.getTotalHits()); for (SearchHit<Stu> hit : hits) { System.out.println(hit.getContent()); }
}
@Test public void highlightStuDoc() {
String preTag = "<font color='red'>"; String postTag = "</font>";
Pageable pageable = PageRequest.of(0, 10);
SortBuilder sortBuilder = new FieldSortBuilder("money") .order(SortOrder.DESC); SortBuilder sortBuilderAge = new FieldSortBuilder("age") .order(SortOrder.ASC);
NativeSearchQuery query = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.matchQuery("description", "spider man")) .withHighlightFields(new HighlightBuilder.Field("description") .preTags(preTag) .postTags(postTag)) .withSort(sortBuilder) .withSort(sortBuilderAge) .withPageable(pageable) .build();
SearchHits<Stu> hits = esTemplate.search(query, Stu.class);
System.out.println("检索后的总分页数目为:" + hits.getTotalHits()); List<SearchHit<Stu>> list = hits.get() .peek(stuSearchHit -> stuSearchHit.getContent().setDescription(stuSearchHit.getHighlightField("description").get(0))).collect(Collectors.toList()); for (SearchHit<Stu> hit : list) { System.out.println(hit.getContent()); }
} }
|