Java Code Examples for org.apache.solr.common.SolrDocument#getFieldValue()

The following examples show how to use org.apache.solr.common.SolrDocument#getFieldValue() . 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: SecureRealTimeGetComponent.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
/**
 * @param doc SolrDocument to check
 * @param idField field where the id is stored
 * @param fieldType type of id field
 * @param filterQuery Query to filter by
 * @param searcher SolrIndexSearcher on which to apply the filter query
 * @returns the internal docid, or -1 if doc is not found or doesn't match filter
 */
private static int getFilteredInternalDocId(SolrDocument doc, SchemaField idField, FieldType fieldType,
      Query filterQuery, SolrIndexSearcher searcher) throws IOException {
  int docid = -1;
  Field f = (Field)doc.getFieldValue(idField.getName());
  String idStr = f.stringValue();
  BytesRef idBytes = new BytesRef();
  fieldType.readableToIndexed(idStr, idBytes);
  // get the internal document id
  long segAndId = searcher.lookupId(idBytes);

    // if docid is valid, run it through the filter
  if (segAndId >= 0) {
    int segid = (int) segAndId;
    AtomicReaderContext ctx = searcher.getTopReaderContext().leaves().get((int) (segAndId >> 32));
    docid = segid + ctx.docBase;
    Weight weight = filterQuery.createWeight(searcher);
    Scorer scorer = weight.scorer(ctx, null);
    if (scorer == null || segid != scorer.advance(segid)) {
      // filter doesn't match.
      docid = -1;
    }
  }
  return docid;
}
 
Example 2
Source File: SolrCopyConsumer.java    From extract with MIT License 6 votes vote down vote up
private void copyField(final String from, final SolrDocument input,
	final SolrInputDocument output) {
	final Map<String, Object> atomic = new HashMap<>();
	String to = map.get(from);

	// If there's no target field, copy the field onto itself.
	// This forces reindexing in Solr.
	if (null == to) {
		to = from;
	}

	// The ID field can't be set atomically.
	if (to.equals(idField)) {
		output.setField(to, input.getFieldValue(idField));
	} else {
		Object value = input.getFieldValue(from);

		// Fix bad values.
		if (value instanceof String && ((String) value).startsWith(BAD_VALUE)) {
			value = ((String) value).substring(BAD_VALUE.length());
		}

		atomic.put("set", value);
		output.setField(to, atomic);
	}
}
 
Example 3
Source File: SolrTaggingConsumer.java    From extract with MIT License 6 votes vote down vote up
@Override
protected void consume(final SolrDocument input) throws SolrServerException, IOException {
	final SolrInputDocument output = new SolrInputDocument(); 
	final String id = (String) input.getFieldValue(idField);

	output.setField(idField, id);
	for (Map.Entry<String, String> entry : literals.entrySet()) {
		Map<String, Object> atomic = new HashMap<>();

		atomic.put("set", entry.getValue());
		output.setField(entry.getKey(), atomic);
	}

	logger.info(String.format("Tagging document with ID \"%s\".", id));
	destination.add(output);
}
 
Example 4
Source File: SolrRrdBackendFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
SolrRrdBackend.SyncData getData(String path) throws IOException {
  if (!persistent) {
    return null;
  }
  try {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add(CommonParams.Q, "{!term f=id}" + ID_PREFIX + ID_SEP + path);
    params.add(CommonParams.FQ, CommonParams.TYPE + ":" + DOC_TYPE);
    QueryResponse rsp = solrClient.query(collection, params);
    SolrDocumentList docs = rsp.getResults();
    if (docs == null || docs.isEmpty()) {
      return null;
    }
    if (docs.size() > 1) {
      throw new SolrServerException("Expected at most 1 doc with id '" + path + "' but got " + docs);
    }
    SolrDocument doc = docs.get(0);
    Object o = doc.getFieldValue(DATA_FIELD);
    if (o == null) {
      return null;
    }
    if (o instanceof byte[]) {
      Object timeObj = doc.getFieldValue("timestamp_l");
      Long time = timeObj instanceof Number ? ((Number)timeObj).longValue() : Long.parseLong(String.valueOf(timeObj));
      return new SolrRrdBackend.SyncData((byte[])o, time);
    } else {
      throw new SolrServerException("Unexpected value of '" + DATA_FIELD + "' field: " + o.getClass().getName() + ": " + o);
    }
  } catch (SolrServerException e) {
    throw new IOException(e);
  }
}
 
