Search in sources :

Example 21 with NamedList

use of org.apache.solr.common.util.NamedList in project lucene-solr by apache.

the class TestInPlaceUpdatesDistrib method addDocAndGetVersion.

@SuppressWarnings("rawtypes")
protected long addDocAndGetVersion(Object... fields) throws Exception {
    SolrInputDocument doc = new SolrInputDocument();
    addFields(doc, fields);
    UpdateRequest ureq = new UpdateRequest();
    ureq.setParam("versions", "true");
    ureq.add(doc);
    UpdateResponse resp;
    // send updates to leader, to avoid SOLR-8733
    resp = ureq.process(LEADER);
    long returnedVersion = Long.parseLong(((NamedList) resp.getResponse().get("adds")).getVal(0).toString());
    assertTrue("Due to SOLR-8733, sometimes returned version is 0. Let us assert that we have successfully" + " worked around that problem here.", returnedVersion > 0);
    return returnedVersion;
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) NamedList(org.apache.solr.common.util.NamedList)

Example 22 with NamedList

use of org.apache.solr.common.util.NamedList in project lucene-solr by apache.

the class FacetStream method open.

public void open() throws IOException {
    if (cache != null) {
        cloudSolrClient = cache.getCloudSolrClient(zkHost);
    } else {
        cloudSolrClient = new Builder().withZkHost(zkHost).build();
    }
    FieldComparator[] adjustedSorts = adjustSorts(buckets, bucketSorts);
    String json = getJsonFacetString(buckets, metrics, adjustedSorts, bucketSizeLimit);
    ModifiableSolrParams paramsLoc = new ModifiableSolrParams(params);
    paramsLoc.set("json.facet", json);
    paramsLoc.set("rows", "0");
    QueryRequest request = new QueryRequest(paramsLoc);
    try {
        NamedList response = cloudSolrClient.request(request, collection);
        getTuples(response, buckets, metrics);
        Collections.sort(tuples, getStreamSort());
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) NamedList(org.apache.solr.common.util.NamedList) Builder(org.apache.solr.client.solrj.impl.CloudSolrClient.Builder) FieldComparator(org.apache.solr.client.solrj.io.comp.FieldComparator) MultipleFieldComparator(org.apache.solr.client.solrj.io.comp.MultipleFieldComparator) IOException(java.io.IOException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) IOException(java.io.IOException)

Example 23 with NamedList

use of org.apache.solr.common.util.NamedList in project lucene-solr by apache.

the class FacetStream method getTuples.

private void getTuples(NamedList response, Bucket[] buckets, Metric[] metrics) {
    Tuple tuple = new Tuple(new HashMap());
    NamedList facets = (NamedList) response.get("facets");
    fillTuples(0, tuples, tuple, facets, buckets, metrics);
}
Also used : HashMap(java.util.HashMap) NamedList(org.apache.solr.common.util.NamedList) Tuple(org.apache.solr.client.solrj.io.Tuple)

Example 24 with NamedList

use of org.apache.solr.common.util.NamedList in project lucene-solr by apache.

the class FeaturesSelectionStream method read.

public Tuple read() throws IOException {
    try {
        if (tupleIterator == null) {
            Map<String, Double> termScores = new HashMap<>();
            Map<String, Long> docFreqs = new HashMap<>();
            long numDocs = 0;
            for (Future<NamedList> getTopTermsCall : callShards(getShardUrls())) {
                NamedList resp = getTopTermsCall.get();
                NamedList<Double> shardTopTerms = (NamedList<Double>) resp.get("featuredTerms");
                NamedList<Integer> shardDocFreqs = (NamedList<Integer>) resp.get("docFreq");
                numDocs += (Integer) resp.get("numDocs");
                for (int i = 0; i < shardTopTerms.size(); i++) {
                    String term = shardTopTerms.getName(i);
                    double score = shardTopTerms.getVal(i);
                    int docFreq = shardDocFreqs.get(term);
                    double prevScore = termScores.containsKey(term) ? termScores.get(term) : 0;
                    long prevDocFreq = docFreqs.containsKey(term) ? docFreqs.get(term) : 0;
                    termScores.put(term, prevScore + score);
                    docFreqs.put(term, prevDocFreq + docFreq);
                }
            }
            List<Tuple> tuples = new ArrayList<>(numTerms);
            termScores = sortByValue(termScores);
            int index = 0;
            for (Map.Entry<String, Double> termScore : termScores.entrySet()) {
                if (tuples.size() == numTerms)
                    break;
                index++;
                Map map = new HashMap();
                map.put(ID, featureSet + "_" + index);
                map.put("index_i", index);
                map.put("term_s", termScore.getKey());
                map.put("score_f", termScore.getValue());
                map.put("featureSet_s", featureSet);
                long docFreq = docFreqs.get(termScore.getKey());
                double d = Math.log(((double) numDocs / (double) (docFreq + 1)));
                map.put("idf_d", d);
                tuples.add(new Tuple(map));
            }
            Map map = new HashMap();
            map.put("EOF", true);
            tuples.add(new Tuple(map));
            tupleIterator = tuples.iterator();
        }
        return tupleIterator.next();
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Tuple(org.apache.solr.client.solrj.io.Tuple)

Example 25 with NamedList

use of org.apache.solr.common.util.NamedList in project lucene-solr by apache.

the class SolrExampleTests method doPivotFacetTest.

private void doPivotFacetTest(boolean missing) throws Exception {
    SolrClient client = getSolrClient();
    // Empty the database...
    // delete everything!
    client.deleteByQuery("*:*");
    client.commit();
    // make sure it got in
    assertNumFound("*:*", 0);
    int id = 1;
    ArrayList<SolrInputDocument> docs = new ArrayList<>();
    docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", true));
    docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", false));
    docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", true));
    docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "b", "inStock", false));
    docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "b", "inStock", true));
    docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "a", "inStock", false));
    docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "a", "inStock", true));
    docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", false));
    docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", true));
    docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", false));
    docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", true));
    // something not matching all fields
    docs.add(makeTestDoc("id", id++, "cat", "b"));
    client.add(docs);
    client.commit();
    SolrQuery query = new SolrQuery("*:*");
    query.addFacetPivotField("features,cat", "cat,features", "features,cat,inStock");
    query.setFacetMinCount(0);
    query.setFacetMissing(missing);
    query.setRows(0);
    QueryResponse rsp = client.query(query);
    assertEquals(docs.size(), rsp.getResults().getNumFound());
    NamedList<List<PivotField>> pivots = rsp.getFacetPivot();
    assertEquals(3, pivots.size());
    //    for(Map.Entry<String, List<PivotField>> entry : pivots ) {
    //      System.out.println( "PIVOT: "+entry.getKey() );
    //      for( PivotField p : entry.getValue() ) {
    //        p.write(System.out, 0 );
    //      }
    //      System.out.println();
    //    }
    //  PIVOT: features,cat
    //  features=bbb (6)
    //    cat=b (4)
    //    cat=a (2)
    //  features=aaa (5)
    //    cat=a (3)
    //    cat=b (2)
    //  features missing (1)
    //    cat=b (1)
    assertEquals("features,cat", pivots.getName(0));
    List<PivotField> pivot = pivots.getVal(0);
    assertEquals(missing ? 3 : 2, pivot.size());
    PivotField ff = pivot.get(0);
    assertEquals("features", ff.getField());
    assertEquals("bbb", ff.getValue());
    assertEquals(6, ff.getCount());
    List<PivotField> counts = ff.getPivot();
    assertEquals(2, counts.size());
    assertEquals("cat", counts.get(0).getField());
    assertEquals("b", counts.get(0).getValue());
    assertEquals(4, counts.get(0).getCount());
    assertEquals("a", counts.get(1).getValue());
    assertEquals(2, counts.get(1).getCount());
    ff = pivot.get(1);
    assertEquals("features", ff.getField());
    assertEquals("aaa", ff.getValue());
    assertEquals(5, ff.getCount());
    counts = ff.getPivot();
    assertEquals(2, counts.size());
    assertEquals("cat", counts.get(0).getField());
    assertEquals("a", counts.get(0).getValue());
    assertEquals(3, counts.get(0).getCount());
    assertEquals("b", counts.get(1).getValue());
    assertEquals(2, counts.get(1).getCount());
    if (missing) {
        ff = pivot.get(2);
        assertEquals("features", ff.getField());
        assertEquals(null, ff.getValue());
        assertEquals(1, ff.getCount());
        counts = ff.getPivot();
        assertEquals(1, counts.size());
        assertEquals("cat", counts.get(0).getField());
        assertEquals("b", counts.get(0).getValue());
        assertEquals(1, counts.get(0).getCount());
    }
    //  PIVOT: cat,features
    //  cat=b (7)
    //    features=bbb (4)
    //    features=aaa (2)
    //    features missing (1)
    //  cat=a (5)
    //    features=aaa (3)
    //    features=bbb (2)
    assertEquals("cat,features", pivots.getName(1));
    pivot = pivots.getVal(1);
    assertEquals(2, pivot.size());
    ff = pivot.get(0);
    assertEquals("cat", ff.getField());
    assertEquals("b", ff.getValue());
    assertEquals(7, ff.getCount());
    counts = ff.getPivot();
    assertEquals(missing ? 3 : 2, counts.size());
    assertEquals("features", counts.get(0).getField());
    assertEquals("bbb", counts.get(0).getValue());
    assertEquals(4, counts.get(0).getCount());
    assertEquals("aaa", counts.get(1).getValue());
    assertEquals(2, counts.get(1).getCount());
    if (missing) {
        assertEquals(null, counts.get(2).getValue());
        assertEquals(1, counts.get(2).getCount());
    }
    ff = pivot.get(1);
    assertEquals("cat", ff.getField());
    assertEquals("a", ff.getValue());
    assertEquals(5, ff.getCount());
    counts = ff.getPivot();
    assertEquals(2, counts.size());
    assertEquals("features", counts.get(0).getField());
    assertEquals("aaa", counts.get(0).getValue());
    assertEquals(3, counts.get(0).getCount());
    assertEquals("bbb", counts.get(1).getValue());
    assertEquals(2, counts.get(1).getCount());
    // Three deep:
    //  PIVOT: features,cat,inStock
    //  features=bbb (6)
    //    cat=b (4)
    //      inStock=false (2)
    //      inStock=true (2)
    //    cat=a (2)
    //      inStock=false (1)
    //      inStock=true (1)
    //  features=aaa (5)
    //    cat=a (3)
    //      inStock=true (2)
    //      inStock=false (1)
    //    cat=b (2)
    //      inStock=false (1)
    //      inStock=true (1)
    //  features missing (1)
    //    cat=b (1)
    //      inStock missing (1)
    assertEquals("features,cat,inStock", pivots.getName(2));
    pivot = pivots.getVal(2);
    assertEquals(missing ? 3 : 2, pivot.size());
    // get(1) should be features=AAAA, then get(0) should be cat=a
    PivotField p = pivot.get(1).getPivot().get(0);
    assertEquals("cat", p.getField());
    assertEquals("a", p.getValue());
    counts = p.getPivot();
    //  p.write(System.out, 5 );
    // 2 trues and 1 false under features=AAAA,cat=a
    assertEquals(2, counts.size());
    assertEquals("inStock", counts.get(0).getField());
    assertEquals(Boolean.TRUE, counts.get(0).getValue());
    assertEquals(2, counts.get(0).getCount());
    if (missing) {
        p = pivot.get(2);
        assertEquals("features", p.getField());
        assertEquals(null, p.getValue());
        assertEquals(1, p.getCount());
        assertEquals(1, p.getPivot().size());
        p = p.getPivot().get(0);
        assertEquals("cat", p.getField());
        assertEquals("b", p.getValue());
        assertEquals(1, p.getCount());
        assertEquals(1, p.getPivot().size());
        p = p.getPivot().get(0);
        assertEquals("inStock", p.getField());
        assertEquals(null, p.getValue());
        assertEquals(1, p.getCount());
        assertEquals(null, p.getPivot());
    }
    // -- SOLR-2255 Test excluding a filter Query --
    // this test is a slight modification to the first pivot facet test
    query = new SolrQuery("*:*");
    query.addFacetPivotField("{!ex=mytag key=mykey}features,cat");
    //filters out one
    query.addFilterQuery("{!tag=mytag}-(features:bbb AND cat:a AND inStock:true)");
    query.setFacetMinCount(0);
    query.setRows(0);
    rsp = client.query(query);
    //one less due to filter
    assertEquals(docs.size() - 1, rsp.getResults().getNumFound());
    //The rest of this test should be just like the original since we've
    // excluded the 'fq' from the facet count
    pivots = rsp.getFacetPivot();
    pivot = pivots.getVal(0);
    assertEquals("mykey", pivots.getName(0));
    assertEquals(2, pivot.size());
    ff = pivot.get(0);
    assertEquals("features", ff.getField());
    assertEquals("bbb", ff.getValue());
    assertEquals(6, ff.getCount());
    counts = ff.getPivot();
    assertEquals(2, counts.size());
    assertEquals("cat", counts.get(0).getField());
    assertEquals("b", counts.get(0).getValue());
    assertEquals(4, counts.get(0).getCount());
    assertEquals("a", counts.get(1).getValue());
    assertEquals(2, counts.get(1).getCount());
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) ErrorTrackingConcurrentUpdateSolrClient(org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ArrayList(java.util.ArrayList) PivotField(org.apache.solr.client.solrj.response.PivotField) SolrDocumentList(org.apache.solr.common.SolrDocumentList) List(java.util.List) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList)

Aggregations

NamedList (org.apache.solr.common.util.NamedList)438 Test (org.junit.Test)125 ArrayList (java.util.ArrayList)110 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)83 Map (java.util.Map)82 SolrException (org.apache.solr.common.SolrException)80 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)78 List (java.util.List)75 HashMap (java.util.HashMap)64 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)55 IOException (java.io.IOException)53 SolrDocumentList (org.apache.solr.common.SolrDocumentList)45 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)35 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)35 SolrParams (org.apache.solr.common.params.SolrParams)31 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)31 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)30 SolrCore (org.apache.solr.core.SolrCore)30 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)27 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)27