Search in sources :

Example 1 with FacetField

use of org.apache.solr.client.solrj.response.FacetField in project jackrabbit-oak by apache.

the class SolrQueryIndex method getIterator.

private AbstractIterator<SolrResultRow> getIterator(final Filter filter, final IndexPlan plan, final String parent, final int parentDepth, final OakSolrConfiguration configuration, final SolrServer solrServer, final LMSEstimator estimator) {
    return new AbstractIterator<SolrResultRow>() {

        public Collection<FacetField> facetFields = new LinkedList<FacetField>();

        private final Set<String> seenPaths = Sets.newHashSet();

        private final Deque<SolrResultRow> queue = Queues.newArrayDeque();

        private int offset = 0;

        private boolean noDocs = false;

        private long numFound = 0;

        @Override
        protected SolrResultRow computeNext() {
            if (!queue.isEmpty() || loadDocs()) {
                return queue.remove();
            }
            return endOfData();
        }

        private SolrResultRow convertToRow(SolrDocument doc) {
            String path = String.valueOf(doc.getFieldValue(configuration.getPathField()));
            if ("".equals(path)) {
                path = "/";
            }
            if (!parent.isEmpty()) {
                path = getAncestorPath(path, parentDepth);
                // avoid duplicate entries
                if (seenPaths.contains(path)) {
                    return null;
                }
                seenPaths.add(path);
            }
            float score = 0f;
            Object scoreObj = doc.get("score");
            if (scoreObj != null) {
                score = (Float) scoreObj;
            }
            return new SolrResultRow(path, score, doc, facetFields);
        }

        /**
             * Loads the Solr documents in batches
             * @return true if any document is loaded
             */
        private boolean loadDocs() {
            if (noDocs) {
                return false;
            }
            try {
                if (log.isDebugEnabled()) {
                    log.debug("converting filter {}", filter);
                }
                SolrQuery query = FilterQueryParser.getQuery(filter, plan, configuration);
                if (numFound > 0) {
                    long rows = configuration.getRows();
                    long maxQueries = numFound / 2;
                    if (maxQueries > configuration.getRows()) {
                        // adjust the rows to avoid making more than 3 Solr requests for this particular query
                        rows = maxQueries;
                        query.setParam("rows", String.valueOf(rows));
                    }
                    long newOffset = configuration.getRows() + offset * rows;
                    if (newOffset >= numFound) {
                        return false;
                    }
                    query.setParam("start", String.valueOf(newOffset));
                    offset++;
                }
                if (log.isDebugEnabled()) {
                    log.debug("sending query {}", query);
                }
                QueryResponse queryResponse = solrServer.query(query);
                if (log.isDebugEnabled()) {
                    log.debug("getting response {}", queryResponse.getHeader());
                }
                SolrDocumentList docs = queryResponse.getResults();
                if (docs != null) {
                    numFound = docs.getNumFound();
                    estimator.update(filter, docs);
                    Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
                    for (SolrDocument doc : docs) {
                        // handle highlight
                        if (highlighting != null) {
                            Object pathObject = doc.getFieldValue(configuration.getPathField());
                            if (pathObject != null && highlighting.get(String.valueOf(pathObject)) != null) {
                                Map<String, List<String>> value = highlighting.get(String.valueOf(pathObject));
                                for (Map.Entry<String, List<String>> entry : value.entrySet()) {
                                    // all highlighted values end up in 'rep:excerpt', regardless of field match
                                    for (String v : entry.getValue()) {
                                        doc.addField(QueryImpl.REP_EXCERPT, v);
                                    }
                                }
                            }
                        }
                        SolrResultRow row = convertToRow(doc);
                        if (row != null) {
                            queue.add(row);
                        }
                    }
                }
                // get facets
                List<FacetField> returnedFieldFacet = queryResponse.getFacetFields();
                if (returnedFieldFacet != null) {
                    facetFields.addAll(returnedFieldFacet);
                }
                // filter facets on doc paths
                if (!facetFields.isEmpty() && docs != null) {
                    for (SolrDocument doc : docs) {
                        String path = String.valueOf(doc.getFieldValue(configuration.getPathField()));
                        // if facet path doesn't exist for the calling user, filter the facet for this doc
                        for (FacetField ff : facetFields) {
                            if (!filter.isAccessible(path + "/" + ff.getName())) {
                                filterFacet(doc, ff);
                            }
                        }
                    }
                }
                // handle spellcheck
                SpellCheckResponse spellCheckResponse = queryResponse.getSpellCheckResponse();
                if (spellCheckResponse != null && spellCheckResponse.getSuggestions() != null && spellCheckResponse.getSuggestions().size() > 0) {
                    putSpellChecks(spellCheckResponse, queue, filter, configuration, solrServer);
                    noDocs = true;
                }
                // handle suggest
                NamedList<Object> response = queryResponse.getResponse();
                Map suggest = (Map) response.get("suggest");
                if (suggest != null) {
                    Set<Map.Entry<String, Object>> suggestEntries = suggest.entrySet();
                    if (!suggestEntries.isEmpty()) {
                        putSuggestions(suggestEntries, queue, filter, configuration, solrServer);
                        noDocs = true;
                    }
                }
            } catch (Exception e) {
                if (log.isWarnEnabled()) {
                    log.warn("query via {} failed.", solrServer, e);
                }
            }
            return !queue.isEmpty();
        }
    };
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) FacetField(org.apache.solr.client.solrj.response.FacetField) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SpellCheckResponse(org.apache.solr.client.solrj.response.SpellCheckResponse) SolrDocument(org.apache.solr.common.SolrDocument) SolrDocumentList(org.apache.solr.common.SolrDocumentList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) NamedList(org.apache.solr.common.util.NamedList) AbstractIterator(com.google.common.collect.AbstractIterator) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Deque(java.util.Deque) SolrServerException(org.apache.solr.client.solrj.SolrServerException) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Collection(java.util.Collection) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap)

