void assertRealtimeGetWorks(String indexName) {
    assertAcked(
        client()
            .admin()
            .indices()
            .prepareUpdateSettings(indexName)
            .setSettings(Settings.builder().put("refresh_interval", -1).build()));
    SearchRequestBuilder searchReq =
        client().prepareSearch(indexName).setQuery(QueryBuilders.matchAllQuery());
    SearchHit hit = searchReq.get().getHits().getAt(0);
    String docId = hit.getId();
    // foo is new, it is not a field in the generated index
    client().prepareUpdate(indexName, "doc", docId).setDoc("foo", "bar").get();
    GetResponse getRsp = client().prepareGet(indexName, "doc", docId).get();
    Map<String, Object> source = getRsp.getSourceAsMap();
    assertThat(source, Matchers.hasKey("foo"));

    assertAcked(
        client()
            .admin()
            .indices()
            .prepareUpdateSettings(indexName)
            .setSettings(
                Settings.builder()
                    .put("refresh_interval", EngineConfig.DEFAULT_REFRESH_INTERVAL)
                    .build()));
  }
  /**
   * Remove the documents from ElasticSearch that are not present in uris
   *
   * @param uris uris that should be present in the index.
   * @return true if the action completed, false if it failed during the process.
   */
  private int removeMissingUris(Set<String> uris) {
    int searchKeepAlive = 60000;
    int count = 0;

    SearchResponse response =
        client
            .prepareSearch()
            .setIndices(indexName)
            .setTypes(typeName)
            .setScroll(new TimeValue(searchKeepAlive))
            .setQuery(QueryBuilders.matchAllQuery())
            .execute()
            .actionGet();

    while (response.getHits().getHits().length > 0) {
      for (SearchHit hit : response.getHits()) {
        if (uris.contains(hit.getId())) continue;

        DeleteResponse deleteResponse =
            client.prepareDelete(indexName, typeName, hit.getId()).execute().actionGet();
        if (deleteResponse.isFound()) count++;
      }

      response =
          client
              .prepareSearchScroll(response.getScrollId())
              .setScroll(new TimeValue(searchKeepAlive))
              .execute()
              .actionGet();
    }
    return count;
  }
  @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"));
    */
  }
Example #4
0
  void assertAllSearchWorks(String indexName) {
    logger.info("--> testing _all search");
    SearchResponse searchRsp = client().prepareSearch(indexName).get();
    ElasticsearchAssertions.assertNoFailures(searchRsp);
    assertThat(searchRsp.getHits().getTotalHits(), greaterThanOrEqualTo(1L));
    SearchHit bestHit = searchRsp.getHits().getAt(0);

    // Make sure there are payloads and they are taken into account for the score
    // the 'string' field has a boost of 4 in the mappings so it should get a payload boost
    String stringValue = (String) bestHit.sourceAsMap().get("string");
    assertNotNull(stringValue);
    Explanation explanation =
        client()
            .prepareExplain(indexName, bestHit.getType(), bestHit.getId())
            .setQuery(QueryBuilders.matchQuery("_all", stringValue))
            .get()
            .getExplanation();
    assertTrue(
        "Could not find payload boost in explanation\n" + explanation,
        findPayloadBoostInExplanation(explanation));

    // Make sure the query can run on the whole index
    searchRsp =
        client()
            .prepareSearch(indexName)
            .setQuery(QueryBuilders.matchQuery("_all", stringValue))
            .setExplain(true)
            .get();
    ElasticsearchAssertions.assertNoFailures(searchRsp);
    assertThat(searchRsp.getHits().getTotalHits(), greaterThanOrEqualTo(1L));
  }
