java 连接操作elasticsearch(四)
- 与关系查询
/** * &-与关系查询 * * @param parentheses 合并查询关键词 使用&&连接的 * @param except_keywords 排除关键词 * @param field1,field2 */ @Autowired public static BoolQueryBuilder getDataByMultiCondition(String parentheses, String except_keywords, String field1, String field2) { BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); BoolQueryBuilder areaQueryBuilder = QueryBuilders.boolQuery(); BoolQueryBuilder personQueryBuilder = QueryBuilders.boolQuery(); BoolQueryBuilder eventQueryBuilder = QueryBuilders.boolQuery(); String[] strArray = parentheses.split("\\)"); for (int i = 0; i < strArray.length; i++) { String subStr = strArray[i].replaceAll("\\(|\\)", ""); String[] fieldArray = new String[] { field1, field2 }; if (subStr != null && !"".equals(subStr)) { String regEx = "&"; //切割关键词 Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(subStr); String regEx2 = "\\|\\|"; Pattern p2 = Pattern.compile(regEx2); Matcher m2 = p2.matcher(subStr); String[] subStrArray = {}; if (m.find(1)) { subStrArray = subStr.split(regEx); for (int k = 0; k < fieldArray.length; k++) { for (int j = 0; j < subStrArray.length; j++) { if (i == 0) { areaQueryBuilder.must(QueryBuilders.matchPhraseQuery(fieldArray[k], subStrArray[j])); } else if (i == 1) { personQueryBuilder.must(QueryBuilders.matchPhraseQuery(fieldArray[k], subStrArray[j])); } else { eventQueryBuilder.must(QueryBuilders.matchPhraseQuery(fieldArray[k], subStrArray[j])); } } } } else if (m2.find(1)) { subStrArray = subStr.split(regEx2); for (int k = 0; k < fieldArray.length; k++) { for (int j = 0; j < subStrArray.length; j++) { if (i == 0) { areaQueryBuilder.should(QueryBuilders.matchPhraseQuery(fieldArray[k], subStrArray[j])); // System.out.println("area-should--subStr--" + // subStr + "--" + fieldArray[k] + ": "+ // subStrArray[j]); } else if (i == 1) { personQueryBuilder .should(QueryBuilders.matchPhraseQuery(fieldArray[k], subStrArray[j])); // System.out.println("person-should--subStr--" // + subStr + "--" + fieldArray[k] + ": "+ // subStrArray[j]); } else { eventQueryBuilder.should(QueryBuilders.matchPhraseQuery(fieldArray[k], subStrArray[j])); // System.out.println("event-should--subStr--" + // subStr + "--" + fieldArray[k] + ": "+ // subStrArray[j]); } } } } else { subStrArray = subStr.split(regEx); for (int k = 0; k < fieldArray.length; k++) { for (int j = 0; j < subStrArray.length; j++) { if (i == 0) { areaQueryBuilder.must(QueryBuilders.matchPhraseQuery(fieldArray[k], subStrArray[j])); // System.out.println("area-single--subStr--" + // subStr + "--" + fieldArray[k] + ": "+ // subStrArray[j]); } else if (i == 1) { personQueryBuilder.must(QueryBuilders.matchPhraseQuery(fieldArray[k], subStrArray[j])); // System.out.println("person-single--subStr--" // + subStr + "--" + fieldArray[k] + ": "+ // subStrArray[j]); } else { eventQueryBuilder.must(QueryBuilders.matchPhraseQuery(fieldArray[k], subStrArray[j])); // System.out.println("event-single--subStr--" + // subStr + "--" + fieldArray[k] + ": "+ // subStrArray[j]); } } } } } } if (areaQueryBuilder.hasClauses() == true && personQueryBuilder.hasClauses() == true && eventQueryBuilder.hasClauses() == true) { boolQueryBuilder.must(areaQueryBuilder); boolQueryBuilder.must(personQueryBuilder); boolQueryBuilder.must(eventQueryBuilder); } else if (areaQueryBuilder.hasClauses() == true && personQueryBuilder.hasClauses() == true && eventQueryBuilder.hasClauses() == false) { boolQueryBuilder.must(areaQueryBuilder); boolQueryBuilder.must(personQueryBuilder); } else if (areaQueryBuilder.hasClauses() == true && personQueryBuilder.hasClauses() == false && eventQueryBuilder.hasClauses() == true) { boolQueryBuilder.must(areaQueryBuilder); boolQueryBuilder.must(eventQueryBuilder); } else if (areaQueryBuilder.hasClauses() == false && personQueryBuilder.hasClauses() == true && eventQueryBuilder.hasClauses() == true) { boolQueryBuilder.must(personQueryBuilder).must(eventQueryBuilder); } else if (areaQueryBuilder.hasClauses() == true && personQueryBuilder.hasClauses() == false && eventQueryBuilder.hasClauses() == false) { boolQueryBuilder.must(areaQueryBuilder); } else if (areaQueryBuilder.hasClauses() == false && personQueryBuilder.hasClauses() == true && eventQueryBuilder.hasClauses() == false) { boolQueryBuilder.must(personQueryBuilder); } else if (areaQueryBuilder.hasClauses() == false && personQueryBuilder.hasClauses() == false && eventQueryBuilder.hasClauses() == true) { boolQueryBuilder.must(eventQueryBuilder); } if (field1 != null && field2 != null && field1 != field2) { if (except_keywords != null && !"".equals(except_keywords)) { String regEx3 = "\\|\\|"; Pattern p3 = Pattern.compile(regEx3); Matcher m3 = p3.matcher(except_keywords); if (m3.find(1)) { String[] exceptKwsArray = {}; exceptKwsArray = except_keywords.split(regEx3); for (int k = 0; k < exceptKwsArray.length; k++) { boolQueryBuilder.mustNot(QueryBuilders.matchPhraseQuery(field1, exceptKwsArray[k])); boolQueryBuilder.mustNot(QueryBuilders.matchPhraseQuery(field2, exceptKwsArray[k])); } } } } return boolQueryBuilder; }
- 多个索引index表查询返回结果集SearchHits关键词高亮显示
public static SearchHits getResponseMultiResult(String[] indexsName, String type, TransportClient client, BoolQueryBuilder boolQueryBuilder, String sortStr, int pageNum, int recordNum, String field1, String field2) { SearchHits hits = null; if (indexsName.length > 0) { try { // 设置高亮属性 HighlightBuilder highlightBuilder = new HighlightBuilder(); highlightBuilder.preTags("<span style=\"color:red\">").postTags("</span>").field(field1).field(field2); // 设置ES查询属性 SearchResponse response = null; if (sortStr != null && !"".equals(sortStr)) { String[] sortArray = sortStr.split("###"); // 判断排序方式 SortOrder sortFlag = null; if ("+".equals(sortArray[1])) { sortFlag = SortOrder.ASC; } else { sortFlag = SortOrder.DESC; } // 执行ES查询 if ("articlesSort".equals(sortArray[0])) { // 按ES库中的_score得分排序 response = client.prepareSearch(indexsName).setTypes(type).setQuery(boolQueryBuilder) .highlighter(highlightBuilder).addSort(SortBuilders.scoreSort().order(sortFlag)) .setFrom(pageNum).setSize(recordNum).setExplain(true).execute().actionGet(); } else { // 按排序字段进行排序 response = client.prepareSearch(indexsName).setTypes(type).setQuery(boolQueryBuilder) .highlighter(highlightBuilder).addSort(sortArray[0], sortFlag).setFrom(pageNum) .setSize(recordNum).setExplain(true).execute().actionGet(); } } else { // 无排序 response = client.prepareSearch(indexsName).setTypes(type).setQuery(boolQueryBuilder) .highlighter(highlightBuilder).setFrom(pageNum).setSize(recordNum).setExplain(true) .execute().actionGet(); } hits = response.getHits(); } catch (Exception e) { e.printStackTrace(); } } return hits; }
- java处理hits集合
StringBuffer sb=new StringBuffer(); Findindex find = new Findindex(); try { find = mapper.readValue(hit.getSourceAsString(), Findindex.class); find.setId(hit.getId()); find.setIndex(hit.getIndex()); //添加索引名 if(!"微博".equals(find.getSource_type())){ //标题涉及词高亮显示 HighlightField hTitle = hit.getHighlightFields().get("title"); if (hTitle != null) { Text[] fragments = hTitle.fragments(); String hTitleStr = ""; for (Text text : fragments) { hTitleStr += text; String[] asr=text.toString().replaceAll("</span><span style=\"color:red\">","").split("</span>"); for (String string : asr) { if(string.indexOf("<span")!=-1){ try { /**style="color:red">的长度是是18,所以+18, * 为了避免其他地方出现>导致关键词挖取混乱,尽量使用长一点的字符串挖取 */ String s = string.substring(string.indexOf("style=\"color:red\">") + 18,string.length()); if(sb.toString().indexOf(s)==-1){ sb.append(s+" "); } } catch (Exception e) { } } } } find.setTitle(hTitleStr); } } //内容涉及词高亮显示 HighlightField hContent = hit.getHighlightFields().get("main_body"); if (hContent != null) { Text[] fragments = hContent.fragments(); String hContentStr = ""; for (Text text : fragments) { hContentStr += text; String[] asr=text.toString().replaceAll("</span><span style=\"color:red\">","").split("</span>"); for (String string : asr) { if(string.indexOf("<span")!=-1){ try { /**style="color:red">的长度是是18,所以+18, * 为了避免其他地方出现>导致关键词挖取混乱,尽量使用长一点的字符串挖取 */ String s = string.substring(string.indexOf("style=\"color:red\">") + 18,string.length()); if(sb.toString().indexOf(s)==-1){ sb.append(s+" "); } } catch (Exception e) { } } } } find.setMain_body(hContentStr); } //是否已读 EsStatus stsatus=select(find.getId(),userId); if(stsatus==null||stsatus.getRead()==null){ find.setRead(false); }else{ find.setRead(true); } find.setKeyword(sb.toString()); //添加涉及词 String content_fingerprint = find.getContent_fingerprint(); if(article==null||article.get(content_fingerprint)==null){ //article==null为不合并,后者为只有一条数据 find.setSimilarity(1); }else{ find.setSimilarity(article.get(content_fingerprint)); //添加相似文章数 } list.add(find); } catch (Exception e) { e.printStackTrace(); } }
小白技术分享