|
| 1 | +package io.ymq.example.elasticsearch; |
| 2 | + |
| 3 | + |
| 4 | +import io.ymq.example.elasticsearch.run.Startup; |
| 5 | +import org.elasticsearch.action.index.IndexResponse; |
| 6 | +import org.elasticsearch.action.search.SearchRequestBuilder; |
| 7 | +import org.elasticsearch.action.search.SearchResponse; |
| 8 | +import org.elasticsearch.client.transport.TransportClient; |
| 9 | +import org.elasticsearch.index.query.QueryBuilders; |
| 10 | +import org.elasticsearch.search.SearchHit; |
| 11 | + |
| 12 | +import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.boot.test.context.SpringBootTest; |
| 19 | +import org.springframework.test.context.junit4.SpringRunner; |
| 20 | + |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.UUID; |
| 24 | + |
| 25 | +/** |
| 26 | + * 单元测试 |
| 27 | + */ |
| 28 | +@RunWith(SpringRunner.class) |
| 29 | +@SpringBootTest(classes = Startup.class) |
| 30 | +public class BaseTests { |
| 31 | + |
| 32 | + private static final Logger LOGGER = LoggerFactory.getLogger(BaseTests.class); |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private TransportClient transportClient; |
| 36 | + |
| 37 | + /** |
| 38 | + * ES的基本类型可以去官网查看 |
| 39 | + * 如果您使用map的方式去写入数据并且创建索引,es会自动根据map的value数据类型来自动转换 |
| 40 | + * 比如age是int,es里面使用有integer,不赘述 |
| 41 | + * 使用map有个大缺陷(除非自己封装对象保存),当你保存java.util.Date类型进去的时候ES会全部转成UTC来保存 |
| 42 | + * 这个只能通过后面的api方式定义索引field的一些属性来指定才行 |
| 43 | + **/ |
| 44 | + @Test |
| 45 | + public void createData() { |
| 46 | + Map<String, Object> map = new HashMap<String, Object>(); |
| 47 | + |
| 48 | + map.put("name", "鹏磊"); |
| 49 | + map.put("age", 24); |
| 50 | + map.put("interests", new String[]{"阅读", "学习"}); |
| 51 | + map.put("about", "我是鹏磊,认识自己的无知是认识世界的最可靠的方法。"); |
| 52 | + |
| 53 | + /* |
| 54 | + map.put("name", "鹏磊"); |
| 55 | + map.put("age", 21); |
| 56 | + map.put("interests", new String[]{"阅读", "学习"}); |
| 57 | + map.put("about", "我是鹏磊,世界上没有优秀的理念,只有脚踏实地的结果"); |
| 58 | + */ |
| 59 | + |
| 60 | + IndexResponse response = transportClient.prepareIndex("about_index", "about", UUID.randomUUID().toString()).setSource(map).get(); |
| 61 | + |
| 62 | + System.out.println("写入数据结果=" + response.status().getStatus() + "!id=" + response.getId()); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * match 使用,会被分词查询 |
| 68 | + */ |
| 69 | + @Test |
| 70 | + public void match() { |
| 71 | + SearchRequestBuilder requestBuilder = transportClient.prepareSearch("about_index").setTypes("about") |
| 72 | + .setQuery(QueryBuilders.matchQuery("about", "鹏磊")); |
| 73 | + System.out.println(requestBuilder.toString()); |
| 74 | + |
| 75 | + SearchResponse response = requestBuilder.execute().actionGet(); |
| 76 | + |
| 77 | + long totalHits = response.getHits().totalHits; |
| 78 | + long length = response.getHits().getHits().length; |
| 79 | + LOGGER.debug("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length); |
| 80 | + |
| 81 | + System.out.println(response.status()); |
| 82 | + if (response.status().getStatus() == 200) { |
| 83 | + for (SearchHit hits : response.getHits().getHits()) { |
| 84 | + System.out.println(hits.getSourceAsString()); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + /** |
| 91 | + * matchphrase使用,短语精准匹配 |
| 92 | + * 不使用matchPhraseQuery会导致 rock climbing被拆分查询 |
| 93 | + */ |
| 94 | + @Test |
| 95 | + public void matchPhrase() { |
| 96 | + SearchRequestBuilder requestBuilder = transportClient.prepareSearch("about_index").setTypes("about") |
| 97 | + .setQuery(QueryBuilders.matchPhraseQuery("about", "鹏磊")); |
| 98 | + System.out.println(requestBuilder.toString()); |
| 99 | + |
| 100 | + SearchResponse response = requestBuilder.execute().actionGet(); |
| 101 | + |
| 102 | + long totalHits = response.getHits().totalHits; |
| 103 | + long length = response.getHits().getHits().length; |
| 104 | + LOGGER.debug("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length); |
| 105 | + |
| 106 | + System.out.println(response.status()); |
| 107 | + if (response.status().getStatus() == 200) { |
| 108 | + for (SearchHit hits : response.getHits().getHits()) { |
| 109 | + System.out.println(hits.getSourceAsString()); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * 高亮显示 |
| 116 | + */ |
| 117 | + @Test |
| 118 | + public void highlight() { |
| 119 | + HighlightBuilder highlightBuilder = new HighlightBuilder(); |
| 120 | + // highlightBuilder.preTags(FragmentSettings.prefix);//设置前缀 |
| 121 | + // highlightBuilder.postTags(FragmentSettings.subfix);//设置后缀 |
| 122 | + highlightBuilder.field("about"); |
| 123 | + // highlightBuilder.fragmenter(FragmentSettings.SPAN) |
| 124 | + // .fragmentSize(FragmentSettings.HIGHLIGHT_MAX_WORDS).numOfFragments(5); |
| 125 | + SearchRequestBuilder requestBuilder = transportClient.prepareSearch("about_index").setTypes("about") |
| 126 | + .setQuery(QueryBuilders.matchPhraseQuery("about", "鹏磊")).highlighter(highlightBuilder); |
| 127 | + System.out.println(requestBuilder.toString()); |
| 128 | + |
| 129 | + SearchResponse response = requestBuilder.execute().actionGet(); |
| 130 | + |
| 131 | + long totalHits = response.getHits().totalHits; |
| 132 | + long length = response.getHits().getHits().length; |
| 133 | + LOGGER.debug("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length); |
| 134 | + |
| 135 | + System.out.println(response.status()); |
| 136 | + if (response.status().getStatus() == 200) { |
| 137 | + for (SearchHit hits : response.getHits().getHits()) { |
| 138 | + System.out.println(hits.getSourceAsString()); |
| 139 | + // 这里使用hight field来覆盖source里面的字段即可 |
| 140 | + System.out.println(hits.getHighlightFields()); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | +} |
0 commit comments