23个最有用的ES检索技巧(Java API实现)

这篇具有很好参考价值的文章主要介绍了23个最有用的ES检索技巧(Java API实现)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

es全文检索开发java,java,elasticsearch,开发语言

前言

本文是对 23个最有用的Elasticseaerch检索技巧 一文提到的ES检索技巧进行 Java API 的简单实现,但仅限于简单实现,并不考虑包括参数校验,异常处理,日志处理,安全等问题,仅供参考

代码见 UsefullESSearchSkill ,原查询语句请对照原文es全文检索开发java,java,elasticsearch,开发语言

运行环境

JDK version : 10.0.2gradle version : 4.7 Elasticsearch version : 6.3.2 IDEA version : 2018.2

运行前请启动 ES 实例,并修改 application.properties 文件中的ES配置

类介绍

实体类 Book

注意:日期 publish_date 的类型设置为 String 是避免 Java 到 ES 之间复杂的转换工作,在ES中该字段仍然被识别为 date 类型

public class Book {public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");private String id;private String title;private List<String> authors;private String summary;private String publish_date;private Integer num_reviews;private String publisher;...
} 
公共类 Constants

定义了一些常用的常量

public class Constants {// 字段名public static String ID = "id";public static String TITLE = "title";public static String AUTHORS = "authors";public static String SUMMARY = "summary";public static String PUBLISHDATE = "publish_date";public static String PUBLISHER = "publisher";public static String NUM_REVIEWS = "num_reviews";public static String TITLE_KEYWORD = "title.keyword";public static String PUBLISHER_KEYWORD = "publisher.keyword";// 过滤要返回的字段public static String[] fetchFieldsTSPD = {ID, TITLE, SUMMARY, PUBLISHDATE};public static String[] fetchFieldsTA = {ID, TITLE, AUTHORS};public static String[] fetchFieldsSA = {ID, SUMMARY, AUTHORS};public static String[] fetchFieldsTSA = {ID, TITLE, SUMMARY, AUTHORS};public static String[] fetchFieldsTPPD = {ID, TITLE, PUBLISHER, PUBLISHDATE};public static String[] fetchFieldsTSPN = {ID, TITLE, SUMMARY, PUBLISHER, NUM_REVIEWS};// 高亮public static HighlightBuilder highlightS = new HighlightBuilder().field(SUMMARY);
} 
公共类 EsConfig

创建 ES 客户端实例,ES 客户端用于与 ES 集群进行交互

@Configuration
public class EsConfig {@Value("${elasticsearch.cluster-nodes}")private String clusterNodes;@Value("${elasticsearch.cluster-name}")private String clusterName;@Beanpublic Client client() {Settings settings = Settings.builder().put("cluster.name", clusterName).put("client.transport.sniff", true).build();TransportClient client = new PreBuiltTransportClient(settings);try {if (clusterNodes != null && !"".equals(clusterNodes)) {for (String node : clusterNodes.split(",")) {String[] nodeInfo = node.split(":");client.addTransportAddress(new TransportAddress(InetAddress.getByName(nodeInfo[0]), Integer.parseInt(nodeInfo[1])));}}} catch (UnknownHostException e) {}return client;}
} 
数据获取工具类 DataUtil

这里的数据也就是 23个最有用的ES检索技巧 文中用于实验的4条数据

