@Test
  public void testDbsGeoEnrichment() throws IOException, InterruptedException {
    // wait for Geo Index to be set up
    int count = 0;
    while (!Index.indexExists(Constants.GEO_INDEX_TEST, geoClient)) {
      Thread.sleep(1000);
      count++;
      if (count > 30) {
        break;
      }
    }
    SearchHit responseGeoIndex =
        searchByAddressInGeodata("Grabenstr. 4", "street").getHits().getAt(0);
    assertTrue(
        "Response should contain the field location",
        responseGeoIndex.getSourceAsString().contains("geocode"));

    // check geo information was processed while transforming organanisation
    // data
    /* TODO: currently not set up for clean tests (no geodata service running)
    SearchHit responseOrganisationsIndex =
    		searchByAddressInOrganisations("Grabenstr. 4").getHits().getAt(0);
    assertTrue("Response should contain the field location",
    		responseOrganisationsIndex.getSourceAsString().contains("geo"));
    assertTrue("Response should contain latitude",
    		responseOrganisationsIndex.getSourceAsString().contains("lat"));
    assertTrue("Response should contain longitude",
    		responseOrganisationsIndex.getSourceAsString().contains("lon"));
    */
  }
 private void indexSearchHit(
     BulkNodeClient bulkNodeClient, KnapsackPullRequest request, SearchHit hit)
     throws IOException {
   IndexRequest indexRequest =
       new IndexRequest(
           mapIndex(request, hit.getIndex()),
           mapType(request, hit.getIndex(), hit.getType()),
           hit.getId());
   for (String f : hit.getFields().keySet()) {
     switch (f) {
       case "_parent":
         indexRequest.parent(hit.getFields().get(f).getValue().toString());
         break;
       case "_routing":
         indexRequest.routing(hit.getFields().get(f).getValue().toString());
         break;
       case "_timestamp":
         indexRequest.timestamp(hit.getFields().get(f).getValue().toString());
         break;
       case "_version":
         indexRequest
             .versionType(VersionType.EXTERNAL)
             .version(Long.parseLong(hit.getFields().get(f).getValue().toString()));
         break;
       case "_source":
         indexRequest.source(hit.getSourceAsString());
         break;
       default:
         indexRequest.source(f, hit.getFields().get(f).getValue().toString());
         break;
     }
   }
   if (!hit.getFields().keySet().contains("_source")) {
     indexRequest.source(hit.getSourceAsString());
   }
   bulkNodeClient.bulkIndex(indexRequest);
 }
  /**
   * @param req
   * @return
   * @throws IOException
   */
  public AgroAutocompleteResponse autocomplete(AgroAutocompleteRequest req) throws IOException {
    AgroAutocompleteResponse res = new AgroAutocompleteResponse();

    // create the SearchRequestBuilder
    SearchRequestBuilder searchReqBuilder = esClient.prepareSearch("history").setTypes("query");

    // setup query
    QueryBuilder queryBuilder;
    queryBuilder = QueryBuilders.prefixQuery("query.raw", req.getQuery());
    searchReqBuilder.setQuery(queryBuilder);

    // setup paging
    searchReqBuilder.setFrom(0);
    searchReqBuilder.setSize(200);

    // execute the request and get the response
    SearchResponse response = searchReqBuilder.execute().actionGet();

    // parse the response into AgroAutocompleteResponse
    Iterator<SearchHit> hitsIter = response.getHits().iterator();
    SearchHit hit;
    int i = 0;

    while (hitsIter.hasNext() && i < 10) {
      hit = hitsIter.next();

      String source = hit.getSourceAsString();
      UserQuery uq = objectMapper.reader(UserQuery.class).readValue(source);
      if (res.addResult(uq.getQuery())) {
        i++;
      }
    }

    // add query (meta)data to res
    res.setTotal(response.getHits().getTotalHits());
    res.setTime(response.getTookInMillis());

    // ..and
    return res;
  }
  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();
  }
 public List<T> list() {
   List<T> results = new ArrayList<>(0);
   try {
     SearchResponse response = client.prepareSearch(index).setTypes(entity).execute().actionGet();
     SearchHits hits = response.getHits();
     results = new ArrayList<>(hits.getHits().length);
     for (SearchHit hit : hits) {
       String source = hit.getSourceAsString();
       if (source == null) {
         continue;
       }
       try {
         T result = mapper.readValue(source, clazz);
         result.setId(hit.getId());
         results.add(result);
       } catch (IOException e) {
         logger.error("Error listing entities of type " + this.entity, e);
       }
     }
   } catch (IndexMissingException e) {
     logger.error("Error listing entities of type " + this.entity, e);
   }
   return results;
 }
 @Test
 public void requestCoordinates() {
   SearchHit response = searchByAddressInOrganisations("Universit��tsstr. 33").getHits().getAt(0);
   assertTrue(
       "Response should contain the field location", response.getSourceAsString().contains("geo"));
 }
Example #7
0
 @Override
 protected Network processHit(SearchHit hit) throws IOException {
   InputStream inputStream = new ByteArrayInputStream(hit.getSourceAsString().getBytes());
   return objectMapper.readValue(inputStream, Network.class);
 }