Example 5
Source File: TopicStream.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void getPersistedCheckpoints() throws IOException {
  ZkStateReader zkStateReader = cloudSolrClient.getZkStateReader();
  Slice[] slices = CloudSolrStream.getSlices(checkpointCollection, zkStateReader, false);

  ClusterState clusterState = zkStateReader.getClusterState();
  Set<String> liveNodes = clusterState.getLiveNodes();

  OUTER:
  for(Slice slice : slices) {
    Collection<Replica> replicas = slice.getReplicas();
    for(Replica replica : replicas) {
      if(replica.getState() == Replica.State.ACTIVE && liveNodes.contains(replica.getNodeName())){
        HttpSolrClient httpClient = streamContext.getSolrClientCache().getHttpSolrClient(replica.getCoreUrl());
        try {
          SolrDocument doc = httpClient.getById(id);
          if(doc != null) {
            @SuppressWarnings({"unchecked"})
            List<String> checkpoints = (List<String>)doc.getFieldValue("checkpoint_ss");
            for (String checkpoint : checkpoints) {
              String[] pair = checkpoint.split("~");
              this.checkpoints.put(pair[0], Long.parseLong(pair[1]));
            }
          }
        } catch (Exception e) {
          throw new IOException(e);
        }
        break OUTER;
      }
    }
  }
}
 
Example 6
Source File: SolrDocumentBuilder.java    From chronix.server with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the given solr document in a metric time series
 *
 * @param doc        the lucene document
 * @param queryStart the query start
 * @param queryEnd   the query end
 * @param decompress marks if the data is requested and hence we have to decompress it or not
 * @return a metric time series
 */
private static MetricTimeSeries convert(SolrDocument doc, long queryStart, long queryEnd, boolean decompress) {


    long tsStart = (long) doc.getFieldValue(Schema.START);
    long tsEnd = (long) doc.getFieldValue(Schema.END);
    byte[] data = ((ByteBuffer) doc.getFieldValue(Schema.DATA)).array();

    String name = doc.getFieldValue(Schema.NAME).toString();
    String type = doc.getFieldValue(Schema.TYPE).toString();

    MetricTimeSeries.Builder ts = new MetricTimeSeries.Builder(name, type);

    for (Map.Entry<String, Object> field : doc) {
        if (Schema.isUserDefined(field.getKey())) {
            if (field.getValue() instanceof ByteBuffer) {
                ts.attribute(field.getKey(), ((ByteBuffer) field.getValue()).array());
            } else {
                ts.attribute(field.getKey(), field.getValue());
            }

        }
    }
    //No data is requested, hence we do not decompress it
    if (decompress) {
        InputStream decompressed = Compression.decompressToStream(data);
        ProtoBufMetricTimeSeriesSerializer.from(decompressed, tsStart, tsEnd, queryStart, queryEnd, ts);
        IOUtils.closeQuietly(decompressed);
    }
    return ts.build();
}
 
Example 7
Source File: MCROAISolrSearcher.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
Header toHeader(SolrDocument doc, Collection<MCROAISetResolver<String, SolrDocument>> setResolver) {
    Date modified = (Date) doc.getFieldValue(getModifiedField());
    String docId = doc.getFieldValue("id").toString();
    Header header = new Header(getObjectManager().getOAIId(docId), modified.toInstant());
    setResolver.parallelStream()
        .map(r -> r.getSets(docId))
        .flatMap(Collection::stream)
        .sorted(this::compare)
        .sequential()
        .forEachOrdered(header.getSetList()::add);
    return header;
}
 
