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
| public Page<Ocr> query(String queryString, int pageNo, int size) { String queryJson = "{\n" + " \"query\": {\n" + " \"bool\": {\n" + " \"should\": [\n" + " {\n" + " \"match\": {\n" + " \"ocrText\": \"${queryString}\"\n" + " }\n" + " },\n" + " {\n" + " \"nested\": {\n" + " \"path\": \"textResult\",\n" + " \"query\": {\n" + " \"match\": {\n" + " \"textResult.text\": \"${queryString}\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " ]\n" + " }\n" + " },\n" + " \"_source\": [\"ocrText\",\"pdfUrl\",\"id\"], \n" + " \"highlight\": {\n" + " \"fields\": {\n" + " \"textResult.text\": {},\n" + " \"ocrText\": {}\n" + " }\n" + " }\n" + "}"; queryJson = queryJson.replace("${queryString}", queryString); log.info("QueryString: {}" + queryJson); Search search = new Search.Builder(queryJson).addIndex(INDEX).addType(TYPE).build(); SearchResult searchResult = null; try { searchResult = jestClient.execute(search);
if (searchResult.isSucceeded()) { List<SearchResult.Hit<Ocr, Void>> hits = searchResult.getHits(Ocr.class); List<Ocr> ocrs = hits.stream().map(hit -> { Ocr ocr = hit.source; Map<String, List<String>> highlight = hit.highlight; if (highlight.containsKey("ocrText")) { Object arr[] = highlight.get("ocrText").toArray(); List<String> ocrTexts = new ArrayList<>(); for (Object o : arr) { ocrTexts.add((String) o); } ocr.setHlOcrText(ocrTexts); } if (highlight.containsKey("textResult.text")) { Object arr[] = highlight.get("textResult.text").toArray(); List<String> textResults = new ArrayList<>(); for (Object o : arr) { textResults.add((String) o); } ocr.setHlTextResult(textResults); } return ocr; }).collect(toList()); int took = searchResult.getJsonObject().get("took").getAsInt(); Page<Ocr> page = Page.<Ocr>builder().list(ocrs).pageNo(pageNo).size(size).total(searchResult.getTotal()).took(took).build(); return page; } else { log.warn("查询失败:index:{}, type:{}, error = {}", INDEX, TYPE, searchResult.getResponseCode()); } } catch (Exception e) { log.error("执行查询失败,抛出异常:index:{}, type:{}", INDEX, TYPE); return null; }
return null; }
@Override public Ocr queryDetail(String id, String queryString) { String queryJson = "{\n" + " \"query\": {\n" + " \"bool\": {\n" + " \"must\": [\n" + " {\n" + " \"match\": {\n" + " \"id\": \"${id}\"\n" + " }\n" + " },\n" + " {\n" + " \"nested\": {\n" + " \"path\": \"textResult\",\n" + " \"query\": {\n" + " \"match\": {\n" + " \"textResult.text\": \"${queryString}\"\n" + " }\n" + " },\n" + " \"inner_hits\":{\n" + " \"_source\":[\"textResult\"]\n" + " }\n" + " }\n" + " }\n" + " ]\n" + " }\n" + " },\n" + " \"highlight\": {\n" + " \"fields\": {\n" + " \"textResult.text\": {}\n" + " }\n" + " }\n" + "}"; queryJson = queryJson.replace("${queryString}", queryString); queryJson = queryJson.replace("${id}", id); log.info("QueryString: {}" + queryJson); Search search = new Search.Builder(queryJson).addIndex(INDEX).addType(TYPE).build(); SearchResult searchResult = null; try { searchResult = jestClient.execute(search); if (searchResult.isSucceeded()) { List<SearchResult.Hit<Ocr, Void>> hits = searchResult.getHits(Ocr.class); List<Ocr> ocrs = hits.stream().map(hit -> { Ocr ocr = hit.source; Map<String, List<String>> highlight = hit.highlight; if (highlight.containsKey("textResult.text")) { Object arr[] = highlight.get("textResult.text").toArray(); List<String> textResults = new ArrayList<>(); for (Object o : arr) { textResults.add((String) o); } ocr.setHlTextResult(textResults); } return ocr; }).collect(toList()); JsonObject innerHits = searchResult.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray().get(0).getAsJsonObject().get("inner_hits").getAsJsonObject(); JsonArray innerHitsResultArray = innerHits.get("textResult").getAsJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray(); List<TextResult> textResults = new ArrayList<>(); for (JsonElement result : innerHitsResultArray) { JsonObject source = result.getAsJsonObject().get("_source").getAsJsonObject(); TextResult textResult = new TextResult(); textResult.setCharNum(source.get("charNum").getAsInt()); textResult.setHandwritten(source.get("isHandwritten").getAsBoolean()); textResult.setLeftBottom(source.get("leftBottom").getAsString()); textResult.setLeftTop(source.get("leftTop").getAsString()); textResult.setRightBottom(source.get("rightBottom").getAsString()); textResult.setRightTop(source.get("rightTop").getAsString()); textResult.setText(source.get("text").getAsString()); textResults.add(textResult); } Ocr ocr = ocrs.get(0); ocr.setTextResult(textResults); return ocr; } else { log.error("执行查询失败,抛出异常:index:{}, type:{}", INDEX, TYPE); return null; } } catch (Exception e) { log.error("执行查询失败,抛出异常:index:{}, type:{}", INDEX, TYPE); return null; } }
|