public static List<Map<String, Object>> doSearchResultEs(String value, String fq) { System.out.println("fq: " + fq); SearchHit[] shs = getFromEs(value, fq); Map<String, Object> itemContent; List<Map<String, Object>> rcdStr = new LinkedList<Map<String, Object>>(); // id:3341326TAX_CODE // score:2.1178803 // source: // { // "NUMBER_OF_CLICKS":0, // "KEY":"TAX_CODE", // "VALUE":"Tax Code", // // "FILE_PATH":"ipm/src/imaging-ui/src/oracle/imaging/axf/resources/user/ApplicationStrings.java", // "FID":41326, // "LAST_MODIFIED":"2012-03-08T21:38:33.627-08:00", // "FTID":263, // "CN":"IPM", // "FE":".java", // "DID":3, // "DN":"drop30", // "RID":3, // "RN":"11.1.1.6", // "PID":3, // "PN":"ias", // "BID":97 // } // {"NUMBER_OF_CLICKS":0,"KEY":"TAX_CODE","VALUE":"Tax // Code","FILE_PATH":"ipm/src/imaging-ui/src/oracle/imaging/axf/resources/user/ApplicationStrings.java","FID":41326,"LAST_MODIFIED":"2012-03-08T21:38:33.627-08:00","FTID":263,"CN":"IPM","FE":".java","DID":3,"DN":"drop30","RID":3,"RN":"11.1.1.6","PID":3,"PN":"ias","BID":97} String resource = null; for (SearchHit hit : shs) { itemContent = new HashMap<String, Object>(); itemContent.put("id", hit.getId()); itemContent.put("score", hit.getScore()); for (Map.Entry<String, Object> entry : hit.getSource().entrySet()) { // System.out.println("key: "+entry.getKey() + ":" + entry.getValue()); if (entry.getKey().equals("FILE_PATH")) itemContent.put("fP", entry.getValue()); else if (entry.getKey().equals("VALUE")) itemContent.put("vL", entry.getValue()); else if (entry.getKey().equals("DN")) itemContent.put("dN", entry.getValue()); else if (entry.getKey().equals("RN")) itemContent.put("rN", entry.getValue()); else if (entry.getKey().equals("PN")) itemContent.put("pN", entry.getValue()); else if (entry.getKey().equals("CN")) itemContent.put("sP", entry.getValue()); else if (entry.getKey().equals("BID")) itemContent.put("bid", entry.getValue()); else if (entry.getKey().equals("KEY")) itemContent.put("key", entry.getValue()); else if (entry.getKey().equals("FID")) itemContent.put("fid", entry.getValue()); } if (itemContent != null) { rcdStr.add(itemContent); } } return rcdStr; }
public static SearchHit[] getFromEs(String value, String fq) { // create a node client Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "esgoodluck").build(); Client client = new TransportClient(settings) .addTransportAddress(new InetSocketTransportAddress("slc08ynb.us.oracle.com", 9300)); String filterTemplate = " \"filter\": {" + "\"bool\": {" + fq + "} }"; String queryStr = "{\"query\": {\"filtered\":{\"query\":{\"multi_match\":{\"query\":\"" + value + "\", \"type\":\"most_fields\",\"fields\":[\"VALUE\",\"VALUE.UNPROCESSED^10\",\"VALUE.VALUE_ST_STEM^5\"]}}," + filterTemplate + "}},\"sort\":[\"_score\",{\"_script\":{\"script\":\"doc['VALUE.ORIGINAL'].value.length()\",\"type\":\"number\"}}]}"; SearchResponse response = client .prepareSearch("latest") .setTypes("latest") .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setSource(queryStr) .execute() .actionGet(); System.out.println("query string:" + queryStr); for (SearchHit hit : response.getHits().getHits()) { System.out.println(hit.getId()); System.out.println(hit.getScore()); System.out.println(hit.getSourceAsString()); for (Map.Entry<String, SearchHitField> entry : hit.getFields().entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } return response.getHits().getHits(); }
@Override public void describeMismatchSafely( final SearchHit searchHit, final Description mismatchDescription) { mismatchDescription.appendText(" was ").appendValue(searchHit.getScore()); }
@Override protected boolean matchesSafely(SearchHit searchHit) { return searchHit.getScore() == score; }
@Test public void testTrackScores() throws Exception { try { client.admin().indices().prepareDelete("test").execute().actionGet(); } catch (Exception e) { // ignore } client.admin().indices().prepareCreate("test").execute().actionGet(); client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet(); client .prepareIndex("test", "type1") .setSource( jsonBuilder() .startObject() .field("id", "1") .field("svalue", "aaa") .field("ivalue", 100) .field("dvalue", 0.1) .endObject()) .execute() .actionGet(); client .prepareIndex("test", "type1") .setSource( jsonBuilder() .startObject() .field("id", "2") .field("svalue", "bbb") .field("ivalue", 200) .field("dvalue", 0.2) .endObject()) .execute() .actionGet(); client.admin().indices().prepareFlush().setRefresh(true).execute().actionGet(); SearchResponse searchResponse = client .prepareSearch() .setQuery(matchAllQuery()) .addSort("svalue", SortOrder.ASC) .execute() .actionGet(); assertThat(searchResponse.getHits().getMaxScore(), equalTo(Float.NaN)); for (SearchHit hit : searchResponse.getHits()) { assertThat(hit.getScore(), equalTo(Float.NaN)); } // now check with score tracking searchResponse = client .prepareSearch() .setQuery(matchAllQuery()) .addSort("svalue", SortOrder.ASC) .setTrackScores(true) .execute() .actionGet(); assertThat(searchResponse.getHits().getMaxScore(), not(equalTo(Float.NaN))); for (SearchHit hit : searchResponse.getHits()) { assertThat(hit.getScore(), not(equalTo(Float.NaN))); } }