Example 8
Source File: SolrUtilities.java    From metron with Apache License 2.0 5 votes vote down vote up
public static Document toDocument(SolrDocument solrDocument) {
  Map<String, Object> document = new HashMap<>();
  solrDocument.getFieldNames().stream()
      .filter(name -> !name.equals(SolrDao.VERSION_FIELD))
      .forEach(name -> document.put(name, solrDocument.getFieldValue(name)));

  reformatComments(solrDocument, document);
  insertChildAlerts(solrDocument, document);

  return new Document(document,
          (String) solrDocument.getFieldValue(Constants.GUID),
          (String) solrDocument.getFieldValue(Constants.SENSOR_TYPE),
          (Long) solrDocument.getFieldValue(Constants.Fields.TIMESTAMP.getName()));
}
 
Example 9
Source File: DistributedAlfrescoSolrFingerPrintIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testFingerPrint3() throws Exception
{
    putHandleDefaults();
    QueryResponse response = query(getDefaultTestClient(), true,
        "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}",
        params("q", "FINGERPRINT:" + NODES[0].getId()+"_45",
            "qt", "/afts",
            "shards.qt", "/afts",
            "start", "0",
            "fl", "DBID,score",
            "rows", "100"));

    SolrDocumentList docs = response.getResults();
    assertEquals(3, docs.getNumFound());
    SolrDocument doc0 = docs.get(0);
    long dbid0 = (long)doc0.getFieldValue("DBID");
    assertEquals(dbid0, NODES[0].getId());

    SolrDocument doc1 = docs.get(1);
    long dbid1 = (long)doc1.getFieldValue("DBID");
    assertEquals(dbid1, NODES[2].getId());

    SolrDocument doc2 = docs.get(2);
    long dbid2 = (long)doc2.getFieldValue("DBID");
    assertEquals(dbid2, NODES[1].getId());
}
 
Example 10
Source File: DistributedAlfrescoSolrFingerPrintIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testFingerPrint4() throws Exception
{
    putHandleDefaults();
    QueryResponse response = query(getDefaultTestClient(), true,
        "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}",
        params("q", "FINGERPRINT:"+ NODES_METADATA[0].getNodeRef().getId(),
            "qt", "/afts",
            "shards.qt", "/afts",
            "start", "0",
            "fl","DBID,score",
            "rows", "100"));

    SolrDocumentList docs = response.getResults();
    assertEquals(4, docs.getNumFound());
    SolrDocument doc0 = docs.get(0);
    long dbid0 = (long)doc0.getFieldValue("DBID");
    assertEquals(dbid0, NODES[0].getId());

    SolrDocument doc1 = docs.get(1);
    long dbid1 = (long)doc1.getFieldValue("DBID");
    assertEquals(dbid1, NODES[2].getId());

    SolrDocument doc2 = docs.get(2);
    long dbid2 = (long)doc2.getFieldValue("DBID");
    assertEquals(dbid2, NODES[1].getId());

    SolrDocument doc3 = docs.get(3);
    long dbid3 = (long)doc3.getFieldValue("DBID");
    assertEquals(dbid3, NODES[3].getId());
}
 
Example 11
Source File: AlfrescoFieldMapperTransformerIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void transformDocument_docTransformerSetTwice_shouldWorkWithoutErrors() throws Exception
{
    putHandleDefaults();

    QueryResponse resp = query(getDefaultTestClient(), true, ALFRESCO_JSON, params("q", "*", "qt", "/afts", "shards.qt", "/afts","fl","id, cm_title,[fmap], [fmap]", "sort", "id asc"));

    assertNotNull(resp);
    SolrDocument docWithRequestedFields3 = resp.getResults().get(0);
    assertEquals(2, docWithRequestedFields3.size());
    assertNotNull(docWithRequestedFields3.get("id"));
    String title = (String) docWithRequestedFields3.getFieldValue("cm_title");
    assertEquals("title1", title);
}
 
