Search in sources :

Example 21 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project Xponents by OpenSextant.

the class TaxonMatcher method extractorImpl.

/**
     * Implementation details -- use with or without the formal ID/buffer
     * pairing.
     *
     * @param id
     *            doc id
     * @param buf
     *            input text
     * @return list of matches
     * @throws ExtractionException
     */
private List<TextMatch> extractorImpl(String id, String buf) throws ExtractionException {
    List<TextMatch> matches = new ArrayList<TextMatch>();
    String docid = (id != null ? id : NO_DOC_ID);
    Map<Integer, Object> beanMap = new HashMap<Integer, Object>(100);
    QueryResponse response = tagTextCallSolrTagger(buf, docid, beanMap);
    @SuppressWarnings("unchecked") List<NamedList<?>> tags = (List<NamedList<?>>) response.getResponse().get("tags");
    log.debug("TAGS SIZE = {}", tags.size());
    /*
         * Retrieve all offsets into a long list.
         */
    TaxonMatch m = null;
    // int x1 = -1, x2 = -1;
    int tag_count = 0;
    String id_prefix = docid + "#";
    for (NamedList<?> tag : tags) {
        m = new TaxonMatch();
        m.start = ((Integer) tag.get("startOffset")).intValue();
        // +1 char after
        m.end = ((Integer) tag.get("endOffset")).intValue();
        // last matched
        // m.pattern_id = "taxtag";
        ++tag_count;
        m.match_id = id_prefix + tag_count;
        // m.setText((String) tag.get("matchText")); // Not reliable.
        // matchText can be null.
        m.setText(buf.substring(m.start, m.end));
        if (TextUtils.countFormattingSpace(m.getText()) > 1) {
            // Phrase with a single TAB is okay
            continue;
        }
        @SuppressWarnings("unchecked") List<Integer> taxonIDs = (List<Integer>) tag.get("ids");
        for (Integer solrId : taxonIDs) {
            Object refData = beanMap.get(solrId);
            if (refData == null) {
                continue;
            }
            /*
                 * Filter out non-Acronyms. e.g., 'who' is not a match for 'WHO'
                 */
            Taxon tx = (Taxon) refData;
            if (this.filterNonAcronyms) {
                if (tx.isAcronym && !m.isUpper()) {
                    continue;
                }
            }
            m.addTaxon(tx);
        }
        //
        if (m.hasTaxons()) {
            matches.add(m);
        }
    }
    log.debug("FOUND LABELS count={}", matches.size());
    return matches;
}
Also used : HashMap(java.util.HashMap) NamedList(org.apache.solr.common.util.NamedList) Taxon(org.opensextant.data.Taxon) ArrayList(java.util.ArrayList) TextMatch(org.opensextant.extraction.TextMatch) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List)

Example 22 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project Xponents by OpenSextant.

the class SolrProxy method searchGazetteer.

/**
     * Search an OpenSextant solr gazetteer.
     *
     * @param index solr server handle
     * @param qparams search parameters
     * @return list of places
     * @throws SolrServerException on err
     */