Example #5
0
  private void InitTasks() throws Exception {
    QueryBuilder builder = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("_id", "_meta"));

    SearchRequestBuilder search = client.prepareSearch("_river");
    search.setTypes(river.riverName().name());
    search.setQuery(builder);
    SearchResponse resp = search.execute().actionGet();

    int hitCount = 0;
    for (SearchHit hit : resp.hits().getHits()) {
      logger.info(
          "Task Manager: Query response hits[ "
              + Integer.toString(hitCount)
              + "]: "
              + hit.sourceAsString());
      hitCount++;

      Map<String, Object> sourceMap = hit.sourceAsMap();
      Map<String, Object> my = (Map<String, Object>) sourceMap.get("my");
      ArrayList arr = (ArrayList) my.get("tasks");
      for (Object taskObj : arr) {
        Task newTask = new Task((Map<String, String>) taskObj, client, river.riverName().name());
        taskArr.add(newTask);
        taskMap.put(newTask.id(), newTask);
      }
    }

    currentTaskIndex = 0;
    currentTask = (Task) taskArr.get(currentTaskIndex);
  }
 /**
  * @param spaceKey to get info for
  * @return spaces indexing info or null if not found.
  */
 protected SpaceIndexingInfo getLastSpaceIndexingInfo(String spaceKey) {
   SpaceIndexingInfo lastIndexing = lastSpaceIndexingInfo.get(spaceKey);
   if (lastIndexing == null && activityLogIndexName != null) {
     try {
       refreshSearchIndex(activityLogIndexName);
       SearchResponse sr =
           client
               .prepareSearch(activityLogIndexName)
               .setTypes(activityLogTypeName)
               .setPostFilter(
                   FilterBuilders.andFilter(
                       FilterBuilders.termFilter(SpaceIndexingInfo.DOCFIELD_SPACE_KEY, spaceKey),
                       FilterBuilders.termFilter(
                           SpaceIndexingInfo.DOCFIELD_RIVER_NAME, riverName().getName())))
               .setQuery(QueryBuilders.matchAllQuery())
               .addSort(SpaceIndexingInfo.DOCFIELD_START_DATE, SortOrder.DESC)
               .addField("_source")
               .setSize(1)
               .execute()
               .actionGet();
       if (sr.getHits().getTotalHits() > 0) {
         SearchHit hit = sr.getHits().getAt(0);
         lastIndexing = SpaceIndexingInfo.readFromDocument(hit.sourceAsMap());
       } else {
         logger.debug("No last indexing info found in activity log for space {}", spaceKey);
       }
     } catch (Exception e) {
       logger.warn(
           "Error during LastSpaceIndexingInfo reading from activity log ES index: {} {}",
           e.getClass().getName(),
           e.getMessage());
     }
   }
   return lastIndexing;
 }
 public final ImmutableList<ModelEntryT> getModels(
     final Logger logger,
     final Marker logMarker,
     final ModelFactory<ModelEntryT> modelFactory,
     final SearchRequestBuilder requestBuilder)
     throws IoExceptionT {
   checkNotNull(requestBuilder);
   try {
     logger.debug(logMarker, "searching index {} with {}", indexName, requestBuilder);
     final SearchResponse response = requestBuilder.execute().actionGet();
     final ImmutableList.Builder<ModelEntryT> modelsBuilder = ImmutableList.builder();
     for (final SearchHit searchHit : response.getHits()) {
       try {
         modelsBuilder.add(
             modelFactory.createModelEntry(searchHit.getId(), searchHit.getSourceRef()));
       } catch (final InvalidModelException e) {
         logger.error(logMarker, "invalid model {} from index, ignoring: ", searchHit.getId(), e);
         continue;
       }
     }
     return modelsBuilder.build();
   } catch (final IndexNotFoundException e) {
     throw e;
   } catch (final ElasticsearchException e) {
     logger.error(
         logMarker, "error searching models on index {} with {}: ", indexName, requestBuilder, e);
     throw exceptionFactory.newIoException(
         e,
         String.format("error searching models on index %s with '%s'", indexName, requestBuilder));
   }
 }
 private void test(
     String name, Class<? extends HashFunction> expectedHashFunction, boolean expectedUseType)
     throws Exception {
   Path zippedIndexDir = getDataPath("/org/elasticsearch/cluster/routing/" + name + ".zip");
   Settings baseSettings = prepareBackwardsDataDir(zippedIndexDir);
   internalCluster()
       .startNode(Settings.builder().put(baseSettings).put(Node.HTTP_ENABLED, true).build());
   ensureYellow("test");
   GetIndexResponse getIndexResponse = client().admin().indices().prepareGetIndex().get();
   assertArrayEquals(new String[] {"test"}, getIndexResponse.indices());
   GetSettingsResponse getSettingsResponse =
       client().admin().indices().prepareGetSettings("test").get();
   assertEquals(
       expectedHashFunction.getName(),
       getSettingsResponse.getSetting("test", IndexMetaData.SETTING_LEGACY_ROUTING_HASH_FUNCTION));
   assertEquals(
       Boolean.valueOf(expectedUseType).toString(),
       getSettingsResponse.getSetting("test", IndexMetaData.SETTING_LEGACY_ROUTING_USE_TYPE));
   SearchResponse allDocs = client().prepareSearch("test").get();
   assertSearchResponse(allDocs);
   assertHitCount(allDocs, 4);
   // Make sure routing works
   for (SearchHit hit : allDocs.getHits().hits()) {
     GetResponse get = client().prepareGet(hit.index(), hit.type(), hit.id()).get();
     assertTrue(get.isExists());
   }
 }
  @Override
  public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
    long totalHits = response.getHits().totalHits();
    List<T> results = new ArrayList<T>();
    for (SearchHit hit : response.getHits()) {
      if (hit != null) {
        T result = null;
        if (!Strings.isNullOrEmpty(hit.sourceAsString())) {
          result = mapEntity(hit.sourceAsString(), clazz);
        } else {
          result = mapEntity(hit.getFields().values(), clazz);
        }
        setPersistentEntityId(result, hit.getId(), clazz);
        results.add(result);
      }
    }
    List<FacetResult> facets = new ArrayList<FacetResult>();
    if (response.getFacets() != null) {
      for (Facet facet : response.getFacets()) {
        FacetResult facetResult = DefaultFacetMapper.parse(facet);
        if (facetResult != null) {
          facets.add(facetResult);
        }
      }
    }

    return new FacetedPageImpl<T>(results, pageable, totalHits, facets);
  }