Example 2 with FacetField

use of org.apache.solr.client.solrj.response.FacetField in project lucene-solr by apache.

the class BlockJoinFacetDistribTest method testBJQFacetComponent.

@Test
public void testBJQFacetComponent() throws Exception {
    assert !colors.removeAll(sizes) : "there is no colors in sizes";
    Collections.shuffle(colors, random());
    List<String> matchingColors = colors.subList(0, Math.min(atLeast(random(), 2), colors.size()));
    Map<String, Set<Integer>> parentIdsByAttrValue = new HashMap<String, Set<Integer>>() {

        @Override
        public Set<Integer> get(Object key) {
            return super.get(key) == null && put((String) key, new HashSet<>()) == null ? super.get(key) : super.get(key);
        }
    };
    cluster.getSolrClient().deleteByQuery(collection, "*:*");
    final int parents = atLeast(10);
    boolean aggregationOccurs = false;
    List<SolrInputDocument> parentDocs = new ArrayList<>();
    for (int parent = 0; parent < parents || !aggregationOccurs; parent++) {
        assert parent < 2000000 : "parent num " + parent + " aggregationOccurs:" + aggregationOccurs + ". Sorry! too tricky loop condition.";
        SolrInputDocument pdoc = new SolrInputDocument();
        pdoc.addField("id", parent);
        pdoc.addField("type_s", "parent");
        final String parentBrand = "brand" + (random().nextInt(5));
        pdoc.addField("BRAND_s", parentBrand);
        for (int child = 0; child < atLeast(colors.size() / 2); child++) {
            SolrInputDocument childDoc = new SolrInputDocument();
            final String color = colors.get(random().nextInt(colors.size()));
            childDoc.addField("COLOR_s", color);
            final String size = sizes.get(random().nextInt(sizes.size()));
            childDoc.addField("SIZE_s", size);
            if (matchingColors.contains(color)) {
                final boolean colorDupe = !parentIdsByAttrValue.get(color).add(parent);
                final boolean sizeDupe = !parentIdsByAttrValue.get(size).add(parent);
                aggregationOccurs |= colorDupe || sizeDupe;
            }
            pdoc.addChildDocument(childDoc);
        }
        parentDocs.add(pdoc);
        if (!parentDocs.isEmpty() && rarely()) {
            indexDocs(parentDocs);
            parentDocs.clear();
            cluster.getSolrClient().commit(collection, false, false, true);
        }
    }
    if (!parentDocs.isEmpty()) {
        indexDocs(parentDocs);
    }
    cluster.getSolrClient().commit(collection);
    // to parent query
    final String childQueryClause = "COLOR_s:(" + (matchingColors.toString().replaceAll("[,\\[\\]]", " ")) + ")";
    final boolean oldFacetsEnabled = random().nextBoolean();
    QueryResponse results = query("q", "{!parent which=\"type_s:parent\"}" + childQueryClause, // try to enforce multiple phases
    "facet", // try to enforce multiple phases
    oldFacetsEnabled ? "true" : "false", oldFacetsEnabled ? "facet.field" : "ignore", "BRAND_s", oldFacetsEnabled && usually() ? "facet.limit" : "ignore", "1", oldFacetsEnabled && usually() ? "facet.mincount" : "ignore", "2", oldFacetsEnabled && usually() ? "facet.overrequest.count" : "ignore", "0", "qt", random().nextBoolean() ? "blockJoinDocSetFacetRH" : "blockJoinFacetRH", "child.facet.field", "COLOR_s", "child.facet.field", "SIZE_s", "distrib.singlePass", random().nextBoolean() ? "true" : "false", "rows", random().nextBoolean() ? "0" : "10");
    NamedList<Object> resultsResponse = results.getResponse();
    assertNotNull(resultsResponse);
    FacetField color_s = results.getFacetField("COLOR_s");
    FacetField size_s = results.getFacetField("SIZE_s");
    String msg = "" + parentIdsByAttrValue + " " + color_s + " " + size_s;
    for (FacetField facet : new FacetField[] { color_s, size_s }) {
        for (Count c : facet.getValues()) {
            assertEquals(c.getName() + "(" + msg + ")", parentIdsByAttrValue.get(c.getName()).size(), c.getCount());
        }
    }
    assertEquals(msg, parentIdsByAttrValue.size(), color_s.getValueCount() + size_s.getValueCount());
//System.out.println(parentIdsByAttrValue);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FacetField(org.apache.solr.client.solrj.response.FacetField) Count(org.apache.solr.client.solrj.response.FacetField.Count) SolrInputDocument(org.apache.solr.common.SolrInputDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with FacetField

use of org.apache.solr.client.solrj.response.FacetField in project lucene-solr by apache.

the class DistributedFacetExistsSmallTest method assertResponse.

private void assertResponse(QueryResponse rsp) {
    final FacetField facetField = rsp.getFacetField(FLD);
    assertThat(facetField.getValueCount(), is(6));
    final List<FacetField.Count> counts = facetField.getValues();
    for (FacetField.Count count : counts) {
        assertThat("Count for: " + count.getName(), count.getCount(), is(1L));
    }
    assertThat(counts.get(0).getName(), is("AAA"));
    assertThat(counts.get(1).getName(), is("B"));
    assertThat(counts.get(2).getName(), is("BB"));
}
Also used : FacetField(org.apache.solr.client.solrj.response.FacetField)

Example 4 with FacetField

use of org.apache.solr.client.solrj.response.FacetField in project lucene-solr by apache.

the class SolrExampleTests method testFaceting.

@Test
public void testFaceting() throws Exception {
    SolrClient client = getSolrClient();
    // Empty the database...
    // delete everything!
    client.deleteByQuery("*:*");
    client.commit();
    // make sure it got in
    assertNumFound("*:*", 0);
    ArrayList<SolrInputDocument> docs = new ArrayList<>(10);
    for (int i = 1; i <= 10; i++) {
        SolrInputDocument doc = new SolrInputDocument();
        doc.setField("id", i + "");
        if ((i % 2) == 0) {
            doc.addField("features", "two");
        }
        if ((i % 3) == 0) {
            doc.addField("features", "three");
        }
        if ((i % 4) == 0) {
            doc.addField("features", "four");
        }
        if ((i % 5) == 0) {
            doc.addField("features", "five");
        }
        docs.add(doc);
    }
    client.add(docs);
    client.commit();
    SolrQuery query = new SolrQuery("*:*");
    query.remove(FacetParams.FACET_FIELD);
    query.addFacetField("features");
    query.setFacetMinCount(0);
    query.setFacet(true);
    query.setRows(0);
    QueryResponse rsp = client.query(query);
    assertEquals(docs.size(), rsp.getResults().getNumFound());
    List<FacetField> facets = rsp.getFacetFields();
    assertEquals(1, facets.size());
    FacetField ff = facets.get(0);
    assertEquals("features", ff.getName());
    // System.out.println( "111: "+ff.getValues() );
    // check all counts
    assertEquals("[two (5), three (3), five (2), four (2)]", ff.getValues().toString());
    // should be the same facets with minCount=0
    query.setFilterQueries("features:two");
    rsp = client.query(query);
    ff = rsp.getFacetField("features");
    assertEquals("[two (5), four (2), five (1), three (1)]", ff.getValues().toString());
    // with minCount > 3
    query.setFacetMinCount(4);
    rsp = client.query(query);
    ff = rsp.getFacetField("features");
    assertEquals("[two (5)]", ff.getValues().toString());
    // with minCount > 3
    query.setFacetMinCount(-1);
    rsp = client.query(query);
    ff = rsp.getFacetField("features");
// System.out.println( rsp.getResults().getNumFound() + " :::: 444: "+ff.getValues() );
}
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) FacetField(org.apache.solr.client.solrj.response.FacetField) Test(org.junit.Test)

