Java Code Examples for org.apache.solr.search.SolrIndexSearcher#search()

The following examples show how to use org.apache.solr.search.SolrIndexSearcher#search() . 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: TestHierarchicalDocBuilder.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void assertSearch(Query query, String field, String... values) throws IOException {
  /* The limit of search queue is doubled to catch the error in case when for some reason there are more docs than expected  */
  SolrIndexSearcher searcher = req.getSearcher();
  TopDocs result = searcher.search(query, values.length * 2);
  assertEquals(values.length, result.totalHits.value);
  List<String> actualValues = new ArrayList<String>();
  for (int index = 0; index < values.length; ++index) {
    Document doc = searcher.doc(result.scoreDocs[index].doc);
    actualValues.add(doc.get(field));
  }
  
  for (String expectedValue: values) {
    boolean removed = actualValues.remove(expectedValue);
    if (!removed) {
      fail("Search result does not contain expected values");
    }
  }
}
 
Example 2
Source File: SolrAuthoritySetQuery.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
private BitsFilter getOwnerFilter(String[] auths, SolrIndexSearcher searcher) throws IOException
{
    Builder builder = new BooleanQuery.Builder();
    for(String current : auths)
    {
        if (AuthorityType.getAuthorityType(current) == AuthorityType.USER)
        {
        	builder.add(new TermQuery(new Term(QueryConstants.FIELD_OWNER, current)), BooleanClause.Occur.SHOULD);
        }
    }

    BitsFilterCollector collector = new BitsFilterCollector(searcher.getTopReaderContext().leaves().size());
    searcher.search(builder.build(), collector);
    return collector.getBitsFilter();
}
 
Example 3
Source File: SolrOwnerQuery.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
private BitsFilter getOwnerFilter(String owner, SolrIndexSearcher searcher) throws IOException
{
    Query query =  new TermQuery(new Term(QueryConstants.FIELD_OWNER, owner));
    BitsFilterCollector collector = new BitsFilterCollector(searcher.getTopReaderContext().leaves().size());
    searcher.search(query, collector);
    return collector.getBitsFilter();
}
 
Example 4
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Set<Long> getErrorDocIds() throws IOException
{
    Set<Long> errorDocIds = new HashSet<>();
    RefCounted<SolrIndexSearcher> refCounted = null;
    try
    {
        refCounted = this.core.getSearcher();
        SolrIndexSearcher searcher = refCounted.get();
        TermQuery errorQuery = new TermQuery(new Term(FIELD_DOC_TYPE, DOC_TYPE_ERROR_NODE));
        DocListCollector docListCollector = new DocListCollector();
        searcher.search(errorQuery, docListCollector);
        IntArrayList docList = docListCollector.getDocs();
        int size = docList.size();

        for (int i = 0; i < size; ++i)
        {
            int doc = docList.get(i);
            Document document = searcher.doc(doc, REQUEST_ONLY_ID_FIELD);
            IndexableField id = document.getField(FIELD_SOLR4_ID);
            String idString = id.stringValue();

            if (idString.startsWith(PREFIX_ERROR))
            {
                idString = idString.substring(PREFIX_ERROR.length());
            }

            errorDocIds.add(Long.valueOf(idString));
        }
    }
    finally
    {
        ofNullable(refCounted).ifPresent(RefCounted::decref);
    }
    return errorDocIds;
}
 
Example 5
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean isInIndex(long id, LRU cache, String fieldName, boolean populateCache, SolrCore core) throws IOException
{
    if(cache.containsKey(id))
    {
        return true;
    }
    else
    {
        RefCounted<SolrIndexSearcher> refCounted = null;
        try
        {
            if(populateCache)
            {
                cache.put(id, null); // Safe to add this here because we reset this on rollback.
            }
            refCounted = core.getSearcher();
            SolrIndexSearcher searcher = refCounted.get();
            FieldType fieldType = searcher.getSchema().getField(fieldName).getType();
            TermQuery q = new TermQuery(new Term(fieldName, fieldType.readableToIndexed(Long.toString(id))));
            TopDocs topDocs = searcher.search(q, 1);
            return topDocs.totalHits > 0;
        }
        finally
        {
            ofNullable(refCounted).ifPresent(RefCounted::decref);
        }
    }
}
 
