Esempio n. 1
0
  @Test
  public void orderByDescTest()
      throws IOException, SqlParseException, SQLFeatureNotSupportedException {
    SearchHits response =
        query(String.format("SELECT age FROM %s/account ORDER BY age DESC LIMIT 1000", TEST_INDEX));
    SearchHit[] hits = response.getHits();

    ArrayList<Integer> ages = new ArrayList<Integer>();
    for (SearchHit hit : hits) {
      ages.add((int) hit.getSource().get("age"));
    }

    ArrayList<Integer> sortedAges = (ArrayList<Integer>) ages.clone();
    Collections.sort(sortedAges, Collections.reverseOrder());
    Assert.assertTrue("The list is not ordered descending", sortedAges.equals(ages));
  }
Esempio n. 2
0
  @Test
  public void orderByAscFieldWithSpaceTest()
      throws IOException, SqlParseException, SQLFeatureNotSupportedException {
    SearchHits response =
        query(
            String.format(
                "SELECT * FROM %s/phrase_2 ORDER BY `test field` ASC LIMIT 1000", TEST_INDEX));
    SearchHit[] hits = response.getHits();

    ArrayList<Integer> testFields = new ArrayList<Integer>();
    for (SearchHit hit : hits) {
      testFields.add((int) hit.getSource().get("test field"));
    }

    ArrayList<Integer> sortedTestFields = (ArrayList<Integer>) testFields.clone();
    Collections.sort(sortedTestFields);
    Assert.assertTrue("The list is not ordered ascending", sortedTestFields.equals(testFields));
  }
  @Test
  public void orderByDescTest()
      throws IOException, SqlParseException, SQLFeatureNotSupportedException {
    ArrayList<Long> agesCount = new ArrayList<>();

    Aggregations result =
        query(
            String.format(
                "SELECT COUNT(*) FROM %s/account GROUP BY age ORDER BY COUNT(*) DESC", TEST_INDEX));
    Terms age = result.get("age");

    for (Terms.Bucket bucket : age.getBuckets()) {
      agesCount.add(((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue());
    }

    ArrayList<Long> sortedAgesCount = (ArrayList<Long>) agesCount.clone();
    Collections.sort(sortedAgesCount, Collections.reverseOrder());
    Assert.assertTrue("The list is not ordered descending", agesCount.equals(agesCount));
  }
Esempio n. 4
0
  public OrderWS buildOrder(int userId, List<Integer> itemIds, BigDecimal linePrice) {
    OrderWS order = new OrderWS();
    order.setUserId(userId);
    order.setBillingTypeId(Constants.ORDER_BILLING_POST_PAID);
    order.setPeriod(ORDER_PERIOD_ONCE); // once
    order.setCurrencyId(CURRENCY_USD);
    order.setActiveSince(new Date());
    order.setProrateFlag(Boolean.FALSE);

    ArrayList<OrderLineWS> lines = new ArrayList<OrderLineWS>(itemIds.size());
    for (int i = 0; i < itemIds.size(); i++) {
      OrderLineWS nextLine = new OrderLineWS();
      nextLine.setTypeId(Constants.ORDER_LINE_TYPE_ITEM);
      nextLine.setDescription("Order line: " + i);
      nextLine.setItemId(itemIds.get(i));
      nextLine.setQuantity(1);
      nextLine.setPrice(linePrice);
      nextLine.setAmount(nextLine.getQuantityAsDecimal().multiply(linePrice));

      lines.add(nextLine);
    }
    order.setOrderLines(lines.toArray(new OrderLineWS[lines.size()]));
    return order;
  }
  @Test
  public void updateMappingConcurrently() throws Throwable {
    createIndex("test1", "test2");

    // This is important. The test assumes all nodes are aware of all indices. Due to initializing
    // shard throttling
    // not all shards are allocated with the initial create index. Wait for it..
    ensureYellow();

    final Throwable[] threadException = new Throwable[1];
    final AtomicBoolean stop = new AtomicBoolean(false);
    Thread[] threads = new Thread[3];
    final CyclicBarrier barrier = new CyclicBarrier(threads.length);
    final ArrayList<Client> clientArray = new ArrayList<>();
    for (Client c : clients()) {
      clientArray.add(c);
    }

    for (int j = 0; j < threads.length; j++) {
      threads[j] =
          new Thread(
              new Runnable() {
                @SuppressWarnings("unchecked")
                @Override
                public void run() {
                  try {
                    barrier.await();

                    for (int i = 0; i < 100; i++) {
                      if (stop.get()) {
                        return;
                      }

                      Client client1 = clientArray.get(i % clientArray.size());
                      Client client2 = clientArray.get((i + 1) % clientArray.size());
                      String indexName = i % 2 == 0 ? "test2" : "test1";
                      String typeName = "type" + (i % 10);
                      String fieldName = Thread.currentThread().getName() + "_" + i;

                      PutMappingResponse response =
                          client1
                              .admin()
                              .indices()
                              .preparePutMapping(indexName)
                              .setType(typeName)
                              .setSource(
                                  JsonXContent.contentBuilder()
                                      .startObject()
                                      .startObject(typeName)
                                      .startObject("properties")
                                      .startObject(fieldName)
                                      .field("type", "string")
                                      .endObject()
                                      .endObject()
                                      .endObject()
                                      .endObject())
                              .get();

                      assertThat(response.isAcknowledged(), equalTo(true));
                      GetMappingsResponse getMappingResponse =
                          client2.admin().indices().prepareGetMappings(indexName).get();
                      ImmutableOpenMap<String, MappingMetaData> mappings =
                          getMappingResponse.getMappings().get(indexName);
                      assertThat(mappings.containsKey(typeName), equalTo(true));
                      assertThat(
                          ((Map<String, Object>)
                                  mappings.get(typeName).getSourceAsMap().get("properties"))
                              .keySet(),
                          Matchers.hasItem(fieldName));
                    }
                  } catch (Throwable t) {
                    threadException[0] = t;
                    stop.set(true);
                  }
                }
              });

      threads[j].setName("t_" + j);
      threads[j].start();
    }

    for (Thread t : threads) t.join();

    if (threadException[0] != null) {
      throw threadException[0];
    }
  }