Example 5 with FacetField

use of org.apache.solr.client.solrj.response.FacetField in project ddf by codice.

the class SolrMetacardClientImpl method getContentTypes.

@Override
public Set<ContentType> getContentTypes() {
    Set<ContentType> finalSet = new HashSet<>();
    String contentTypeField = resolver.getField(Metacard.CONTENT_TYPE, AttributeType.AttributeFormat.STRING, true);
    String contentTypeVersionField = resolver.getField(Metacard.CONTENT_TYPE_VERSION, AttributeType.AttributeFormat.STRING, true);
    /*
         * If we didn't find the field, it most likely means it does not exist. If it does not
         * exist, then we can safely say that no content types are in this catalog provider
         */
    if (contentTypeField == null || contentTypeVersionField == null) {
        return finalSet;
    }
    SolrQuery query = new SolrQuery(contentTypeField + ":[* TO *]");
    query.setFacet(true);
    query.addFacetField(contentTypeField);
    query.addFacetPivotField(contentTypeField + "," + contentTypeVersionField);
    try {
        QueryResponse solrResponse = client.query(query, SolrRequest.METHOD.POST);
        List<FacetField> facetFields = solrResponse.getFacetFields();
        for (Map.Entry<String, List<PivotField>> entry : solrResponse.getFacetPivot()) {
            // however, the content type names can still be obtained via the facet fields.
            if (CollectionUtils.isEmpty(entry.getValue())) {
                LOGGER.debug("No content type versions found associated with any available content types.");
                if (CollectionUtils.isNotEmpty(facetFields)) {
                    // values (content type names).
                    for (FacetField.Count currContentType : facetFields.get(0).getValues()) {
                        // unknown version, so setting it to null
                        ContentType contentType = new ContentTypeImpl(currContentType.getName(), null);
                        finalSet.add(contentType);
                    }
                }
            } else {
                for (PivotField pf : entry.getValue()) {
                    String contentTypeName = pf.getValue().toString();
                    LOGGER.debug("contentTypeName: {}", contentTypeName);
                    if (CollectionUtils.isEmpty(pf.getPivot())) {
                        // if there are no sub-pivots, that means that there are no content type
                        // versions
                        // associated with this content type name
                        LOGGER.debug("Content type does not have associated contentTypeVersion: {}", contentTypeName);
                        ContentType contentType = new ContentTypeImpl(contentTypeName, null);
                        finalSet.add(contentType);
                    } else {
                        for (PivotField innerPf : pf.getPivot()) {
                            LOGGER.debug("contentTypeVersion: {}. For contentTypeName: {}", innerPf.getValue(), contentTypeName);
                            ContentType contentType = new ContentTypeImpl(contentTypeName, innerPf.getValue().toString());
                            finalSet.add(contentType);
                        }
                    }
                }
            }
        }
    } catch (SolrServerException | IOException e) {
        LOGGER.info("Solr exception getting content types", e);
    }
    return finalSet;
}
Also used : ContentTypeImpl(ddf.catalog.data.impl.ContentTypeImpl) ContentType(ddf.catalog.data.ContentType) SolrServerException(org.apache.solr.client.solrj.SolrServerException) FacetField(org.apache.solr.client.solrj.response.FacetField) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) PivotField(org.apache.solr.client.solrj.response.PivotField) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

FacetField (org.apache.solr.client.solrj.response.FacetField)5 ArrayList (java.util.ArrayList)4 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)4 HashSet (java.util.HashSet)3 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 SolrServerException (org.apache.solr.client.solrj.SolrServerException)2 SolrDocumentList (org.apache.solr.common.SolrDocumentList)2 SolrInputDocument (org.apache.solr.common.SolrInputDocument)2 Test (org.junit.Test)2 AbstractIterator (com.google.common.collect.AbstractIterator)1 ContentType (ddf.catalog.data.ContentType)1 ContentTypeImpl (ddf.catalog.data.impl.ContentTypeImpl)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 Deque (java.util.Deque)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1