public static List<Place> searchGazetteer(SolrServer index, SolrParams qparams) throws SolrServerException {
    QueryResponse response = index.query(qparams, SolrRequest.METHOD.GET);
    List<Place> places = new ArrayList<>();
    SolrDocumentList docList = response.getResults();
    for (SolrDocument solrDoc : docList) {
        places.add(SolrProxy.createPlace(solrDoc));
    }
    return places;
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ArrayList(java.util.ArrayList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Place(org.opensextant.data.Place)

Example 23 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project incubator-atlas by apache.

the class Solr5Index method query.

@Override
public Iterable<RawQuery.Result<String>> query(RawQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    List<RawQuery.Result<String>> result;
    String collection = query.getStore();
    String keyIdField = getKeyFieldId(collection);
    SolrQuery solrQuery = new SolrQuery(query.getQuery()).addField(keyIdField).setIncludeScore(true).setStart(query.getOffset()).setRows(query.hasLimit() ? query.getLimit() : maxResults);
    try {
        QueryResponse response = solrClient.query(collection, solrQuery);
        if (logger.isDebugEnabled())
            logger.debug("Executed query [{}] in {} ms", query.getQuery(), response.getElapsedTime());
        int totalHits = response.getResults().size();
        if (!query.hasLimit() && totalHits >= maxResults) {
            logger.warn("Query result set truncated to first [{}] elements for query: {}", maxResults, query);
        }
        result = new ArrayList<>(totalHits);
        for (SolrDocument hit : response.getResults()) {
            double score = Double.parseDouble(hit.getFieldValue("score").toString());
            result.add(new RawQuery.Result<>(hit.getFieldValue(keyIdField).toString(), score));
        }
    } catch (IOException e) {
        logger.error("Query did not complete : ", e);
        throw new PermanentBackendException(e);
    } catch (SolrServerException e) {
        logger.error("Unable to query Solr index.", e);
        throw new PermanentBackendException(e);
    }
    return result;
}
Also used : PermanentBackendException(com.thinkaurelius.titan.diskstorage.PermanentBackendException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) RawQuery(com.thinkaurelius.titan.diskstorage.indexing.RawQuery)

Example 24 with QueryResponse

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

the class SolrIndexEditorIT method testAddSomeNodes.

@Test
public void testAddSomeNodes() throws Exception {
    Root r = createRoot();
    r.getTree("/").addChild("a").addChild("b").addChild("doc1").setProperty("text", "hit that hot hat tattoo");
    r.getTree("/").getChild("a").addChild("c").addChild("doc2").setProperty("text", "it hits hot hats");
    r.getTree("/").getChild("a").getChild("b").addChild("doc3").setProperty("text", "tattoos hate hot hits");
    r.getTree("/").getChild("a").getChild("b").addChild("doc4").setProperty("text", "hats tattoos hit hot");
    r.commit();
    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    QueryResponse queryResponse = server.query(query);
    assertTrue("no documents were indexed", queryResponse.getResults().size() > 0);
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrBaseTest(org.apache.jackrabbit.oak.plugins.index.solr.SolrBaseTest) Test(org.junit.Test)

Example 25 with QueryResponse

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

the class SolrIndexEditorTest method testIndexedProperties.

@Test
public void testIndexedProperties() throws Exception {
    SolrServer solrServer = TestUtils.createSolrServer();
    OakSolrConfiguration configuration = TestUtils.getTestConfiguration();
    IndexUpdateCallback callback = mock(IndexUpdateCallback.class);
    SolrIndexEditor solrIndexEditor = new SolrIndexEditor(solrServer, configuration, callback);
    NodeState before = mock(NodeState.class);
    NodeState after = mock(NodeState.class);
    Iterable properties = new Iterable<PropertyState>() {

        @Override
        public Iterator<PropertyState> iterator() {
            return Arrays.asList(PropertyStates.createProperty("foo1", "bar")).iterator();
        }
    };
    when(after.getProperties()).thenReturn(properties);
    solrIndexEditor.leave(before, after);
    QueryResponse queryResponse = solrServer.query(new SolrQuery("foo1:*"));
    assertEquals(1, queryResponse.getResults().getNumFound());
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) IndexUpdateCallback(org.apache.jackrabbit.oak.plugins.index.IndexUpdateCallback) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) OakSolrConfiguration(org.apache.jackrabbit.oak.plugins.index.solr.configuration.OakSolrConfiguration) SolrServer(org.apache.solr.client.solrj.SolrServer) SolrQuery(org.apache.solr.client.solrj.SolrQuery) PropertyState(org.apache.jackrabbit.oak.api.PropertyState) Test(org.junit.Test)

Aggregations

QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)285 SolrQuery (org.apache.solr.client.solrj.SolrQuery)123 Test (org.junit.Test)111 SolrDocument (org.apache.solr.common.SolrDocument)78 SolrInputDocument (org.apache.solr.common.SolrInputDocument)67 SolrDocumentList (org.apache.solr.common.SolrDocumentList)60 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)58 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)56 SolrServerException (org.apache.solr.client.solrj.SolrServerException)42 ArrayList (java.util.ArrayList)39 IOException (java.io.IOException)35 NamedList (org.apache.solr.common.util.NamedList)32 SolrClient (org.apache.solr.client.solrj.SolrClient)27 SolrParams (org.apache.solr.common.params.SolrParams)27 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)26 ErrorTrackingConcurrentUpdateSolrClient (org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient)25 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)25 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)23 HashMap (java.util.HashMap)21 PivotField (org.apache.solr.client.solrj.response.PivotField)19