Example 6
Source File: AuthQueryIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Queries the index and asserts if the count matches documents returned.
 * @param queryString
 * @param count
 * @throws IOException
 * @throws org.apache.lucene.queryparser.classic.ParseException
 */
private void assertFTSQuery(String queryString,
                          int count,
                          String... name) throws IOException, ParseException
{
    SolrServletRequest solrQueryRequest = null;
    RefCounted<SolrIndexSearcher>refCounted = null;
    try
    {
        solrQueryRequest = new SolrServletRequest(getCore(), null);
        refCounted = getCore().getSearcher(false, true, null);
        SolrIndexSearcher solrIndexSearcher = refCounted.get();
        
        SearchParameters searchParameters = new SearchParameters();
        searchParameters.setQuery(queryString);
        Query query = dataModel.getFTSQuery(new Pair<SearchParameters, Boolean>(searchParameters, Boolean.FALSE),
                solrQueryRequest, FTSQueryParser.RerankPhase.SINGLE_PASS);
        TopDocs docs = solrIndexSearcher.search(query, count * 2 + 10);
    
        Assert.assertEquals(count, docs.totalHits);
    } 
    finally
    {
        refCounted.decref();
        solrQueryRequest.close();
    }
}
 
Example 7
Source File: AbstractAlfrescoSolrIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void waitForDocCount(Query query, long expectedNumFound, long waitMillis)
        throws Exception
{
    Date date = new Date();
    long timeout = date.getTime() + waitMillis;

    RefCounted<SolrIndexSearcher> ref = null;
    int totalHits = 0;
    while(new Date().getTime() < timeout)
    {
        try
        {
            ref = getCore().getSearcher();
            SolrIndexSearcher searcher = ref.get();
            TopDocs topDocs = searcher.search(query, 10);
            totalHits = topDocs.totalHits;
            if (topDocs.totalHits == expectedNumFound)
            {
                LOG.warn("Query \"" + query + "\" returned " + totalHits + " as expected");
                return;
            }
            else
            {
                LOG.warn("Query \"" + query + "\" returned " + totalHits + ", expected " + expectedNumFound);
                Thread.sleep(2000);
            }
        }
        finally
        {
            ref.decref();
        }
    }
    throw new Exception("Wait error expected "+expectedNumFound+" found "+totalHits+" : "+query.toString());
}
 
Example 8
Source File: QueryComponent.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void doProcessUngroupedSearch(ResponseBuilder rb, QueryCommand cmd, QueryResult result) throws IOException {

    SolrQueryRequest req = rb.req;
    SolrQueryResponse rsp = rb.rsp;

    SolrIndexSearcher searcher = req.getSearcher();

    try {
      searcher.search(result, cmd);
    } catch (FuzzyTermsEnum.FuzzyTermsException e) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
    }
    rb.setResult(result);

    ResultContext ctx = new BasicResultContext(rb);
    rsp.addResponse(ctx);
    rsp.getToLog().add("hits", rb.getResults()==null || rb.getResults().docList==null ? 0 : rb.getResults().docList.matches());

    if ( ! rb.req.getParams().getBool(ShardParams.IS_SHARD,false) ) {
      if (null != rb.getNextCursorMark()) {
        rb.rsp.add(CursorMarkParams.CURSOR_MARK_NEXT,
                   rb.getNextCursorMark().getSerializedTotem());
      }
    }

    if(rb.mergeFieldHandler != null) {
      rb.mergeFieldHandler.handleMergeFields(rb, searcher);
    } else {
      doFieldSortValues(rb, searcher);
    }

    doPrefetch(rb);
  }
 
Example 9
Source File: ClassificationUpdateProcessorIntegrationTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private Document getDoc(String id) throws IOException {
  try (SolrQueryRequest req = req()) {
    SolrIndexSearcher searcher = req.getSearcher();
    TermQuery query = new TermQuery(new Term(ID, id));
    TopDocs doc1 = searcher.search(query, 1);
    ScoreDoc scoreDoc = doc1.scoreDocs[0];
    return searcher.doc(scoreDoc.doc);
  }
}
 
