Java Code Examples for org.apache.solr.common.params.ModifiableSolrParams#getMap()

The following examples show how to use org.apache.solr.common.params.ModifiableSolrParams#getMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestJsonFacets.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testComplex() throws Exception {
  Random r = random();

  Client client = Client.localClient;

  double price_low = 11000;
  double price_high = 100000;

  ModifiableSolrParams p = params("make_s","make_s", "model_s","model_s", "price_low",Double.toString(price_low), "price_high",Double.toString(price_high));


  MacroExpander m = new MacroExpander( p.getMap() );

  String make_s = m.expand("${make_s}");
  String model_s = m.expand("${model_s}");

  client.deleteByQuery("*:*", null);


  int nDocs = 99;
  String[] makes = {"honda", "toyota", "ford", null};
  Double[] prices = {10000.0, 30000.0, 50000.0, 0.0, null};
  String[] honda_models = {"accord", "civic", "fit", "pilot", null};  // make sure this is alphabetized to match tiebreaks in index
  String[] other_models = {"z1", "z2", "z3", "z4", "z5", "z6", null};

  int nHonda = 0;
  final int[] honda_model_counts = new int[honda_models.length];

  for (int i=0; i<nDocs; i++) {
    SolrInputDocument doc = sdoc("id", Integer.toString(i));

    Double price = rand(prices);
    if (price != null) {
      doc.addField("cost_f", price);
    }
    boolean matches_price = price!=null && price >= price_low && price <= price_high;

    String make = rand(makes);
    if (make != null) {
      doc.addField(make_s, make);
    }

    if ("honda".equals(make)) {
      int modelNum = r.nextInt(honda_models.length);
      String model = honda_models[modelNum];
      if (model != null) {
        doc.addField(model_s, model);
      }
      if (matches_price) {
        nHonda++;
        honda_model_counts[modelNum]++;
      }
    } else if (make == null) {
      doc.addField(model_s, rand(honda_models));  // add some docs w/ model but w/o make
    } else {
      // other makes
      doc.addField(model_s, rand(other_models));  // add some docs w/ model but w/o make
    }

    client.add(doc, null);
    if (r.nextInt(10) == 0) {
      client.add(doc, null);  // dup, causing a delete
    }
    if (r.nextInt(20) == 0) {
      client.commit();  // force new seg
    }
  }

  client.commit();

  // now figure out top counts
  List<Integer> idx = new ArrayList<>();
  for (int i=0; i<honda_model_counts.length-1; i++) {
    idx.add(i);
  }
  Collections.sort(idx, (o1, o2) -> {
    int cmp = honda_model_counts[o2] - honda_model_counts[o1];
    return cmp == 0 ? o1 - o2 : cmp;
  });



  // straight query facets
  client.testJQ(params(p, "q", "*:*", "rows","0", "fq","+${make_s}:honda +cost_f:[${price_low} TO ${price_high}]"
          , "json.facet", "{makes:{terms:{field:${make_s}, facet:{models:{terms:{field:${model_s}, limit:2, mincount:0}}}}"
              + "}}"
          , "facet","true", "facet.pivot","make_s,model_s", "facet.limit", "2"
      )
      , "facets=={count:" + nHonda + ", makes:{buckets:[{val:honda, count:" + nHonda + ", models:{buckets:["
         + "{val:" + honda_models[idx.get(0)] + ", count:" + honda_model_counts[idx.get(0)] + "},"
         + "{val:" + honda_models[idx.get(1)] + ", count:" + honda_model_counts[idx.get(1)] + "}]}"
         + "}]}}"
  );


}
 
Example 2
Source File: TestJsonFacets.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void doBigger(Client client, ModifiableSolrParams p) throws Exception {
  MacroExpander m = new MacroExpander(p.getMap());

  String cat_s = m.expand("${cat_s}");
  String where_s = m.expand("${where_s}");

  client.deleteByQuery("*:*", null);

  Random r = new Random(0);  // make deterministic
  int numCat = 1;
  int numWhere = 2000000000;
  int commitPercent = 10;
  int ndocs=1000;

  Map<Integer, Map<Integer, List<Integer>>> model = new HashMap<>();  // cat->where->list<ids>
  for (int i=0; i<ndocs; i++) {
    Integer cat = r.nextInt(numCat);
    Integer where = r.nextInt(numWhere);
    client.add( sdoc("id", getId(i), cat_s,cat, where_s, where) , null );
    Map<Integer,List<Integer>> sub = model.get(cat);
    if (sub == null) {
      sub = new HashMap<>();
      model.put(cat, sub);
    }
    List<Integer> ids = sub.get(where);
    if (ids == null) {
      ids = new ArrayList<>();
      sub.put(where, ids);
    }
    ids.add(i);

    if (r.nextInt(100) < commitPercent) {
      client.commit();
    }
  }

  client.commit();

  int sz = model.get(0).size();

  client.testJQ(params(p, "q", "*:*"
          , "json.facet", "{f1:{type:terms, field:${cat_s}, limit:2, facet:{x:'unique($where_s)'}  }}"
      )
      , "facets=={ 'count':" + ndocs + "," +
          "'f1':{  'buckets':[{ 'val':'0', 'count':" + ndocs + ", x:" + sz + " }]} } "
  );

  if (client.local()) {
    // distrib estimation prob won't match
    client.testJQ(params(p, "q", "*:*"
            , "json.facet", "{f1:{type:terms, field:${cat_s}, limit:2, facet:{x:'hll($where_s)'}  }}"
        )
        , "facets=={ 'count':" + ndocs + "," +
            "'f1':{  'buckets':[{ 'val':'0', 'count':" + ndocs + ", x:" + sz + " }]} } "
    );
  }

  client.testJQ(params(p, "q", "*:*"
      , "json.facet", "{f1:{type:terms, field:id, limit:1, offset:990}}"
      )
      , "facets=={ 'count':" + ndocs + "," +
          "'f1':{buckets:[{val:'00990',count:1}]}} "
  );


  for (int i=0; i<20; i++) {
    int off = random().nextInt(ndocs);
    client.testJQ(params(p, "q", "*:*", "off",Integer.toString(off)
        , "json.facet", "{f1:{type:terms, field:id, limit:1, offset:${off}}}"
        )
        , "facets=={ 'count':" + ndocs + "," +
            "'f1':{buckets:[{val:'"  + getId(off)  + "',count:1}]}} "
    );
  }
}