Example #10
0
  @Test
  public void dateBetweenSearch()
      throws IOException, SqlParseException, SQLFeatureNotSupportedException {
    DateTimeFormatter formatter = DateTimeFormat.forPattern(DATE_FORMAT);

    DateTime dateLimit1 = new DateTime(2014, 8, 18, 0, 0, 0);
    DateTime dateLimit2 = new DateTime(2014, 8, 21, 0, 0, 0);

    SearchHits response =
        query(
            String.format(
                "SELECT insert_time FROM %s/online WHERE insert_time BETWEEN '2014-08-18' AND '2014-08-21' LIMIT 3",
                TEST_INDEX));
    SearchHit[] hits = response.getHits();
    for (SearchHit hit : hits) {
      Map<String, Object> source = hit.getSource();
      DateTime insertTime = formatter.parseDateTime((String) source.get("insert_time"));

      boolean isBetween =
          (insertTime.isAfter(dateLimit1) || insertTime.isEqual(dateLimit1))
              && (insertTime.isBefore(dateLimit2) || insertTime.isEqual(dateLimit2));

      Assert.assertTrue("insert_time must be between 2014-08-18 and 2014-08-21", isBetween);
    }
  }
Example #11
0
  @Test
  public void complexConditionQuery()
      throws IOException, SqlParseException, SQLFeatureNotSupportedException {
    String errorMessage =
        "Result does not exist to the condition (gender='m' AND (age> 25 OR account_number>5)) OR (gender='f' AND (age>30 OR account_number < 8)";

    SearchHits response =
        query(
            String.format(
                "SELECT * FROM %s/account WHERE (gender='m' AND (age> 25 OR account_number>5)) OR (gender='f' AND (age>30 OR account_number < 8))",
                TEST_INDEX));
    SearchHit[] hits = response.getHits();

    for (SearchHit hit : hits) {
      Map<String, Object> source = hit.getSource();
      String gender = ((String) source.get("gender")).toLowerCase();
      int age = (int) source.get("age");
      int account_number = (int) source.get("account_number");

      Assert.assertTrue(
          errorMessage,
          (gender.equals("m") && (age > 25 || account_number > 5))
              || (gender.equals("f") && (age > 30 || account_number < 8)));
    }
  }
 public void testCloseAndReopenOrDeleteWithActiveScroll() throws IOException {
   createIndex("test");
   for (int i = 0; i < 100; i++) {
     client()
         .prepareIndex("test", "type1", Integer.toString(i))
         .setSource(jsonBuilder().startObject().field("field", i).endObject())
         .execute()
         .actionGet();
   }
   refresh();
   SearchResponse searchResponse =
       client()
           .prepareSearch()
           .setQuery(matchAllQuery())
           .setSize(35)
           .setScroll(TimeValue.timeValueMinutes(2))
           .addSort("field", SortOrder.ASC)
           .execute()
           .actionGet();
   long counter = 0;
   assertThat(searchResponse.getHits().getTotalHits(), equalTo(100l));
   assertThat(searchResponse.getHits().hits().length, equalTo(35));
   for (SearchHit hit : searchResponse.getHits()) {
     assertThat(((Number) hit.sortValues()[0]).longValue(), equalTo(counter++));
   }
   if (randomBoolean()) {
     client().admin().indices().prepareClose("test").get();
     client().admin().indices().prepareOpen("test").get();
     ensureGreen("test");
   } else {
     client().admin().indices().prepareDelete("test").get();
   }
 }