Example 10
Source File: AddBlockUpdateTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testOverwrite() throws IOException{
  assertU(add(
    nest(doc("id","X", parent, "X"),
           doc(child,"a", "id", "66"),
           doc(child,"b", "id", "66"))));
  assertU(add(
    nest(doc("id","Y", parent, "Y"),
           doc(child,"a", "id", "66"),
           doc(child,"b", "id", "66"))));
  String overwritten = random().nextBoolean() ? "X": "Y";
  String dubbed = overwritten.equals("X") ? "Y":"X";

  assertU(add(
      nest(doc("id",overwritten, parent, overwritten),
             doc(child,"c","id", "66"),
             doc(child,"d","id", "66")), "overwrite", "true"));
  assertU(add(
      nest(doc("id",dubbed, parent, dubbed),
             doc(child,"c","id", "66"),
             doc(child,"d","id", "66")), "overwrite", "false"));

  assertU(commit());

  assertQ(req(parent+":"+overwritten, "//*[@numFound='1']"));
  assertQ(req(parent+":"+dubbed, "//*[@numFound='2']"));

  final SolrIndexSearcher searcher = getSearcher();
  assertSingleParentOf(searcher, one("ab"), dubbed);

  final TopDocs docs = searcher.search(join(one("cd")), 10);
  assertEquals(2, docs.totalHits.value);
  final String pAct = searcher.doc(docs.scoreDocs[0].doc).get(parent)+
                      searcher.doc(docs.scoreDocs[1].doc).get(parent);
  assertTrue(pAct.contains(dubbed) && pAct.contains(overwritten) && pAct.length()==2);

  assertQ(req("id:66", "//*[@numFound='6']"));
  assertQ(req(child+":(a b)", "//*[@numFound='2']"));
  assertQ(req(child+":(c d)", "//*[@numFound='4']"));
}
 
Example 11
Source File: AddBlockUpdateTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void assertSingleParentOf(final SolrIndexSearcher searcher,
    final String childTerm, String parentExp) throws IOException {
  final TopDocs docs = searcher.search(join(childTerm), 10);
  assertEquals(1, docs.totalHits.value);
  final String pAct = searcher.doc(docs.scoreDocs[0].doc).get(parent);
  assertEquals(parentExp, pAct);
}
 
Example 12
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public List<NodeMetaData> getCascadeNodes(List<Long> txnIds) throws IOException, JSONException
{
    List<FieldInstance> list = dataModel.getIndexedFieldNamesForProperty(ContentModel.PROP_CASCADE_TX).getFields();
    FieldInstance fieldInstance = list.get(0);

    RefCounted<SolrIndexSearcher> refCounted = null;
    IntArrayList docList;
    Set<Long> parentNodesId = new HashSet<>();

    try
    {
        refCounted = core.getSearcher();
        SolrIndexSearcher searcher = refCounted.get();
        String field = fieldInstance.getField();
        SchemaField schemaField = searcher.getSchema().getField(field);
        FieldType fieldType = schemaField.getType();
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        BooleanQuery booleanQuery;

        for(Long l : txnIds)
        {
            BytesRefBuilder bytesRefBuilder = new BytesRefBuilder();
            fieldType.readableToIndexed(l.toString(), bytesRefBuilder);
            TermQuery termQuery = new TermQuery(new Term(field, bytesRefBuilder.toBytesRef()));
            BooleanClause booleanClause = new BooleanClause(termQuery, BooleanClause.Occur.SHOULD);
            builder.add(booleanClause);
        }

        booleanQuery = builder.build();

        DocListCollector collector = new DocListCollector();
        searcher.search(booleanQuery, collector);
        docList = collector.getDocs();
        int size = docList.size();
        for(int i=0; i<size; i++)
        {
            int docId = docList.get(i);
            Document document = searcher.doc(docId, REQUEST_ONLY_ID_FIELD);
            IndexableField indexableField = document.getField(FIELD_SOLR4_ID);
            String id = indexableField.stringValue();
            TenantDbId ids = AlfrescoSolrDataModel.decodeNodeDocumentId(id);
            parentNodesId.add(ids.dbId);
        }
    }
    finally
    {
        ofNullable(refCounted).ifPresent(RefCounted::decref);
    }

    List<NodeMetaData> allNodeMetaDatas = new ArrayList<>();

    for (Long parentNodeId : parentNodesId)
    {
        NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
        nmdp.setFromNodeId(parentNodeId);
        nmdp.setToNodeId(parentNodeId);
        nmdp.setIncludeAclId(true);
        nmdp.setIncludeChildAssociations(false);
        nmdp.setIncludeChildIds(true);
        nmdp.setIncludeOwner(false);
        nmdp.setIncludeParentAssociations(false);
        nmdp.setIncludePaths(true);
        nmdp.setIncludeProperties(false);
        nmdp.setIncludeTxnId(true);
        nmdp.setMaxResults(1);
        // Gets only one
        Optional<Collection<NodeMetaData>> nodeMetaDatas = getNodesMetaDataFromRepository(nmdp);
        allNodeMetaDatas.addAll(nodeMetaDatas.orElse(Collections.emptyList()));
    }

    return allNodeMetaDatas;
}
 