public class DataUtil {public static SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");/** * 模拟获取数据 */public static List<Book> batchData() {List<Book> list = new LinkedList<>();Book book1 = new Book("1", "Elasticsearch: The Definitive Guide", Arrays.asList("clinton gormley", "zachary tong"),"A distibuted real-time search and analytics engine", "2015-02-07", 20, "oreilly");Book book2 = new Book("2", "Taming Text: How to Find, Organize, and Manipulate It", Arrays.asList("grant ingersoll", "thomas morton", "drew farris"),"organize text using approaches such as full-text search, proper name recognition, clustering, tagging, information extraction, and summarization","2013-01-24", 12, "manning");Book book3 = new Book("3", "Elasticsearch in Action", Arrays.asList("radu gheorge", "matthew lee hinman", "roy russo"),"build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms","2015-12-03", 18, "manning");Book book4 = new Book("4", "Solr in Action", Arrays.asList("trey grainger", "timothy potter"), "Comprehensive guide to implementing a scalable search engine using Apache Solr","2014-04-05", 23, "manning");list.add(book1);list.add(book2);list.add(book3);list.add(book4);return list;}public static Date parseDate(String dateStr) {try {return dateFormater.parse(dateStr);} catch (ParseException e) {}return null;} 
公共查询工具类 CommonQueryUtils

对执行完ES查询请求后的数据进行解析

public class CommonQueryUtils {public static Gson gson = new GsonBuilder().setDateFormat("YYYY-MM-dd").create();/** * 处理ES返回的数据,封装 */public static List<Book> parseResponse(SearchResponse searchResponse) {List<Book> list = new LinkedList<>();for (SearchHit hit : searchResponse.getHits().getHits()) {// 用gson直接解析Book book = gson.fromJson(hit.getSourceAsString(), Book.class);list.add(book);}return list;}/** * 解析完数据后,构建 Response 对象 */public static Response<List<Book>> buildResponse(SearchResponse searchResponse) {// 超时处理if (searchResponse.isTimedOut()) {return new Response<>(ResponseCode.ESTIMEOUT);}// 处理ES返回的数据List<Book> list = parseResponse(searchResponse);// 有shard执行失败if (searchResponse.getFailedShards() > 0) {return new Response<>(ResponseCode.FAILEDSHARDS, list);}return new Response<>(ResponseCode.OK, list);}...
} 

数据准备

BulkTests

创建索引,以及使用 bulk API 批量插入数据

@RunWith(SpringRunner.class)
@SpringBootTest
public class BulkTests {// 在Test中 Autowired需要引入包 org.elasticsearch.plugin:transport-netty4-client:6.3.2,否则异常找不到Transport类@Autowiredprivate Client client;@Value("${elasticsearch.bookIndex}")private String bookIndex;@Value("${elasticsearch.bookType}")private String bookType;private Gson gson = new GsonBuilder().setDateFormat("YYYY-MM-dd").create();/** * 创建索引,设置 settings,设置mappings */@Testpublic void createIndex() {int settingShards = 1;int settingReplicas = 0;// 判断索引是否存在,存在则删除IndicesExistsResponse indicesExistsResponse = client.admin().indices().prepareExists(bookIndex).get();if (indicesExistsResponse.isExists()) {System.out.println("索引 " + bookIndex + " 存在!");// 删除索引,防止报异常ResourceAlreadyExistsException[index [bookdb_index/yL05ZfXFQ4GjgOEM5x8tFQ] already existsDeleteIndexResponse deleteResponse = client.admin().indices().prepareDelete(bookIndex).get();if (deleteResponse.isAcknowledged()){System.out.println("索引" + bookIndex + "已删除");}else {System.out.println("索引" + bookIndex + "删除失败");}} else {System.out.println("索引 " + bookIndex + " 不存在!");}// 设置SettingsCreateIndexResponse response = client.admin().indices().prepareCreate(bookIndex).setSettings(Settings.builder().put("index.number_of_shards", settingShards).put("index.number_of_replicas", settingReplicas)).get();// 查看结果GetSettingsResponse getSettingsResponse = client.admin().indices().prepareGetSettings(bookIndex).get();System.out.println("索引设置结果");for (ObjectObjectCursor<String, Settings> cursor : getSettingsResponse.getIndexToSettings()) {String index = cursor.key;Settings settings = cursor.value;Integer shards = settings.getAsInt("index.number_of_shards", null);Integer replicas = settings.getAsInt("index.number_of_replicas", null);System.out.println("index:" + index + ", shards:" + shards + ", replicas:" + replicas);Assert.assertEquals(java.util.Optional.of(settingShards), java.util.Optional.of(shards));Assert.assertEquals(java.util.Optional.of(settingReplicas), java.util.Optional.of(replicas));}}/** * Bulk 批量插入数据 */@Testpublic void bulk() {List<Book> list = DateUtil.batchData();BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();// 添加index操作到 bulk 中list.forEach(book -> {// 新版的API中使用setSource时,参数的个数必须是偶数,否则需要加上 setSource(json, XContentType.JSON)bulkRequestBuilder.add(client.prepareIndex(bookIndex, bookType, book.getId()).setSource(gson.toJson(book), XContentType.JSON));});BulkResponse responses = bulkRequestBuilder.get();if (responses.hasFailures()) {// bulk有失败for (BulkItemResponse res : responses) {System.out.println(res.getFailure());}Assert.assertTrue(false);}}
} 

开始查询

控制类

查询接口文章来源地址https://www.toymoban.com/news/detail-564244.html

@RestController
@RequestMapping("basicmatch")
public class BasicMatchQueryController {@Autowiredprivate BasicMatchQueryService basicMatchQueryService;/** * 1.1 对 "guide" 执行全文检索 * 测试:http://localhost:8080/basicmatch/multimatch?query=guide */@RequestMapping("multimatch")public Response<List<Book>> multiMatch(@RequestParam(value = "query", required = true) String query) {return basicMatchQueryService.multiBatch(query);}/** * 1.2 指定特定字段检索 * 测试:http://localhost:8080/basicmatch/match?title=in action&from=0&size=4 */@RequestMapping("match")public ResponsePage<List<Book>> match(MatchForm form) {return basicMatchQueryService.match(form);}/** * 2 对 "guide" 执行多字段检索 * 测试:http://localhost:8080/basicmatch/multifield?query=guide */@RequestMapping("multifield")public Response<List<Book>> multiField(@RequestParam(value = "query", required = true) String query) {return basicMatchQueryService.multiField(query);}/** * 3、 Boosting提升某字段得分的检索( Boosting): 将“摘要”字段的得分提高了3倍 * 测试:http://localhost:8080/basicmatch/multifieldboost?query=elasticsearch guide */@RequestMapping("multifieldboost")public Response<List<Book>> multiFieldboost(@RequestParam(value = "query", required = true) String query) {return basicMatchQueryService.multiFieldboost(query);}/** * 4、Bool检索( Bool Query) * 测试:http://localhost:8080/basicmatch/bool?shouldTitles=Elasticsearch&shouldTitles=Solr&mustAuthors=clinton gormely&mustNotAuthors=radu gheorge */@RequestMapping("bool")public Response<List<Book>> bool(@ModelAttribute BoolForm form) {return basicMatchQueryService.bool(form);}/** * 5、 Fuzzy 模糊检索( Fuzzy Queries) */@RequestMapping("fuzzy")public Response<List<Book>> fuzzy(String query) {return basicMatchQueryService.fuzzy(query);}/** * 6、 Wildcard Query 通配符检索 * 测试:http://localhost:8080/basicmatch/wildcard?pattern=t* */@RequestMapping("wildcard")public Response<List<Book>> wildcard(String pattern) {return basicMatchQueryService.wildcard(Constants.AUTHORS, pattern);}/** * 7、正则表达式检索( Regexp Query) * 测试:http://localhost:8080/basicmatch/regexp */@RequestMapping("regexp")public Response<List<Book>> regexp(String regexp) {// 由于Tomcat的原因,直接接收有特殊字符的 正则表达式 会异常,所以这里写死,不过多探究// 若regexp = "t[a-z]*y";return basicMatchQueryService.regexp(Constants.AUTHORS, regexp);}/** * 8、匹配短语检索( Match Phrase Query) * 测试:http://localhost:8080/basicmatch/phrase?query=search engine */@RequestMapping("phrase")public Response<List<Book>> phrase(String query) {return basicMatchQueryService.phrase(query);}/** * 9、匹配词组前缀检索 * 测试:http://localhost:8080/basicmatch/phraseprefix?query=search en */@RequestMapping("phraseprefix")public Response<List<Book>> phrasePrefix(String query) {return basicMatchQueryService.phrasePrefix(query);}/** * 10、字符串检索( Query String) * 测试:http://localhost:8080/basicmatch/querystring?query=(saerch~1 algorithm~1) AND (grant ingersoll)OR (tom morton) */@RequestMapping("querystring")public Response<List<Book>> queryString(String query) {return basicMatchQueryService.queryString(query);}/** * 11、简化的字符串检索 (Simple Query String) * 测试:http://localhost:8080/basicmatch/simplequerystring?query=(saerch~1 algorithm~1) AND (grant ingersoll)OR (tom morton) */@RequestMapping("simplequerystring")public Response<List<Book>> simplequerystring(String query) {// 这里写死,仅为测试query = "(saerch~1 algorithm~1) + (grant ingersoll)| (tom morton)";return basicMatchQueryService.simpleQueryString(query);}/** * 12、Term=检索(指定字段检索) * 测试:http://localhost:8080/basicmatch/term?query=manning */@RequestMapping("term")public Response<List<Book>> term(String query) {return basicMatchQueryService.term(query);}/** * 13、Term排序检索-(Term Query - Sorted) * 测试:http://localhost:8080/basicmatch/termsort?query=manning */@RequestMapping("termsort")public Response<List<Book>> termsort(String query) {return basicMatchQueryService.termsort(query);}/** * 14、范围检索(Range query) * 测试:http://localhost:8080/basicmatch/range?startDate=2015-01-01&endDate=2015-12-31 */@RequestMapping("range")public Response<List<Book>> range(String startDate, String endDate) {return basicMatchQueryService.range(startDate, endDate);}/** * 15. 过滤检索 * 测试:http://localhost:8080/basicmatch/filter?query=elasticsearch&gte=20 */@RequestMapping("filter")public Response<List<Book>> filter(String query, Integer gte, Integer lte) {return basicMatchQueryService.filter(query, gte, lte);}/** * 17、 Function 得分:Field值因子( Function Score: Field Value Factor) * 测试:http://localhost:8080/basicmatch/fieldvaluefactor?query=search engine */@RequestMapping("fieldvaluefactor")public Response<List<Book>> fieldValueFactor(String query) {return basicMatchQueryService.fieldValueFactor(query);}/** * 18、 Function 得分:衰减函数( Function Score: Decay Functions ) * 测试:http://localhost:8080/basicmatch/decay?query=search engines&origin=2014-06-15 */@RequestMapping("decay")public Response<List<Book>> decay(String query, @RequestParam(value = "origin", defaultValue = "2014-06-15") String origin) {return basicMatchQueryService.decay(query, origin);}/** * 19、Function得分:脚本得分( Function Score: Script Scoring ) * 测试:ES需要配置允许groovy脚本运行才可以 */@RequestMapping("script")public Response<List<Book>> script(String query, @RequestParam(value = "threshold", defaultValue = "2015-07-30") String threshold) {return basicMatchQueryService.script(query, threshold);}
} 

服务类

@Service
public class BasicMatchQueryService {@Autowiredprivate Client client;@Value("${elasticsearch.bookIndex}")private String bookIndex;@Value("${elasticsearch.bookType}")private String bookType;/** * 进行ES查询,执行请求前后打印出 查询语句 和 查询结果 */private SearchResponse requestGet(String queryName, SearchRequestBuilder requestBuilder) {System.out.println(queryName + " 构建的查询:" + requestBuilder.toString());SearchResponse searchResponse = requestBuilder.get();System.out.println(queryName + " 搜索结果:" + searchResponse.toString());return searchResponse;}...
} 
1.1 对 “guide” 执行全文检索 Multi Match Query
 public Response<List<Book>> multiBatch(String query) {MultiMatchQueryBuilder queryBuilder = new MultiMatchQueryBuilder(query);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(queryBuilder);SearchResponse searchResponse = requestGet("multiBatch", requestBuilder);return CommonQueryUtils.buildResponse(searchResponse);} 
1.2 在标题字段(title)中搜索带有 “in action” 字样的图书
 public ResponsePage<List<Book>> match(MatchForm form) {MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("title", form.getTitle());// 高亮HighlightBuilder highlightBuilder = new HighlightBuilder().field("title").fragmentSize(200);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(matchQueryBuilder).setFrom(form.getFrom()).setSize(form.getSize()).highlighter(highlightBuilder)// 设置 _source 要返回的字段.setFetchSource(Constants.fetchFieldsTSPD, null);...} 
多字段检索 (Multi-field Search)
 public Response<List<Book>> multiField(String query) {MultiMatchQueryBuilder queryBuilder = new MultiMatchQueryBuilder(query).field("title").field("summary");SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(queryBuilder);...} 
3、 Boosting提升某字段得分的检索( Boosting),将“摘要”字段的得分提高了3倍
 public Response<List<Book>> multiFieldboost(String query) {MultiMatchQueryBuilder queryBuilder = new MultiMatchQueryBuilder(query).field("title").field("summary", 3);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(queryBuilder);...} 
4、Bool检索( Bool Query)
 /** * 在标题中搜索一本名为 "Elasticsearch" 或 "Solr" 的书, * AND由 "clinton gormley" 创作,但NOT由 "radu gheorge" 创作 */public Response<List<Book>> bool(BoolForm form) {BoolQueryBuilder boolQuery = new BoolQueryBuilder();// 搜索标题 shouldBoolQueryBuilder shouldTitleBool = new BoolQueryBuilder();form.getShouldTitles().forEach(title -> {shouldTitleBool.should().add(new MatchQueryBuilder("title", title));});boolQuery.must().add(shouldTitleBool);// match 作者form.getMustAuthors().forEach(author -> {boolQuery.must().add(new MatchQueryBuilder("authors", author));});// not match 作者form.getMustNotAuthors().forEach(author -> {boolQuery.mustNot().add(new MatchQueryBuilder("authors", author));});...} 
5、 Fuzzy 模糊检索( Fuzzy Queries)
 public Response<List<Book>> fuzzy(String query) {MultiMatchQueryBuilder queryBuilder = new MultiMatchQueryBuilder(query).field("title").field("summary").fuzziness(Fuzziness.AUTO);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(queryBuilder).setFetchSource(Constants.fetchFieldsTSPD, null).setSize(2);...} 
6、 Wildcard Query 通配符检索
 /** * 要查找具有以 "t" 字母开头的作者的所有记录 */public Response<List<Book>> wildcard(String fieldName, String pattern) {WildcardQueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder(fieldName, pattern);HighlightBuilder highlightBuilder = new HighlightBuilder().field(Constants.AUTHORS, 200);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(wildcardQueryBuilder).setFetchSource(Constants.fetchFieldsTA, null).highlighter(highlightBuilder);} 
7、正则表达式检索( Regexp Query)
 public Response<List<Book>> regexp(String fieldName, String regexp) {RegexpQueryBuilder queryBuilder = new RegexpQueryBuilder(fieldName, regexp);HighlightBuilder highlightBuilder = new HighlightBuilder().field(Constants.AUTHORS);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setQuery(queryBuilder).setTypes(bookType).highlighter(highlightBuilder).setFetchSource(Constants.fetchFieldsTA, null);} 
8、匹配短语检索( Match Phrase Query)
 public Response<List<Book>> phrase(String query) {MultiMatchQueryBuilder queryBuilder = new MultiMatchQueryBuilder(query).field(Constants.TITLE).field(Constants.SUMMARY).type(MultiMatchQueryBuilder.Type.PHRASE).slop(3);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(queryBuilder).setFetchSource(Constants.fetchFieldsTSPD, null);} 
9、匹配词组前缀检索
 public Response<List<Book>> phrasePrefix(String query) {MatchPhrasePrefixQueryBuilder queryBuilder = new MatchPhrasePrefixQueryBuilder(Constants.SUMMARY, query).slop(3).maxExpansions(10);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(queryBuilder).setFetchSource(Constants.fetchFieldsTSPD, null);} 
10、字符串检索( Query String)
 public Response<List<Book>> queryString(String query) {QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder(query);queryBuilder.field(Constants.SUMMARY, 2).field(Constants.TITLE).field(Constants.AUTHORS).field(Constants.PUBLISHER);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(queryBuilder).setFetchSource(Constants.fetchFieldsTSA, null);} 
11、简化的字符串检索 (Simple Query String)
 public Response<List<Book>> simpleQueryString(String query) {SimpleQueryStringBuilder queryBuilder = new SimpleQueryStringBuilder(query);queryBuilder.field(Constants.SUMMARY, 2).field(Constants.TITLE).field(Constants.AUTHORS).field(Constants.PUBLISHER);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(queryBuilder).setFetchSource(Constants.fetchFieldsTSA, null).highlighter(Constants.highlightS);} 
12、Term/Terms检索(指定字段检索)
 public Response<List<Book>> term(String query) {TermQueryBuilder termQueryBuilder = new TermQueryBuilder(Constants.PUBLISHER, query);// terms 查询/*String[] values = {"manning", "oreilly"};TermsQueryBuilder termsQueryBuilder = new TermsQueryBuilder(Constants.PUBLISHER, values);*/SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(termQueryBuilder).setFetchSource(Constants.fetchFieldsTPPD, null);} 
13、Term排序检索-(Term Query - Sorted)
 public Response<List<Book>> termsort(String query) {TermQueryBuilder termQueryBuilder = new TermQueryBuilder(Constants.PUBLISHER, query);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(termQueryBuilder).addSort(Constants.PUBLISHER_KEYWORD, SortOrder.DESC).addSort(Constants.TITLE_KEYWORD, SortOrder.ASC).setFetchSource(Constants.fetchFieldsTPPD, null);} 
14、范围检索(Range query)
 public Response<List<Book>> range(String startDate, String endDate) {RangeQueryBuilder queryBuilder = new RangeQueryBuilder(Constants.PUBLISHDATE).gte(startDate).lte(endDate);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(queryBuilder).setFetchSource(Constants.fetchFieldsTPPD, null);} 
15. 过滤检索
 public Response<List<Book>> filter(String query, Integer gte, Integer lte) {BoolQueryBuilder queryBuilder = new BoolQueryBuilder();queryBuilder.must().add(new MultiMatchQueryBuilder(query).field(Constants.TITLE).field(Constants.SUMMARY));if (gte != null || lte != null) {RangeQueryBuilder rangeQueryBuilder = new RangeQueryBuilder(Constants.NUM_REVIEWS);if (gte != null) {rangeQueryBuilder.gte(gte);}if (lte != null) {rangeQueryBuilder.lte(lte);}queryBuilder.filter().add(rangeQueryBuilder);}SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(queryBuilder).setFetchSource(Constants.fetchFieldsTSPN, null);} 
17、 Function 得分:Field值因子( Function Score: Field Value Factor)
 public Response<List<Book>> fieldValueFactor(String query) {// queryMultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder(query).field(Constants.TITLE).field(Constants.SUMMARY);// fieldValueFactorFieldValueFactorFunctionBuilder fieldValueFactor = ScoreFunctionBuilders.fieldValueFactorFunction(Constants.NUM_REVIEWS).factor(2).modifier(FieldValueFactorFunction.Modifier.LOG1P);// functionscoreFunctionScoreQueryBuilder queryBuilder = QueryBuilders.functionScoreQuery(multiMatchQueryBuilder, fieldValueFactor);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(queryBuilder).setFetchSource(Constants.fetchFieldsTSPN, null);} 
18、 Function 得分:衰减函数( Function Score: Decay Functions )
 public Response<List<Book>> decay(String query, String origin) {MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder(query).field(Constants.TITLE).field(Constants.SUMMARY);ExponentialDecayFunctionBuilder exp = ScoreFunctionBuilders.exponentialDecayFunction(Constants.PUBLISHDATE, origin, "30d", "7d");FunctionScoreQueryBuilder queryBuilder = QueryBuilders.functionScoreQuery(multiMatchQueryBuilder, exp).boostMode(CombineFunction.REPLACE);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(queryBuilder).setFetchSource(Constants.fetchFieldsTSPN, null);} 
19、Function得分:脚本得分( Function Score: Script Scoring )
 public Response<List<Book>> script(String query, String threshold) {MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder(query).field(Constants.TITLE).field(Constants.SUMMARY);// 参数Map<String, Object> params = new HashMap<>();params.put("threshold", threshold);// 脚本String scriptStr = "publish_date = doc['publish_date'].value; num_reviews = doc['num_reviews'].value; if (publish_date > Date.parse('yyyy-MM-dd', threshold).getTime()) { return log(2.5 + num_reviews) }; return log(1 + num_reviews);";Script script = new Script(ScriptType.INLINE, "painless", scriptStr, params);ScriptScoreFunctionBuilder scriptScoreFunctionBuilder = ScoreFunctionBuilders.scriptFunction(script);FunctionScoreQueryBuilder queryBuilder = QueryBuilders.functionScoreQuery(multiMatchQueryBuilder, scriptScoreFunctionBuilder);SearchRequestBuilder requestBuilder = client.prepareSearch(bookIndex).setTypes(bookType).setQuery(queryBuilder).setFetchSource(Constants.fetchFieldsTSPN, null);} 

到了这里,关于23个最有用的ES检索技巧(Java API实现)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 全文检索[ES系列] - 第495篇

    历史文章( 文章 累计490+) 《国内最全的Spring Boot系列之一》 《国内最全的Spring Boot系列之二》 《国内最全的Spring Boot系列之三》 《国内最全的Spring Boot系列之四》 《国内最全的Spring Boot系列之五》 《国内最全的Spring Boot系列之六》 Mybatis-Plus通用枚举功能 [MyBatis-Plus系列

    2024年02月04日
    浏览(18)
  • 商城-学习整理-高级-全文检索-ES(九)

    https://www.elastic.co/cn/what-is/elasticsearch Elastic 的底层是开源库 Lucene。但是,你没法直接用 Lucene,必须自己写代码去调用它的接口。Elastic 是 Lucene 的封装,提供了 REST API 的操作接口,开箱即用。 REST API:天然的跨平台。 官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/cur

    2024年02月12日
    浏览(15)
  • ES+微服务对文档进行全文检索

    打开ES服务 进入es安装目录下F:elasticsearch-7.17.1bin,双击elasticsearch.bat,如图 成功后,如图 2. 打开ES可视化服务 进入安装F:elasticsearch-head-master路径下,执行npm run start 3. 打开浏览器 参考文献:https://blog.csdn.net/mjl1125/article/details/121975950

    2024年02月11日
    浏览(15)
  • 第八章 全文检索【上】+商品添加ES + 商品热度排名

    根据用户输入的检索条件,查询出对用的商品 首页的分类  搜索栏 1.3.1 建立mapping! 这时我们要思考三个问题: 哪些字段需要分词  例如:商品名称 我们用哪些字段进行过滤 平台属性值 分类Id 品牌Id 哪些字段我们需要通过搜索查询出来。 商品名称,价格,图片等。 以上分析

    2024年02月09日
    浏览(9)
  • Java调用Elasticsearch API实现全文检索,搭配MinIO文件存储

    应用背景: 对存储在MinIO服务器的文件实现全文检索。也可以是其他服务器或本地文件,本文仅详细介绍MinIO文件的读取及转换。通过Elasticsearch的Ingest-Attachment插件抽取文件内容,支持Word、Excel、PDF、TXT等格式文件,无需手动解析文件内容。 上代码,详细解释可以阅读注释、

    2024年02月11日
    浏览(14)
  • 【ElasticSearch-基础篇】ES高级查询Query DSL全文检索

    和术语级别查询(Term-Level Queries)不同,全文检索查询(Full Text Queries)旨在 基于相关性搜索和匹配文本数据 。这些查询会对输入的文本进行分析,将其 拆分 为词项(单个单词),并执行诸如分词、词干处理和标准化等操作。 全文检索的关键特点: 对输入的文本进行分析

    2024年01月22日
    浏览(17)
  • ES全文检索pdf、word、txt等文本文件内容

    需求: 用ES对上传文件内容的检索和高亮显示。 之前从事于物联网行业,从多年前了解ES以后没有使用过,本篇文章就是为了记录小白用ES完成工作的过程。 Elasticsearch的介绍、安装和环境这里不过多介绍,网上有很多。 思考: 文本搜索,文本需要上传elasticsearch。支持

    2024年04月11日
    浏览(20)
  • 17、全文检索 -- Elasticsearch -- 使用 反应式 RestClient (ReactiveElasticsearchClient)操作 Es 服务器(增、删、查 :索引库和文档)

    Elasticsearch 所提供 RestHighLevelClient 本身提供了 【同步编程】 和 【异步编程】两种模型。 Elasticsearch 官方并未提供反应式的 RestClient : 因此 Spring Data Elasticsearch 额外补充了一个 ReactiveElasticsearchClient,用于提供反应式API支持, ReactiveElasticsearchClient 相当于 RestHighLevelClient 的反应式

    2024年04月28日
    浏览(13)
  • ES是一个分布式全文检索框架,隐藏了复杂的处理机制,核心数据分片机制、集群发现、分片负载均衡请求路由

    ES是一个分布式框架,隐藏了复杂的处理机制,核心数据分片机制、集群发现、分片负载均衡请求路由。 ES的高可用架构,总体如下图: 说明:本文会以pdf格式持续更新,更多最新尼恩3高pdf笔记,请从下面的链接获取:语雀 或者 码云 ES基本概念名词 Cluster 代表一个集群,集

    2024年02月10日
    浏览(11)
  • 18、全文检索--Elasticsearch-- SpringBoot 整合 Spring Data Elasticsearch(异步方式(Reactive)和 传统同步方式 分别操作ES的代码演示)

    启动命令行窗口,执行:elasticsearch 命令即可启动 Elasticsearch 服务器 三种查询方式解释: 方法名查询: 就是全自动查询,只要按照规则来定义查询方法 ,Spring Data Elasticsearch 就会帮我们生成对应的查询语句,并且生成方法体。 @Query 查询 : 就是半自动查询, 按照 S

    2024年03月12日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包