Example #13
0
 @Override
 public Set<String> loadAllKeys() {
   logger.info("Load all keys called");
   SearchResponse response =
       elasticsearchConnection
           .getClient()
           .prepareSearch(TABLE_META_INDEX)
           .setTypes(TABLE_META_TYPE)
           .setQuery(QueryBuilders.matchAllQuery())
           .setSearchType(SearchType.SCAN)
           .setScroll(new TimeValue(30, TimeUnit.SECONDS))
           .setNoFields()
           .execute()
           .actionGet();
   Set<String> ids = Sets.newHashSet();
   while (true) {
     response =
         elasticsearchConnection
             .getClient()
             .prepareSearchScroll(response.getScrollId())
             .setScroll(new TimeValue(60000))
             .execute()
             .actionGet();
     SearchHits hits = response.getHits();
     for (SearchHit hit : hits) {
       ids.add(hit.getId());
     }
     if (0 == response.getHits().hits().length) {
       break;
     }
   }
   logger.info("Loaded value count: " + ids.size());
   return ids;
 }
  // Relates to #10397
  public void testUpdateScripts() {
    createIndex("test_index");
    ensureGreen("test_index");
    client().prepareIndex("test_index", "test_type", "1").setSource("{\"foo\":\"bar\"}").get();
    flush("test_index");

    int iterations = randomIntBetween(2, 11);
    for (int i = 1; i < iterations; i++) {
      assertAcked(
          client()
              .admin()
              .cluster()
              .preparePutStoredScript()
              .setScriptLang(GroovyScriptEngineService.NAME)
              .setId("script1")
              .setSource(new BytesArray("{\"script\":\"" + i + "\"}")));
      SearchResponse searchResponse =
          client()
              .prepareSearch()
              .setSource(
                  new SearchSourceBuilder()
                      .query(QueryBuilders.matchAllQuery())
                      .scriptField(
                          "test_field", new Script("script1", ScriptType.STORED, "groovy", null)))
              .setIndices("test_index")
              .setTypes("test_type")
              .get();
      assertHitCount(searchResponse, 1);
      SearchHit sh = searchResponse.getHits().getAt(0);
      assertThat(sh.field("test_field").getValue(), equalTo(i));
    }
  }