Example 13
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public List<Transaction> getCascades(int num) throws IOException
{
    RefCounted<SolrIndexSearcher> refCounted = null;
    try
    {
        refCounted = this.core.getSearcher();
        SolrIndexSearcher searcher = refCounted.get();

        Collector collector;

        TopFieldCollector topFieldCollector = TopFieldCollector.create(new Sort(new SortField(FIELD_TXID, SortField.Type.LONG)),
                                                                        num,
                                                                        null,
                                                                        false,
                                                                        false,
                                                                        false);

        collector = topFieldCollector;

        LegacyNumericRangeQuery q = LegacyNumericRangeQuery.newIntRange(FIELD_CASCADE_FLAG, 1, 1, true, true);
        DelegatingCollector delegatingCollector = new TxnCacheFilter(cleanCascadeCache);

        delegatingCollector.setLastDelegate(collector);
        collector = delegatingCollector;

        searcher.search(q, collector);
        ScoreDoc[] scoreDocs = topFieldCollector.topDocs().scoreDocs;

        Set<String> fields = new HashSet<>();
        fields.add(FIELD_S_TXID);
        fields.add(FIELD_S_TXCOMMITTIME);

        List<Transaction> transactions = new ArrayList<>(scoreDocs.length);

        for(ScoreDoc scoreDoc : scoreDocs)
        {
            Transaction transaction = new Transaction();
            Document doc = searcher.doc(scoreDoc.doc, fields);

            IndexableField txID = doc.getField(FIELD_S_TXID);
            long txnID = txID.numericValue().longValue();
            cleanCascadeCache.put(txnID, null);
            transaction.setId(txnID);

            IndexableField txnCommitTime = doc.getField(FIELD_S_TXCOMMITTIME);
            transaction.setCommitTimeMs(txnCommitTime.numericValue().longValue());

            transactions.add(transaction);
        }

        return transactions;
    }
    finally
    {
        ofNullable(refCounted).ifPresent(RefCounted::decref);
    }
}
 