Example 12
Source File: DistributedAlfrescoSolrFingerPrintIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testFingerPrint6() throws Exception
{
    putHandleDefaults();
    QueryResponse response = query(getDefaultTestClient(), true,
        "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}",
        params("q", "FINGERPRINT:" + NODES_METADATA[0].getNodeRef().getId() +"_45",
            "qt", "/afts",
            "shards.qt", "/afts",
            "start", "0",
            "fl", "DBID,score",
            "rows", "100"));

    SolrDocumentList docs = response.getResults();
    assertEquals(3, docs.getNumFound());
    SolrDocument doc0 = docs.get(0);
    long dbid0 = (long)doc0.getFieldValue("DBID");
    assertEquals(dbid0, NODES[0].getId());

    SolrDocument doc1 = docs.get(1);
    long dbid1 = (long)doc1.getFieldValue("DBID");
    assertEquals(dbid1, NODES[2].getId());

    SolrDocument doc2 = docs.get(2);
    long dbid2 = (long)doc2.getFieldValue("DBID");
    assertEquals(dbid2, NODES[1].getId());
}
 
Example 13
Source File: IndexSchema.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Like {@link #printableUniqueKey(org.apache.lucene.document.Document)} */
public String printableUniqueKey(SolrDocument solrDoc) {
  Object val = solrDoc.getFieldValue(uniqueKeyFieldName);
  if (val == null) {
    return null;
  } else if (val instanceof IndexableField) {
    return uniqueKeyFieldType.toExternal((IndexableField) val);
  } else {
    return val.toString();
  }
}
 
Example 14
Source File: JSONWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void writeSolrDocument(String name, SolrDocument doc, ReturnFields returnFields, int idx) throws IOException {
  if( idx > 0 ) {
    writeArraySeparator();
  }

  indent();
  writeMapOpener(doc.size());
  incLevel();

  boolean first=true;
  for (String fname : doc.getFieldNames()) {
    if (returnFields!= null && !returnFields.wantsField(fname)) {
      continue;
    }

    if (first) {
      first=false;
    }
    else {
      writeMapSeparator();
    }

    indent();
    writeKey(fname, true);
    Object val = doc.getFieldValue(fname);
    writeVal(fname, val);
  }

  if(doc.hasChildDocuments()) {
    if(first == false) {
      writeMapSeparator();
      indent();
    }
    writeKey("_childDocuments_", true);
    writeArrayOpener(doc.getChildDocumentCount());
    List<SolrDocument> childDocs = doc.getChildDocuments();
    for(int i=0; i<childDocs.size(); i++) {
      writeSolrDocument(null, childDocs.get(i), null, i);
    }
    writeArrayCloser();
  }

  decLevel();
  writeMapCloser();
}
 
Example 15
Source File: AmbariInfraWithStormLogSearch.java    From streamline with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public LogSearchResult search(LogSearchCriteria logSearchCriteria) {
    SolrQuery query = new SolrQuery();

    query.setQuery(buildColumnAndValue(COLUMN_NAME_LOG_MESSAGE, buildValue(logSearchCriteria.getSearchString())));
    query.addFilterQuery(buildColumnAndValue(COLUMN_NAME_TYPE, COLUMN_VALUE_TYPE_WORKER_LOG));
    query.addFilterQuery(buildColumnAndValue(COLUMN_NAME_STREAMLINE_TOPOLOGY_ID, buildValue(logSearchCriteria.getAppId())));
    query.addFilterQuery(buildColumnAndValue(COLUMN_NAME_LOG_TIME, buildDateRangeValue(logSearchCriteria.getFrom(), logSearchCriteria.getTo())));

    List<String> componentNames = logSearchCriteria.getComponentNames();
    if (componentNames != null && !componentNames.isEmpty()) {
        query.addFilterQuery(buildColumnAndValue(COLUMN_NAME_STREAMLINE_COMPONENT_NAME, buildORValues(componentNames)));
    }

    List<String> logLevels = logSearchCriteria.getLogLevels();
    if (logLevels == null || logLevels.isEmpty()) {
        logLevels = DEFAULT_LOG_LEVELS;
    }
    query.addFilterQuery(buildColumnAndValue(COLUMN_NAME_LOG_LEVEL, buildORValues(logLevels)));

    if (logSearchCriteria.getAscending() == null || logSearchCriteria.getAscending()) {
        query.addSort(COLUMN_NAME_LOG_TIME, SolrQuery.ORDER.asc);
    } else {
        query.addSort(COLUMN_NAME_LOG_TIME, SolrQuery.ORDER.desc);
    }

    if (logSearchCriteria.getStart() != null) {
        query.setStart(logSearchCriteria.getStart());
    }
    if (logSearchCriteria.getLimit() != null) {
        query.setRows(logSearchCriteria.getLimit());
    }

    LOG.debug("Querying to Solr: query => {}", query);

    long numFound;
    List<LogSearchResult.LogDocument> results = new ArrayList<>();
    try {
        QueryResponse response = solr.query(query);

        SolrDocumentList docList = response.getResults();
        numFound = docList.getNumFound();

        for (SolrDocument document : docList) {
            String appId = (String) document.getFieldValue(COLUMN_NAME_STREAMLINE_TOPOLOGY_ID);
            String componentName = (String) document.getFieldValue(COLUMN_NAME_STREAMLINE_COMPONENT_NAME);
            String logLevel = (String) document.getFieldValue(COLUMN_NAME_LOG_LEVEL);
            String logMessage = (String) document.getFieldValue(COLUMN_NAME_LOG_MESSAGE);
            String host = (String) document.getFieldValue(COLUMN_NAME_HOST);
            String port = (String) document.getFieldValue(COLUMN_NAME_STORM_WORKER_PORT);
            Date logDate = (Date) document.getFieldValue(COLUMN_NAME_LOG_TIME);
            long timestamp = logDate.toInstant().toEpochMilli();

            LogSearchResult.LogDocument logDocument = new LogSearchResult.LogDocument(appId, componentName,
                    logLevel, logMessage, host, port != null ? Integer.parseInt(port) : null, timestamp);
            results.add(logDocument);
        }

    } catch (SolrServerException | IOException e) {
        // TODO: any fine-grained control needed?
        throw new RuntimeException(e);
    }

    return new LogSearchResult(numFound, results);
}
 
Example 16
Source File: FirstQueryITCase.java    From apache-solr-essentials with Apache License 2.0 4 votes vote down vote up
/**
 * Sample and simple scenario where we index one document and make a query.
 * 
 * @throws Exception hopefully never otherwise the test fails.
 */
@Test
public void indexAndQuery() throws Exception {
	// This is the (input) Data Transfer Object between your client and SOLR.
	final SolrInputDocument input = new SolrInputDocument();
	
	// 1. Populates with (at least required) fields
	input.setField("id", 1);
	input.setField("title", "Apache SOLR Essentials");
	input.setField("author", "Andrea Gazzarini");
	input.setField("isbn", "972-2-5A619-12A-X");
	
	// 2. Adds the document
	client.add(input);
	
	// 3. Makes index changes visible
	client.commit();
	
	// 4. Builds a new query object with a "select all" query. 
	final SolrQuery query = new SolrQuery("*:*");
	
	// 5. Executes the query
	final QueryResponse response = client.query(query);
	
	assertEquals(1, response.getResults().getNumFound());
	
	// 6. Gets the (output) Data Transfer Object.
	final SolrDocument output = response.getResults().iterator().next();
	final String id = (String) output.getFieldValue("id");
	final String title = (String) output.getFieldValue("title");
	final String author = (String) output.getFieldValue("author");
	final String isbn = (String) output.getFieldValue("isbn");
	
	// 7.1 In case we are running as a Java application print out the query results.
	System.out.println("It works! I found the following book: ");
	System.out.println("--------------------------------------");
	System.out.println("ID: " + id);
	System.out.println("Title: " + title);
	System.out.println("Author: " + author);
	System.out.println("ISBN: " + isbn);
	
	// 7. Otherwise asserts the query results using standard JUnit procedures.
	assertEquals("1", id);
	assertEquals("Apache SOLR Essentials", title);
	assertEquals("Andrea Gazzarini", author);
	assertEquals("972-2-5A619-12A-X", isbn);
}
 
Example 17
Source File: AmbariInfraWithStormLogSearch.java    From streamline with Apache License 2.0 4 votes vote down vote up
@Override
public EventSearchResult searchEvent(EventSearchCriteria criteria) {
    SolrQuery query = new SolrQuery();

    String searchString = criteria.getSearchString();

    List<String> queryStrings = new ArrayList<>();
    addQueryStringToListOnlyIfAvailable(queryStrings, searchString, COLUMN_NAME_STREAMLINE_EVENT_KEYVALUES);
    addQueryStringToListOnlyIfAvailable(queryStrings, searchString, COLUMN_NAME_STREAMLINE_EVENT_HEADERS);
    addQueryStringToListOnlyIfAvailable(queryStrings, searchString, COLUMN_NAME_STREAMLINE_EVENT_AUX_KEYVALUES);

    // this is to get rid of non-streamline events
    String queryString = buildColumnAndValue(COLUMN_NAME_STREAMLINE_EVENT_ID, buildValue(null));

    if (!queryStrings.isEmpty()) {
        queryString += " AND (" + String.join(" OR ", queryStrings) + ")";
    }

    query.setQuery(queryString);

    query.addFilterQuery(buildColumnAndValue(COLUMN_NAME_TYPE, COLUMN_VALUE_TYPE_EVENT));
    query.addFilterQuery(buildColumnAndValue(COLUMN_NAME_STREAMLINE_TOPOLOGY_ID, buildValue(criteria.getAppId())));
    query.addFilterQuery(buildColumnAndValue(COLUMN_NAME_LOG_TIME, buildDateRangeValue(criteria.getFrom(), criteria.getTo())));

    List<String> componentNames = criteria.getComponentNames();
    if (componentNames != null && !componentNames.isEmpty()) {
        query.addFilterQuery(buildColumnAndValue(COLUMN_NAME_STREAMLINE_COMPONENT_NAME, buildORValues(componentNames)));
    }

    String searchEventId = criteria.getSearchEventId();
    if (searchEventId != null) {
        // eventId OR rootId OR parentId
        String queryToEventId = buildColumnAndValue(COLUMN_NAME_STREAMLINE_EVENT_ID, buildValue(searchEventId));
        String queryToRootIds = buildColumnAndValue(COLUMN_NAME_STREAMLINE_EVENT_ROOT_ID_SET, buildValue("*" + searchEventId + "*"));
        String queryToParentIds = buildColumnAndValue(COLUMN_NAME_STREAMLINE_EVENT_PARENT_ID_SET, buildValue("*" + searchEventId + "*"));
        query.addFilterQuery(queryToEventId + " OR " + queryToRootIds + " OR " + queryToParentIds);
    }

    if (criteria.getAscending() == null || criteria.getAscending()) {
        query.addSort(COLUMN_NAME_LOG_TIME, SolrQuery.ORDER.asc);
    } else {
        query.addSort(COLUMN_NAME_LOG_TIME, SolrQuery.ORDER.desc);
    }

    if (criteria.getStart() != null) {
        query.setStart(criteria.getStart());
    }
    if (criteria.getLimit() != null) {
        query.setRows(criteria.getLimit());
    }

    LOG.debug("Querying to Solr: query => {}", query);

    long numFound;
    List<EventSearchResult.Event> results = new ArrayList<>();
    try {
        QueryResponse response = solr.query(query);

        SolrDocumentList docList = response.getResults();
        numFound = docList.getNumFound();

        for (SolrDocument document : docList) {
            String appId = (String) document.getFieldValue(COLUMN_NAME_STREAMLINE_TOPOLOGY_ID);
            String componentName = (String) document.getFieldValue(COLUMN_NAME_STREAMLINE_COMPONENT_NAME);
            String eventId = (String) document.getFieldValue(COLUMN_NAME_STREAMLINE_EVENT_ID);
            String rootIds = (String) document.getFieldValue(COLUMN_NAME_STREAMLINE_EVENT_ROOT_ID_SET);
            String parentIds = (String) document.getFieldValue(COLUMN_NAME_STREAMLINE_EVENT_PARENT_ID_SET);
            String keyValues = (String) document.getFieldValue(COLUMN_NAME_STREAMLINE_EVENT_KEYVALUES);
            String headers = (String) document.getFieldValue(COLUMN_NAME_STREAMLINE_EVENT_HEADERS);
            String auxKeyValues = (String) document.getFieldValue(COLUMN_NAME_STREAMLINE_EVENT_AUX_KEYVALUES);

            Date logDate = (Date) document.getFieldValue(COLUMN_NAME_LOG_TIME);
            long timestamp = logDate.toInstant().toEpochMilli();

            EventSearchResult.Event event = new EventSearchResult.Event(appId, componentName,
                    eventId, rootIds, parentIds, keyValues, headers, auxKeyValues, timestamp);
            results.add(event);
        }

    } catch (SolrServerException | IOException e) {
        // TODO: any fine-grained control needed?
        throw new RuntimeException(e);
    }

    return new EventSearchResult(numFound, results);
}
 
Example 18
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public TrackerState getTrackerInitialState()
{
    try (SolrQueryRequest request = newSolrQueryRequest())
    {
        TrackerState state = new TrackerState();
        SolrRequestHandler handler = core.getRequestHandler(REQUEST_HANDLER_GET);

        ModifiableSolrParams newParams =
                new ModifiableSolrParams(request.getParams())
                        .set("ids", "TRACKER!STATE!ACLTX,TRACKER!STATE!TX");
        request.setParams(newParams);

        SolrDocumentList response = executeQueryRequest(request, newSolrQueryResponse(), handler);
        if (response == null)
        {
            LOGGER.error("Got no response from a tracker initial state request.");
            return state;
        }

        for (int i = 0; i < response.getNumFound(); i++)
        {
            SolrDocument current = response.get(i);
            // ACLTX
            if (current.getFieldValue(FIELD_S_ACLTXCOMMITTIME) != null)
            {
                if (state.getLastIndexedChangeSetCommitTime() == 0)
                {
                    state.setLastIndexedChangeSetCommitTime(getFieldValueLong(current, FIELD_S_ACLTXCOMMITTIME));
                }

                if (state.getLastIndexedChangeSetId() == 0)
                {
                    state.setLastIndexedChangeSetId(getFieldValueLong(current, FIELD_S_ACLTXID));
                }
            }

            // TX
            if (current.getFieldValue(FIELD_S_TXCOMMITTIME) != null)
            {
                if (state.getLastIndexedTxCommitTime() == 0)
                {
                    state.setLastIndexedTxCommitTime(getFieldValueLong(current, FIELD_S_TXCOMMITTIME));
                }

                if (state.getLastIndexedTxId() == 0)
                {
                    state.setLastIndexedTxId(getFieldValueLong(current, FIELD_S_TXID));
                }
            }
        }

        long startTime = System.currentTimeMillis();
        state.setLastStartTime(startTime);
        state.setTimeToStopIndexing(startTime - lag);
        state.setTimeBeforeWhichThereCanBeNoHoles(startTime - holeRetention);

        long timeBeforeWhichThereCanBeNoTxHolesInIndex = state.getLastIndexedTxCommitTime() - holeRetention;
        state.setLastGoodTxCommitTimeInIndex(timeBeforeWhichThereCanBeNoTxHolesInIndex > 0 ? timeBeforeWhichThereCanBeNoTxHolesInIndex : 0);

        long timeBeforeWhichThereCanBeNoChangeSetHolesInIndex = state.getLastIndexedChangeSetCommitTime() - holeRetention;
        state.setLastGoodChangeSetCommitTimeInIndex(timeBeforeWhichThereCanBeNoChangeSetHolesInIndex > 0 ? timeBeforeWhichThereCanBeNoChangeSetHolesInIndex : 0);

        return state;
    }
}
 
Example 19
Source File: TestSubQueryTransformer.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testJustJohnJavabin() throws Exception {
  final SolrQueryRequest johnTwoFL = req(johnAndNancyParams);
  ModifiableSolrParams params = new ModifiableSolrParams(johnTwoFL.getParams());
  params.set("q","name_s:john");
  params.set("wt","javabin");
  
  johnTwoFL.setParams(params);
  
  final NamedList<Object> unmarshalled;
  SolrCore core = johnTwoFL.getCore();
  SolrQueryResponse rsp = new SolrQueryResponse();
  SolrRequestInfo.setRequestInfo(new SolrRequestInfo(johnTwoFL, rsp));

  SolrQueryResponse response = h.queryAndResponse(
      johnTwoFL.getParams().get(CommonParams.QT), johnTwoFL);

  BinaryQueryResponseWriter responseWriter = (BinaryQueryResponseWriter) core.getQueryResponseWriter(johnTwoFL);
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  responseWriter.write(bytes, johnTwoFL, response);

  try (JavaBinCodec jbc = new JavaBinCodec()) {
    unmarshalled = (NamedList<Object>) jbc.unmarshal(
        new ByteArrayInputStream(bytes.toByteArray()));
  }

  johnTwoFL.close();
  SolrRequestInfo.clearRequestInfo();
  
  SolrDocumentList resultDocs = (SolrDocumentList)(unmarshalled.get("response"));
  
    Map<String,String> engText = new HashMap<>();
    engText.put("text_t", "These guys develop stuff");
    
    Map<String,String> engId = new HashMap<>();
    engId.put("text_t", "These guys develop stuff");
    engId.put("dept_id_s_dv", "Engineering");
    
    for (int docNum : new int []{0, peopleMultiplier-1}) {
      SolrDocument employeeDoc = resultDocs.get(docNum);
      assertEquals("john", employeeDoc.getFieldValue("name_s_dv"));
      for (String subResult : new String []{"depts", "depts_i"}) {

        SolrDocumentList subDoc = (SolrDocumentList)employeeDoc.getFieldValue(subResult);
        for (int deptNum : new int []{0, deptMultiplier-1}) {
          SolrDocument deptDoc = subDoc.get(deptNum);
          Object expectedDept = (subResult.equals("depts") ? engText : engId);
          assertTrue( "" + expectedDept + " equals to " + deptDoc,
              expectedDept.equals(deptDoc));
        }
    }
  }
}
 
Example 20
Source File: SolrDeleteDuplicates.java    From anthelion with Apache License 2.0 4 votes vote down vote up
public RecordReader<Text, SolrRecord> getRecordReader(final InputSplit split,
    final JobConf job, 
    Reporter reporter)
    throws IOException {

  SolrServer solr = SolrUtils.getCommonsHttpSolrServer(job);
  SolrInputSplit solrSplit = (SolrInputSplit) split;
  final int numDocs = solrSplit.getNumDocs();
  
  SolrQuery solrQuery = new SolrQuery(SOLR_GET_ALL_QUERY);
  solrQuery.setFields(SolrConstants.ID_FIELD, SolrConstants.BOOST_FIELD,
                      SolrConstants.TIMESTAMP_FIELD,
                      SolrConstants.DIGEST_FIELD);
  solrQuery.setStart(solrSplit.getDocBegin());
  solrQuery.setRows(numDocs);

  QueryResponse response;
  try {
    response = solr.query(solrQuery);
  } catch (final SolrServerException e) {
    throw new IOException(e);
  }

  final SolrDocumentList solrDocs = response.getResults();

  return new RecordReader<Text, SolrRecord>() {

    private int currentDoc = 0;

    public void close() throws IOException { }

    public Text createKey() {
      return new Text();
    }

    public SolrRecord createValue() {
      return new SolrRecord();
    }

    public long getPos() throws IOException {
      return currentDoc;
    }

    public float getProgress() throws IOException {
      return currentDoc / (float) numDocs;
    }

    public boolean next(Text key, SolrRecord value) throws IOException {
      if (currentDoc >= numDocs) {
        return false;
      }

      SolrDocument doc = solrDocs.get(currentDoc);
      String digest = (String) doc.getFieldValue(SolrConstants.DIGEST_FIELD);
      key.set(digest);
      value.readSolrDocument(doc);

      currentDoc++;
      return true;
    }    
  };
}