Example #15
0
  public void addDeletion(SearchRequestBuilder searchRequest) {
    searchRequest
        .addSort("_doc", SortOrder.ASC)
        .setScroll(TimeValue.timeValueMinutes(5))
        .setSize(100)
        // load only doc ids, not _source fields
        .setFetchSource(false);

    // this search is synchronous. An optimization would be to be non-blocking,
    // but it requires to tracking pending requests in close().
    // Same semaphore can't be reused because of potential deadlock (requires to acquire
    // two locks)
    SearchResponse searchResponse = searchRequest.get();

    while (true) {
      SearchHit[] hits = searchResponse.getHits().getHits();
      for (SearchHit hit : hits) {
        DeleteRequestBuilder deleteRequestBuilder =
            client.prepareDelete(hit.index(), hit.type(), hit.getId());
        SearchHitField routing = hit.field("_routing");
        if (routing != null) {
          deleteRequestBuilder.setRouting(routing.getValue());
        }
        add(deleteRequestBuilder.request());
      }

      String scrollId = searchResponse.getScrollId();
      searchResponse =
          client.prepareSearchScroll(scrollId).setScroll(TimeValue.timeValueMinutes(5)).get();
      if (hits.length == 0) {
        client.nativeClient().prepareClearScroll().addScrollId(scrollId).get();
        break;
      }
    }
  }
  @Override
  public List<Article> query(String queryString, int size) {
    Assert.hasText(queryString, "queryString must not be empty!");
    SearchResponse searchResponse =
        transportClient
            .prepareSearch(INDICE)
            .setTypes(TYPE)
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
            .setQuery(QueryBuilders.queryString(queryString))
            .setFrom(0)
            .setSize(size)
            .setExplain(true)
            .execute()
            .actionGet();

    SearchHits hits = searchResponse.getHits();
    long total = hits.getTotalHits();
    logger.debug("search articles result total:{}", total);
    List<Article> list = new ArrayList<Article>();
    Article article = null;
    for (SearchHit hit : hits) {

      Long id = (Long) hit.getSource().get("id");
      String title = (String) hit.getSource().get(TITLE);
      String summary = (String) hit.getSource().get(SUMMARY);
      logger.debug("id:{},title:{},summary:{}", id, title, summary);

      article = new Article();
      article.setId(id);
      article.setTitle(title);
      article.setSummary(summary);
      list.add(article);
    }
    return list;
  }
  public void testNativeScript() throws InterruptedException, ExecutionException {

    indexRandom(
        true,
        client().prepareIndex("test", "type1", "1").setSource("text", "doc1"),
        client().prepareIndex("test", "type1", "2").setSource("text", "doc2"),
        client().prepareIndex("test", "type1", "3").setSource("text", "doc3"),
        client().prepareIndex("test", "type1", "4").setSource("text", "doc4"),
        client().prepareIndex("test", "type1", "5").setSource("text", "doc5"),
        client().prepareIndex("test", "type1", "6").setSource("text", "doc6"));

    client().admin().indices().prepareFlush("test").execute().actionGet();
    SearchResponse sr =
        client()
            .prepareSearch("test")
            .setQuery(QueryBuilders.matchAllQuery())
            .addScriptField("int", new Script("int", ScriptType.INLINE, "native", null))
            .addScriptField("float", new Script("float", ScriptType.INLINE, "native", null))
            .addScriptField("double", new Script("double", ScriptType.INLINE, "native", null))
            .addScriptField("long", new Script("long", ScriptType.INLINE, "native", null))
            .execute()
            .actionGet();
    assertThat(sr.getHits().hits().length, equalTo(6));
    for (SearchHit hit : sr.getHits().getHits()) {
      Object result = hit.getFields().get("int").getValues().get(0);
      assertThat(result, equalTo((Object) intArray));
      result = hit.getFields().get("long").getValues().get(0);
      assertThat(result, equalTo((Object) longArray));
      result = hit.getFields().get("float").getValues().get(0);
      assertThat(result, equalTo((Object) floatArray));
      result = hit.getFields().get("double").getValues().get(0);
      assertThat(result, equalTo((Object) doubleArray));
    }
  }
  @Test
  public void testEscapeHtml() throws Exception {

    try {
      client.admin().indices().prepareDelete("test").execute().actionGet();
    } catch (Exception e) {
      // ignore
    }

    client
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(ImmutableSettings.settingsBuilder().put("number_of_shards", 2))
        .addMapping(
            "type1",
            jsonBuilder()
                .startObject()
                .startObject("type1")
                .startObject("properties")
                .startObject("title")
                .field("type", "string")
                .field("store", "yes")
                .endObject()
                .endObject()
                .endObject())
        .execute()
        .actionGet();

    for (int i = 0; i < 5; i++) {
      client
          .prepareIndex("test", "type1", Integer.toString(i))
          .setSource("title", "This is a html escaping highlighting test for *&? elasticsearch")
          .setRefresh(true)
          .execute()
          .actionGet();
    }

    SearchResponse search =
        client
            .prepareSearch()
            .setQuery(fieldQuery("title", "test"))
            .setEncoder("html")
            .addHighlightedField("title", 50, 1, 10)
            .execute()
            .actionGet();

    assertThat(Arrays.toString(search.shardFailures()), search.failedShards(), equalTo(0));

    assertThat(search.hits().totalHits(), equalTo(5l));
    assertThat(search.hits().hits().length, equalTo(5));

    for (SearchHit hit : search.hits()) {
      // LUCENE 3.1 UPGRADE: Caused adding the space at the end...
      assertThat(
          hit.highlightFields().get("title").fragments()[0],
          equalTo("This is a html escaping highlighting <em>test</em> for *&amp;? elasticsearch"));
    }
  }