Example 14
Source File: AbstractAlfrescoSolrIT.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void assertAQuery(
        String queryString,
        int count,
        Locale locale,
        String[] textAttributes,
        String[] allAttributes,
        String... name)
{
    RefCounted<SolrIndexSearcher>refCounted = null;
    try (SolrServletRequest solrQueryRequest = new SolrServletRequest(getCore(), null))
    {
        refCounted = getCore().getSearcher();
        SolrIndexSearcher solrIndexSearcher = refCounted.get();
        SearchParameters searchParameters = new SearchParameters();
        searchParameters.setQuery(queryString);
        if (locale != null)
        {
            searchParameters.addLocale(locale);
        }

        if (textAttributes != null)
        {
            for (String textAttribute : textAttributes)
            {
                searchParameters.addTextAttribute(textAttribute);
        }
    }
    if (allAttributes != null)
    {
        for (String allAttribute : allAttributes)
        {
            searchParameters.addAllAttribute(allAttribute);
        }
    }

    Query query = dataModel.getLuceneQueryParser(searchParameters, solrQueryRequest, FTSQueryParser.RerankPhase.SINGLE_PASS).parse(queryString);
    LOG.debug("####### Query ######:"+query);
    TopDocs docs = solrIndexSearcher.search(query, count * 2 + 10);

        assertEquals(fixQueryString(queryString, name), count, docs.totalHits);
    }
    catch(Exception exception)
    {
        exception.printStackTrace();
        throw new RuntimeException(exception);
    }
    finally
    {
        ofNullable(refCounted).ifPresent(RefCounted::decref);
    }
}
 
Example 15
Source File: SimpleFacets.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public NamedList<Integer> getGroupedCounts(SolrIndexSearcher searcher,
                                           DocSet base,
                                           String field,
                                           boolean multiToken,
                                           int offset,
                                           int limit,
                                           int mincount,
                                           boolean missing,
                                           String sort,
                                           String prefix,
                                           Predicate<BytesRef> termFilter) throws IOException {
  GroupingSpecification groupingSpecification = rb.getGroupingSpec();
  String[] groupFields = groupingSpecification != null? groupingSpecification.getFields(): null;
  final String groupField = ArrayUtils.isNotEmpty(groupFields) ? groupFields[0] : null;
  if (groupField == null) {
    throw new SolrException (
        SolrException.ErrorCode.BAD_REQUEST,
        "Specify the group.field as parameter or local parameter"
    );
  }

  BytesRef prefixBytesRef = prefix != null ? new BytesRef(prefix) : null;
  final TermGroupFacetCollector collector = TermGroupFacetCollector.createTermGroupFacetCollector(groupField, field, multiToken, prefixBytesRef, 128);
  
  Collector groupWrapper = getInsanityWrapper(groupField, collector);
  Collector fieldWrapper = getInsanityWrapper(field, groupWrapper);
  // When GroupedFacetCollector can handle numerics we can remove the wrapped collectors
  searcher.search(base.getTopFilter(), fieldWrapper);
  
  boolean orderByCount = sort.equals(FacetParams.FACET_SORT_COUNT) || sort.equals(FacetParams.FACET_SORT_COUNT_LEGACY);
  TermGroupFacetCollector.GroupedFacetResult result 
    = collector.mergeSegmentResults(limit < 0 ? Integer.MAX_VALUE : 
                                    (offset + limit), 
                                    mincount, orderByCount);

  CharsRefBuilder charsRef = new CharsRefBuilder();
  FieldType facetFieldType = searcher.getSchema().getFieldType(field);
  NamedList<Integer> facetCounts = new NamedList<>();
  List<TermGroupFacetCollector.FacetEntry> scopedEntries 
    = result.getFacetEntries(offset, limit < 0 ? Integer.MAX_VALUE : limit);
  for (TermGroupFacetCollector.FacetEntry facetEntry : scopedEntries) {
    //:TODO:can we filter earlier than this to make it more efficient?
    if (termFilter != null && !termFilter.test(facetEntry.getValue())) {
      continue;
    }
    facetFieldType.indexedToReadable(facetEntry.getValue(), charsRef);
    facetCounts.add(charsRef.toString(), facetEntry.getCount());
  }

  if (missing) {
    facetCounts.add(null, result.getTotalMissingCount());
  }

  return facetCounts;
}
 
Example 16
Source File: AbstractSolrQueryFacet.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * Start the collection for this facet value.
 *
 * @param searcher the solr searcher
 * @throws IOException if an exception occurs during the querying
 */
public void execute(SolrIndexSearcher searcher) throws IOException {
  collectionManager.clearLastingCollectTargets();
  collectionManager.addLastingCollectTarget(collection);
  searcher.search(query, this);
}