Example #19
0
 public static <D extends BaseDoc> List<D> convertToDocs(
     SearchHits hits, Function<Map<String, Object>, D> converter) {
   List<D> docs = new ArrayList<>();
   for (SearchHit hit : hits.getHits()) {
     docs.add(converter.apply(hit.getSource()));
   }
   return docs;
 }
  @Test
  public void testSameContent() throws Exception {
    try {
      client.admin().indices().prepareDelete("test").execute().actionGet();
    } catch (Exception e) {
      // ignore
    }

    client
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(ImmutableSettings.settingsBuilder().put("number_of_shards", 2))
        .addMapping(
            "type1",
            jsonBuilder()
                .startObject()
                .startObject("type1")
                .startObject("properties")
                .startObject("title")
                .field("type", "string")
                .field("store", "yes")
                .field("term_vector", "with_positions_offsets")
                .endObject()
                .endObject()
                .endObject()
                .endObject())
        .execute()
        .actionGet();

    for (int i = 0; i < 5; i++) {
      client
          .prepareIndex("test", "type1", Integer.toString(i))
          .setSource("title", "This is a test on the highlighting bug present in elasticsearch")
          .setRefresh(true)
          .execute()
          .actionGet();
    }

    SearchResponse search =
        client
            .prepareSearch()
            .setQuery(fieldQuery("title", "bug"))
            .addHighlightedField("title", -1, 0)
            .execute()
            .actionGet();

    assertThat(search.hits().totalHits(), equalTo(5l));
    assertThat(search.hits().hits().length, equalTo(5));
    assertThat(search.getFailedShards(), equalTo(0));

    for (SearchHit hit : search.hits()) {
      // LUCENE 3.1 UPGRADE: Caused adding the space at the end...
      assertThat(
          hit.highlightFields().get("title").fragments()[0],
          equalTo("This is a test on the highlighting <em>bug</em> present in elasticsearch "));
    }
  }
 private List<String> extractIds(SearchResponse response) {
   List<String> ids = new ArrayList<String>();
   for (SearchHit hit : response.getHits()) {
     if (hit != null) {
       ids.add(hit.getId());
     }
   }
   return ids;
 }
  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;
  }
Example #23
0
 public static void removePendingRatings(String userId, ElasticSearchContextListener es) {
   // definitely not  more than 2, but to be safe, take 100...
   SearchResponse response =
       es.searchrequest(INDEX_NAME, QueryBuilders.matchQuery(ES_STATUS, ES_STATUS_PENDING), 0, 100)
           .actionGet();
   for (SearchHit hit : response.getHits()) {
     es.deleteRequest(INDEX_NAME, hit.getId()).actionGet();
   }
 }
Example #24
0
 @Test
 public void andTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
   SearchHits response =
       query(String.format("SELECT * FROM %s WHERE age=32 AND gender='M' LIMIT 1000", TEST_INDEX));
   SearchHit[] hits = response.getHits();
   for (SearchHit hit : hits) {
     Assert.assertEquals(32, hit.getSource().get("age"));
     Assert.assertEquals("M", hit.getSource().get("gender"));
   }
 }
  private <T> T createSavedObjectWithHit(SearchHit hit, Query<T> query) {
    T object = createSavedObject(hit.getType(), hit.getId(), query);
    State objectState = State.getInstance(object);

    if (!objectState.isReferenceOnly()) {
      objectState.setValues(hit.getSource());
    }

    return swapObjectType(query, object);
  }
Example #26
0
 public QProfileRuleResult searchInactives(ProfileRuleQuery query, Paging paging) {
   SearchHits hits =
       searchRules(
           query, paging, ruleFilterForInactiveRuleSearch(query), FIELD_SOURCE, FIELD_PARENT);
   List<QProfileRule> result = Lists.newArrayList();
   for (SearchHit hit : hits.getHits()) {
     result.add(new QProfileRule(hit.sourceAsMap()));
   }
   return new QProfileRuleResult(
       result, PagingResult.create(paging.pageSize(), paging.pageIndex(), hits.getTotalHits()));
 }
Example #27
0
 @Test
 public void lessThanTest()
     throws IOException, SqlParseException, SQLFeatureNotSupportedException {
   int someAge = 25;
   SearchHits response =
       query(String.format("SELECT * FROM %s WHERE age < %s LIMIT 1000", TEST_INDEX, someAge));
   SearchHit[] hits = response.getHits();
   for (SearchHit hit : hits) {
     int age = (int) hit.getSource().get("age");
     assertThat(age, lessThan(someAge));
   }
 }
Example #28
0
  @Test
  public void testChildrenAggs() throws Exception {
    SearchResponse searchResponse =
        client()
            .prepareSearch("test")
            .setQuery(matchQuery("randomized", true))
            .addAggregation(
                terms("category")
                    .field("category")
                    .size(0)
                    .subAggregation(
                        children("to_comment")
                            .childType("comment")
                            .subAggregation(
                                terms("commenters")
                                    .field("commenter")
                                    .size(0)
                                    .subAggregation(topHits("top_comments")))))
            .get();
    assertSearchResponse(searchResponse);

    Terms categoryTerms = searchResponse.getAggregations().get("category");
    assertThat(categoryTerms.getBuckets().size(), equalTo(categoryToControl.size()));
    for (Map.Entry<String, Control> entry1 : categoryToControl.entrySet()) {
      Terms.Bucket categoryBucket = categoryTerms.getBucketByKey(entry1.getKey());
      assertThat(categoryBucket.getKey(), equalTo(entry1.getKey()));
      assertThat(categoryBucket.getDocCount(), equalTo((long) entry1.getValue().articleIds.size()));

      Children childrenBucket = categoryBucket.getAggregations().get("to_comment");
      assertThat(childrenBucket.getName(), equalTo("to_comment"));
      assertThat(childrenBucket.getDocCount(), equalTo((long) entry1.getValue().commentIds.size()));
      assertThat(
          (long) childrenBucket.getProperty("_count"),
          equalTo((long) entry1.getValue().commentIds.size()));

      Terms commentersTerms = childrenBucket.getAggregations().get("commenters");
      assertThat((Terms) childrenBucket.getProperty("commenters"), sameInstance(commentersTerms));
      assertThat(
          commentersTerms.getBuckets().size(),
          equalTo(entry1.getValue().commenterToCommentId.size()));
      for (Map.Entry<String, Set<String>> entry2 :
          entry1.getValue().commenterToCommentId.entrySet()) {
        Terms.Bucket commentBucket = commentersTerms.getBucketByKey(entry2.getKey());
        assertThat(commentBucket.getKey(), equalTo(entry2.getKey()));
        assertThat(commentBucket.getDocCount(), equalTo((long) entry2.getValue().size()));

        TopHits topHits = commentBucket.getAggregations().get("top_comments");
        for (SearchHit searchHit : topHits.getHits().getHits()) {
          assertThat(entry2.getValue().contains(searchHit.getId()), is(true));
        }
      }
    }
  }
Example #29
0
 @Test
 public void inTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
   SearchHits response =
       query(
           String.format(
               "SELECT age FROM %s/phrase WHERE age IN (20, 22) LIMIT 1000", TEST_INDEX));
   SearchHit[] hits = response.getHits();
   for (SearchHit hit : hits) {
     int age = (int) hit.getSource().get("age");
     assertThat(age, isOneOf(20, 22));
   }
 }
Example #30
0
  @Test
  public void selectFieldWithSpace()
      throws IOException, SqlParseException, SQLFeatureNotSupportedException {
    String[] arr = new String[] {"test field"};
    Set expectedSource = new HashSet(Arrays.asList(arr));

    SearchHits response = query(String.format("SELECT `test field` FROM %s/phrase_2", TEST_INDEX));
    SearchHit[] hits = response.getHits();
    for (SearchHit hit : hits) {
      Assert.assertEquals(expectedSource, hit.getSource().keySet());
    }
  }