Java Code Examples for org.apache.solr.common.SolrDocument

The following examples show how to use org.apache.solr.common.SolrDocument. These examples are extracted from open source projects. 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 Project: lucene-solr   Source File: TestCloudPseudoReturnFields.java    License: Apache License 2.0 6 votes vote down vote up
public void testScoreAndExplicitRealFields() throws Exception {
  
  SolrDocumentList docs = null;
  SolrDocument doc = null;

  for (SolrParams p : Arrays.asList(params("q","*:*", "rows", "1", "fl","score,val_i"),
                                    params("q","*:*", "rows", "1", "fl","score", "fl","val_i"))) {
    docs = assertSearch(p);
    assertEquals(p + " => " + docs, 5, docs.getNumFound());
    doc = docs.get(0); // doesn't really matter which one
    assertEquals(p + " => " + doc, 2, doc.size());
    assertTrue(p + " => " + doc, doc.getFieldValue("val_i") instanceof Integer);
    assertTrue(p + " => " + doc, doc.getFieldValue("score") instanceof Float);
  }
  
  docs = assertSearch(params("q","*:*", "rows", "1", "fl","val_i"));
  assertEquals("" + docs, 5, docs.getNumFound());
  doc = docs.get(0); // doesn't really matter which one
  assertEquals("" + doc, 1, doc.size());
  assertTrue("" + doc, doc.getFieldValue("val_i") instanceof Integer);
}
 
Example 2
Source Project: parker   Source File: SolrTest.java    License: MIT License 6 votes vote down vote up
@Test
public void testQuery() throws Exception {
    SolrQuery query = new SolrQuery();

    // 根据标题或内容来搜索
    query.setQuery("title:项目 or content:项目");

    // 返回的字段
    query.addField("id");
    query.addField("title");
    query.addField("content");

    QueryResponse response = client.query(query);
    SolrDocumentList documents = response.getResults();
    System.out.println("结果集大小=" + documents.size());
    for(SolrDocument document : documents) {
        final String title = (String) document.getFirstValue("title");
        System.out.println(title);
        //final String name = (String) document.getFirstValue("name");
        //System.out.println("id: " + id + "; name: " + name);
    }
}
 
Example 3
Source Project: mycore   Source File: MCRSolrSearchUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean tryAdvance(Consumer<? super SolrDocument> action) {
    if (action == null) {
        throw new NullPointerException();
    }
    long i = start, size = estimateSize();
    if (size > 0) {
        if (response == null) {
            ModifiableSolrParams p = new ModifiableSolrParams(params);
            p.set("start", (int) i);
            p.set("rows", (int) rows);
            response = query(p);
        }
        action.accept(response.getResults().get(response.getResults().size() - (int) size));
        this.start = i + 1;
        this.size -= 1;
        return true;
    }
    return false;
}
 
Example 4
Source Project: lucene-solr   Source File: TestCloudPseudoReturnFields.java    License: Apache License 2.0 6 votes vote down vote up
public void testFunctionsAndScoreRTG() throws Exception {

    // if we use RTG (committed or otherwise) score should be ignored
    for (String id : Arrays.asList("42","99")) {
      for (SolrParams p : Arrays.asList(params("fl","score","fl","log(val_i)","fl","abs(val_i)"),
                                        params("fl","score","fl","log(val_i),abs(val_i)"),
                                        params("fl","score,log(val_i)","fl","abs(val_i)"),
                                        params("fl","score,log(val_i),abs(val_i)"))) {
        SolrDocument doc = getRandClient(random()).getById(id, p);
        String msg = id + "," + p + " => " + doc;
        assertEquals(msg, 2, doc.size());
        assertTrue(msg, doc.getFieldValue("log(val_i)") instanceof Double);
        assertTrue(msg, doc.getFieldValue("abs(val_i)") instanceof Float);
        // true for both these specific docs
        assertEquals(msg, 0.0D, doc.getFieldValue("log(val_i)"));
        assertEquals(msg, 1.0F, doc.getFieldValue("abs(val_i)"));
      }
    }
  }
 
Example 5
Source Project: extract   Source File: MergingSolrSpewer.java    License: MIT License 6 votes vote down vote up
private void mergeField(final String name, final String newValue, final SolrDocument existingDocument, final
SolrInputDocument inputDocument) {

	// Even though the superclass sets the path and parent path fields, we should set them again in case there's
	// a retry and they need to be overwritten.
	if (null == existingDocument) {
		setFieldValue(inputDocument, name, newValue);
		return;
	}

	// Create a HashSet from existing values so that only non-existing (distinct) values are added.
	// A HashSet gives constant time performance, as opposed to a loop, which is important when dealing with
	// potentially thousands of values.
	final Collection<Object> existingValues = existingDocument.getFieldValues(name);
	if (null != existingValues) {
		final Set<String> values = existingValues.stream()
				.map(String::valueOf)
				.collect(Collectors.toCollection(HashSet::new));

		values.add(newValue);
		setFieldValue(inputDocument, name, values.toArray(new String[values.size()]));
	} else {
		setFieldValue(inputDocument, name, newValue);
	}
}
 
Example 6
Source Project: BioSolr   Source File: TestMerge.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * When [shard] is not in the field list, the [shard] field should not be returned, even
 * if score is in the field list.
 */
@Test
public void testNotWantsShardWithScore() throws Exception {
  try (SolrCore core = h.getCoreContainer().getCore("merge")) {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add("q", "*:*");
    params.add("sort", "letter asc");
    params.add("fl", "*,score");

    SolrDocumentList docs = queryDocs(core, "merge", params);
    assertEquals(3, docs.size());
    
    for (SolrDocument doc : docs) {
      assertNull(doc.getFieldValue("[shard]"));
      assertEquals(1.0f, doc.getFieldValue("score"));
    }
  }   
}
 
Example 7
Source Project: lucene-solr   Source File: GetByIdTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGetIdWithParams() throws Exception {
  final SolrParams ID_FL_ONLY = params(CommonParams.FL, "id");

  SolrDocument rsp = getSolrClient().getById("0", ID_FL_ONLY);
  assertNull(rsp);

  rsp = getSolrClient().getById("1", ID_FL_ONLY);
  assertEquals("1", rsp.get("id"));
  assertNull("This field should have been removed from the response.", rsp.get("term_s"));
  assertNull("This field should have been removed from the response.", rsp.get("term2_s"));

  rsp = getSolrClient().getById("2", ID_FL_ONLY);
  assertEquals("2", rsp.get("id"));
  assertNull("This field should have been removed from the response.", rsp.get("term_s"));
  assertNull("This field should have been removed from the response.", rsp.get("term2_s"));
}
 
Example 8
Source Project: vind   Source File: SolrUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static SolrInputDocument toSolrInputDocument(SolrDocument solrDocument) {
    SolrInputDocument solrInputDocument = new SolrInputDocument();

    for (String name : solrDocument.getFieldNames()) {
        solrInputDocument.addField(name, solrDocument.getFieldValue(name));
    }

    //Don't forget children documents
    if(solrDocument.getChildDocuments() != null) {
        for(SolrDocument childDocument : solrDocument.getChildDocuments()) {
            //You can add paranoic check against infinite loop childDocument == solrDocument
            solrInputDocument.addChildDocument(toSolrInputDocument(childDocument));
        }
    }
    return solrInputDocument;
}
 
Example 9
Source Project: lucene-solr   Source File: TestCloudPseudoReturnFields.java    License: Apache License 2.0 6 votes vote down vote up
public void testFunctionsAndExplicitRTG() throws Exception {
  // shouldn't matter if we use RTG (committed or otherwise)
  for (String id : Arrays.asList("42","99")) {
    for (SolrParams p : Arrays.asList(params("fl","log(val_i),val_i"),
                                      params("fl","log(val_i)","fl","val_i"))) {
      SolrDocument doc = getRandClient(random()).getById(id, p);
      String msg = id + "," + p + " => " + doc;
      assertEquals(msg, 2, doc.size());
      assertTrue(msg, doc.getFieldValue("log(val_i)") instanceof Double);
      assertTrue(msg, doc.getFieldValue("val_i") instanceof Integer);
      // true for both these specific docs
      assertEquals(msg, 0.0D, doc.getFieldValue("log(val_i)"));
      assertEquals(msg, 1, doc.getFieldValue("val_i"));
    }
  }
}
 
Example 10
Source Project: thoth   Source File: DocumentShrinker.java    License: BSD 3-Clause Clear License 6 votes vote down vote up
/**
 * Tag slower documents and add them to the shrank core
 */
private void tagAndAddSlowThothDocuments() throws IOException, SolrServerException {
  // Query to return top MAX_NUMBER_SLOW_THOTH_DOCS slower thoth documents
  QueryResponse qr = realTimeServer.query(
      new SolrQuery()
          .setQuery(createThothDocsAggregationQuery())
          .addSort(QTIME, SolrQuery.ORDER.desc)
          .setRows(MAX_NUMBER_SLOW_THOTH_DOCS)
  );

  for (SolrDocument solrDocument: qr.getResults()){
    SolrInputDocument si = ClientUtils.toSolrInputDocument(solrDocument);
    // Remove old ID and version
    si.removeField(ID);
    si.removeField("_version_");
    // Tag document as slow
    si.addField(SLOW_QUERY_DOCUMENT, true);
    LOG.debug("Adding slow query document for server " + serverDetail.getName());
    shrankServer.add(si);
  }
}
 
Example 11
Source Project: SolRDF   Source File: DeepPagingIterator.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Triple next() {
	final SolrDocument document = iterator().next();
	
	Triple triple = null;
	if (consumer.requireTripleBuild()) { 
		triple = Triple.create(
				NTriples.asURIorBlankNode((String) document.getFieldValue(Field.S)), 
				NTriples.asURI((String) document.getFieldValue(Field.P)),
				NTriples.asNode((String) document.getFieldValue(Field.O)));
	} else {
		triple = DUMMY_TRIPLE;
	}
	// FIXME
	// consumer.afterTripleHasBeenBuilt(triple, nextDocId);
	return triple;
}
 
Example 12
Source Project: lucene-solr   Source File: TestCloudPseudoReturnFields.java    License: Apache License 2.0 6 votes vote down vote up
public void testAugmentersAndExplicit() throws Exception {
  for (SolrParams p : Arrays.asList(params("q", "*:*", "fl","id,[docid],[explain],x_alias:[value v=10 t=int]"),
                                    params("q", "*:*", "fl","id","fl","[docid],[explain],x_alias:[value v=10 t=int]"),
                                    params("q", "*:*", "fl","id","fl","[docid]","fl","[explain]","fl","x_alias:[value v=10 t=int]"))) {
    SolrDocumentList docs = assertSearch(p);
    assertEquals(p + " => " + docs, 5, docs.getNumFound());
    // shouldn't matter what doc we pick...
    for (SolrDocument doc : docs) {
      String msg = p + " => " + doc;
      assertEquals(msg, 4, doc.size());
      assertTrue(msg, doc.getFieldValue("id") instanceof String);
      assertTrue(msg, doc.getFieldValue("[docid]") instanceof Integer);
      assertTrue(msg, doc.getFieldValue("[explain]") instanceof String);
      assertTrue(msg, doc.getFieldValue("x_alias") instanceof Integer);
      assertEquals(msg, 10, doc.getFieldValue("x_alias"));
    }
  }
}
 
Example 13
Source Project: lucene-solr   Source File: TestCloudPseudoReturnFields.java    License: Apache License 2.0 6 votes vote down vote up
public void testFunctions() throws Exception {

    SolrDocumentList docs = assertSearch(params("q","*:*","rows","1","fl","log(val_i)"));
    assertEquals(""+docs, 5, docs.getNumFound());
    SolrDocument doc = docs.get(0); // doesn't really matter which one
    assertEquals(""+doc, 1, doc.size());
    assertTrue(""+doc, doc.getFieldValue("log(val_i)") instanceof Double);
    
    for (SolrParams p : Arrays.asList(params("q","*:*", "rows", "1", "fl","log(val_i),abs(val_i)"),
                                      params("q","*:*", "rows", "1", "fl","log(val_i)", "fl","abs(val_i)"))) {
      docs = assertSearch(p);
      assertEquals(p + " => " + docs, 5, docs.getNumFound());
      doc = docs.get(0); // doesn't really matter which one
      assertEquals(p + " => " + doc, 2, doc.size());
      assertTrue(p + " => " + doc, doc.getFieldValue("log(val_i)") instanceof Double);
      assertTrue(p + " => " + doc, doc.getFieldValue("abs(val_i)") instanceof Float);
    }
  }
 
Example 14
Source Project: lucene-solr   Source File: UpdateLogTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyPartialUpdatesAfterMultipleCommits() {    
  ulogAdd(ulog, null, sdoc("id", "1", "title_s", "title1", "val1_i_dvo", "1", "_version_", "100"));
  ulogAdd(ulog, 100L, sdoc("id", "1", "price", "1000", "val1_i_dvo", "2", "_version_", "101"));
  ulogAdd(ulog, 101L, sdoc("id", "1", "val1_i_dvo", "3", "_version_", "102"));

  // Do 3 commits, then in-place update, and verify that applying partial updates can't find full doc
  for (int i=0; i<3; i++)
    ulogCommit(ulog);
  ulogAdd(ulog, 101L, sdoc("id", "1", "val1_i_dvo", "6", "_version_", "300"));

  Object partialUpdate = ulog.lookup(DOC_1_INDEXED_ID);
  SolrDocument partialDoc = RealTimeGetComponent.toSolrDoc((SolrInputDocument)((List)partialUpdate).get(4), h.getCore().getLatestSchema());
  long prevVersion = (Long)((List)partialUpdate).get(3);
  long prevPointer = (Long)((List)partialUpdate).get(2);

  assertEquals(6L, ((NumericDocValuesField)partialDoc.getFieldValue("val1_i_dvo")).numericValue());
  assertFalse(partialDoc.containsKey("title_s"));

  long returnVal = ulog.applyPartialUpdates(DOC_1_INDEXED_ID, prevPointer, prevVersion, null, partialDoc);

  assertEquals(-1, returnVal);
}
 
Example 15
Source Project: lucene-solr   Source File: TestRandomFlRTGCloud.java    License: Apache License 2.0 6 votes vote down vote up
public Collection<String> assertRTGResults(final Collection<FlValidator> validators,
                                           final SolrInputDocument expected,
                                           final SolrDocument actual) {
  final int compVal = assertParseInt("expected id", expected.getFieldValue("id"));
  
  final Object actualVal = actual.getFieldValue(SUBQ_KEY);
  assertTrue("Expected a doclist: " + actualVal,
             actualVal instanceof SolrDocumentList);
  assertTrue("should be at most 2 docs in doc list: " + actualVal,
             ((SolrDocumentList) actualVal).getNumFound() <= 2);
  
  for (SolrDocument subDoc : (SolrDocumentList) actualVal) {
    final int subDocIdVal = assertParseInt("subquery id", subDoc.getFirstValue("id"));
    assertTrue("subDocId="+subDocIdVal+" not in valid range for id="+compVal+" (expected "
               + (compVal-1) + " or " + (compVal-2) + ")",
               ((subDocIdVal < compVal) && ((compVal-2) <= subDocIdVal)));
    
  }

  return Collections.<String>singleton(SUBQ_KEY);
}
 
Example 16
Source Project: lucene-solr   Source File: SmileWriterTest.java    License: Apache License 2.0 6 votes vote down vote up
public static SolrDocument sampleDoc(Random r, int bufnum) {
  SolrDocument sdoc = new SolrDocument();
  sdoc.put("id", "my_id_" + bufnum);
  sdoc.put("author", str(r, 10 + r.nextInt(10)));
  sdoc.put("address", str(r, 20 + r.nextInt(20)));
  sdoc.put("license", str(r, 10));
  sdoc.put("title", str(r, 5 + r.nextInt(10)));
  sdoc.put("title_bin", str(r, 5 + r.nextInt(10)).getBytes(StandardCharsets.UTF_8));
  sdoc.put("modified_dt", r.nextInt(1000000));
  sdoc.put("creation_dt", r.nextInt(1000000));
  sdoc.put("birthdate_dt", r.nextInt(1000000));
  sdoc.put("clean", r.nextBoolean());
  sdoc.put("dirty", r.nextBoolean());
  sdoc.put("employed", r.nextBoolean());
  sdoc.put("priority", r.nextInt(100));
  sdoc.put("dependents", r.nextInt(6));
  sdoc.put("level", r.nextInt(101));
  sdoc.put("education_level", r.nextInt(10));
  // higher level of reuse for string values
  sdoc.put("state", "S"+r.nextInt(50));
  sdoc.put("country", "Country"+r.nextInt(20));
  sdoc.put("some_boolean", ""+r.nextBoolean());
  sdoc.put("another_boolean", ""+r.nextBoolean());
  return sdoc;
}
 
Example 17
Source Project: lucene-solr   Source File: BaseEditorialTransformer.java    License: Apache License 2.0 6 votes vote down vote up
protected BytesRef getKey(SolrDocument doc) {
  Object obj = doc.get(idFieldName);
  if (obj instanceof IndexableField) {
    IndexableField f = (IndexableField) obj;
    BytesRefBuilder bytesRefBuilder = new BytesRefBuilder();
    Number n = f.numericValue();
    if (n != null) {
      ft.readableToIndexed(n.toString(), bytesRefBuilder);
    } else {
      ft.readableToIndexed(f.stringValue(), bytesRefBuilder);
    }
    return bytesRefBuilder.get();
  } else if (obj instanceof String) { // Allows the idField to be stored=false, docValues=true
    return new BytesRef(((String)obj));
  }
  throw new AssertionError("Expected an IndexableField but got: " + obj.getClass());
}
 
Example 18
Source Project: lucene-solr   Source File: UpdateLogTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyPartialUpdatesDependingOnNonAddShouldThrowException() {
  ulogAdd(ulog, null, sdoc("id", "1", "title_s", "title1", "val1_i_dvo", "1", "_version_", "100"));
  ulogDelete(ulog, "1", 500L, false); // dbi
  ulogAdd(ulog, 500L, sdoc("id", "1", "val1_i_dvo", "2", "_version_", "501"));
  ulogAdd(ulog, 501L, sdoc("id", "1", "val1_i_dvo", "3", "_version_", "502"));

  Object partialUpdate = ulog.lookup(DOC_1_INDEXED_ID);
  SolrDocument partialDoc = RealTimeGetComponent.toSolrDoc((SolrInputDocument)((List)partialUpdate).get(4), h.getCore().getLatestSchema());
  long prevVersion = (Long)((List)partialUpdate).get(3);
  long prevPointer = (Long)((List)partialUpdate).get(2);

  assertEquals(3L, ((NumericDocValuesField)partialDoc.getFieldValue("val1_i_dvo")).numericValue());
  assertEquals(502L, ((NumericDocValuesField)partialDoc.getFieldValue("_version_")).numericValue());
  assertFalse(partialDoc.containsKey("title_s"));

  // If an in-place update depends on a non-add (i.e. DBI), assert that an exception is thrown.
  SolrException ex = expectThrows(SolrException.class, () -> {
      long returnVal = ulog.applyPartialUpdates(DOC_1_INDEXED_ID, prevPointer, prevVersion, null, partialDoc);
      fail("502 depends on 501, 501 depends on 500, but 500 is a"
           + " DELETE. This should've generated an exception. returnVal is: "+returnVal);
    });
  assertEquals(ex.toString(), SolrException.ErrorCode.INVALID_STATE.code, ex.code());
  assertThat(ex.getMessage(), containsString("should've been either ADD or UPDATE_INPLACE"));
  assertThat(ex.getMessage(), containsString("looking for id=1"));
}
 
Example 19
Source Project: FXDesktopSearch   Source File: LuceneIndexHandler.java    License: Apache License 2.0 6 votes vote down vote up
private String getOrDefault(SolrDocument document, String aFieldname, String aDefault) {
    Object theValue = document.get(aFieldname);
    if (theValue == null) {
        return aDefault;
    }
    if (theValue instanceof String) {
        return (String) theValue;
    }
    if (theValue instanceof List) {
        List theList = (List) theValue;
        if (theList.isEmpty()) {
            return aDefault;
        }
        Object theFirst = theList.get(0);
        if (theFirst instanceof String) {
            return (String) theFirst;
        }
        return aDefault;
    }
    return aDefault;
}
 
Example 20
public void doTestAssertTagStreaming(BiFunction<ModifiableSolrParams,String,QueryRequest> newQueryRequest) throws IOException, SolrServerException {
  ModifiableSolrParams params = params();
  String input = "foo boston bar";//just one tag;
  QueryRequest req = newQueryRequest.apply(params, input);
  req.setPath("/tag");

  final AtomicReference<SolrDocument> refDoc = new AtomicReference<>();
  req.setStreamingResponseCallback(new StreamingResponseCallback() {
    @Override
    public void streamSolrDocument(SolrDocument doc) {
      refDoc.set(doc);
    }

    @Override
    public void streamDocListInfo(long numFound, long start, Float maxScore) {

    }
  });
  QueryResponse rsp = req.process(solrServer);
  assertNotNull(rsp.getResponse().get("tags"));
  assertNotNull(refDoc.get());
  assertEquals("Boston", ((Field)refDoc.get().getFieldValue("name")).stringValue());
}
 
Example 21
@Test
public void fieldsRequestsShouldTurnIntoSelectors() throws Exception,
    TException {

  String table = "fieldsRequestsShouldTurnIntoSelectors";
  SolrServer server = TestTableCreator.newTable(table)
      .withRowCount(1).withRecordsPerRow(2)
      .withRecordColumns("fam.value", "fam.mvf").create();

  SolrQuery query = new SolrQuery("value0-0");
  query.setFields("fam.value");
  QueryResponse response = server.query(query);

  assertEquals("We should get our doc back for a valid test.", 1l, response.getResults().getNumFound());

  SolrDocument docResult = response.getResults().get(0);

  assertEquals("value0-0", docResult.getFieldValue("fam.value"));
  assertNull("We shouldn't get this one back since it wasnt in our fields.", docResult.getFieldValues("fam.mvf"));

  removeTable(table);
}
 
Example 22
Source Project: lucene-solr   Source File: UnifiedSolrHighlighter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the unique keys for the topdocs to key the results
 */
protected String[] getUniqueKeys(SolrIndexSearcher searcher, int[] docIDs) throws IOException {
  IndexSchema schema = searcher.getSchema();
  SchemaField keyField = schema.getUniqueKeyField();
  if (keyField != null) {
    SolrReturnFields returnFields = new SolrReturnFields(keyField.getName(), null);
    String[] uniqueKeys = new String[docIDs.length];
    for (int i = 0; i < docIDs.length; i++) {
      int docid = docIDs[i];
      SolrDocument solrDoc = searcher.getDocFetcher().solrDoc(docid, returnFields);
      uniqueKeys[i] = schema.printableUniqueKey(solrDoc);
    }
    return uniqueKeys;
  } else {
    return new String[docIDs.length];
  }
}
 
Example 23
Source Project: tutorials   Source File: SolrJavaLiveTest.java    License: MIT License 6 votes vote down vote up
@Test
public void whenAdd_thenVerifyAddedByQueryOnPrice() throws SolrServerException, IOException {

    SolrQuery query = new SolrQuery();
    query.set("q", "price:599.99");
    QueryResponse response = null;

    response = solrJavaIntegration.getSolrClient().query(query);

    SolrDocumentList docList = response.getResults();
    assertEquals(1, docList.getNumFound());

    for (SolrDocument doc : docList) {
        assertEquals("123456", (String) doc.getFieldValue("id"));
        assertEquals((Double) 599.99, (Double) doc.getFieldValue("price"));
    }
}
 
Example 24
Source Project: lucene-solr   Source File: TestCloudPseudoReturnFields.java    License: Apache License 2.0 6 votes vote down vote up
public void testGlobsAndScoreRTG() throws Exception {
  // behavior shouldn't matter if we are committed or uncommitted, score should be ignored
  for (String id : Arrays.asList("42","99")) {
    SolrDocument doc = getRandClient(random()).getById(id, params("fl","val_*,score"));
    String msg = id + ": fl=val_*,score => " + doc;
    assertEquals(msg, 1, doc.size());
    assertTrue(msg, doc.getFieldValue("val_i") instanceof Integer);
    assertEquals(msg, 1, doc.getFieldValue("val_i"));

    for (SolrParams p : Arrays.asList(params("fl","val_*,subj*,score"),
                                      params("fl","val_*","fl","subj*","fl","score"),
                                      params("fl","val_*","fl","subj*,score"))) {
      doc = getRandClient(random()).getById(id, p);
      msg = id + ": " + p + " => " + doc;
      assertEquals(msg, 2, doc.size());
      assertTrue(msg, doc.getFieldValue("val_i") instanceof Integer);
      assertEquals(msg, 1, doc.getFieldValue("val_i"));
      assertTrue(msg, doc.getFieldValue("subject") instanceof String); 
    }
  }
}
 
Example 25
Source Project: mycore   Source File: MCROAISolrSearcher.java    License: GNU General Public License v3.0 6 votes vote down vote up
protected MCROAISolrResult solrQuery(Optional<String> cursor) throws SolrServerException, IOException {
    SolrQuery query = getBaseQuery(CommonParams.Q);

    // set support
    if (this.set != null) {
        String setId = this.set.getSetId();
        MCROAISetConfiguration<SolrQuery, SolrDocument, String> setConfig = getSetManager().getConfig(setId);
        setConfig.getHandler().apply(this.set, query);
    }
    // from & until
    if (this.from != null || this.until != null) {
        String fromUntilCondition = buildFromUntilCondition(this.from, this.until);
        query.add(CommonParams.FQ, fromUntilCondition);
    }

    // cursor
    query.set(CursorMarkParams.CURSOR_MARK_PARAM, cursor.orElse(CursorMarkParams.CURSOR_MARK_START));
    query.set(CommonParams.ROWS, String.valueOf(getPartitionSize()));
    query.set(CommonParams.SORT, "id asc");

    // do the query
    SolrClient solrClient = MCRSolrClientFactory.getMainSolrClient();
    QueryResponse response = solrClient.query(query);
    Collection<MCROAISetResolver<String, SolrDocument>> setResolver = getSetResolver(response.getResults());
    return new MCROAISolrResult(response, d -> toHeader(d, setResolver));
}
 
Example 26
Source Project: lucene-solr   Source File: TestCloudPseudoReturnFields.java    License: Apache License 2.0 5 votes vote down vote up
public void testFilterAndOneRealFieldRTG() throws Exception {
  SolrParams params = params("fl","id,val_i",
                             "fq","{!field f='subject' v=$my_var}",
                             "my_var","uncommitted");
  SolrDocumentList docs = getRandClient(random()).getById(Arrays.asList("42","99"), params);
  final String msg = params + " => " + docs;
  assertEquals(msg, 1, docs.size());
  assertEquals(msg, 1, docs.getNumFound());
  
  SolrDocument doc = docs.get(0);
  assertEquals(msg, 2, doc.size());
  assertEquals(msg, "99", doc.getFieldValue("id"));
  assertEquals(msg, 1, doc.getFieldValue("val_i"));
}
 
Example 27
Source Project: lucene-solr   Source File: BaseEditorialTransformer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void transform(SolrDocument doc, int docid) {
  //this only gets added if QueryElevationParams.MARK_EXCLUDED is true
  Set<BytesRef> ids = getIdSet();
  if (ids != null && ids.isEmpty() == false) {
    BytesRef key = getKey(doc);
    doc.setField(name, ids.contains(key));
  } else {
    //if we have no ids, that means we weren't marking, but the user still asked for the field to be added, so just mark everything as false
    doc.setField(name, Boolean.FALSE);
  }
}
 
Example 28
/**
 * Verify that given SolrServer contains the expected SolrDocuments.
 */
private static void verifySolrDocuments(SolrClient solrServer, Collection<SolrDocument> expectedDocuments)
        throws IOException, SolrServerException {

    solrServer.commit();

    SolrQuery query = new SolrQuery("*:*");
    QueryResponse qResponse = solrServer.query(query);
    Assert.assertEquals(expectedDocuments.size(), qResponse.getResults().getNumFound());

    // verify documents have expected fields and values
    for (SolrDocument expectedDoc : expectedDocuments) {
        boolean found = false;
        for (SolrDocument solrDocument : qResponse.getResults()) {
            boolean foundAllFields = true;
            for (String expectedField : expectedDoc.getFieldNames()) {
                Object expectedVal = expectedDoc.getFirstValue(expectedField);
                Object actualVal = solrDocument.getFirstValue(expectedField);
                foundAllFields = expectedVal.equals(actualVal);
            }

            if (foundAllFields) {
                found = true;
                break;
            }
        }
        Assert.assertTrue("Could not find " + expectedDoc, found);
    }
}
 
Example 29
Source Project: lucene-solr   Source File: JavaBinCodec.java    License: Apache License 2.0 5 votes vote down vote up
public void writeSolrDocument(SolrDocument doc) throws IOException {
  List<SolrDocument> children = doc.getChildDocuments();
  int fieldsCount = 0;
  if(writableDocFields == null || writableDocFields.wantsAllFields() || ignoreWritable){
    fieldsCount = doc.size();
  } else {
    for (Entry<String, Object> e : doc) {
      if(toWrite(e.getKey())) fieldsCount++;
    }
  }
  int sz = fieldsCount + (children==null ? 0 : children.size());
  writeTag(SOLRDOC);
  writeTag(ORDERED_MAP, sz);
  if (cew == null) cew = new ConditionalKeyMapWriter.EntryWriterWrapper(ew, (k) -> toWrite(k.toString()));
  doc.writeMap(cew);
  if (children != null) {
    try {
      ignoreWritable = true;
      for (SolrDocument child : children) {
        writeSolrDocument(child);
      }
    } finally {
      ignoreWritable = false;
    }
  }

}
 
Example 30
Source Project: BioSolr   Source File: TestDJoin.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Test that (a) sorting works correctly, so that we get docs 1 and 2 returned (sort values A and B)
 *       and (b) we still get all parts of doc 1 returned even though it ranks third on shard/3
 */
@Test
public void testJoin() throws Exception {
  try (SolrCore core = h.getCoreContainer().getCore("djoin")) {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add("q", "*:*");
    params.add("rows", "2");
    params.add("sort", "letter asc");
    params.add("fl", "*,[shard]");

    SolrQueryResponse rsp = query(core, "djoin", params);
    assertNull(rsp.getException());
    SolrDocumentList docs = (SolrDocumentList)rsp.getValues().get("response");
    assertEquals(2, docs.size());
    
    assertEquals(true, docs.get(0).get(DuplicateDocumentList.MERGE_PARENT_FIELD));
    assertEquals(3, docs.get(0).getChildDocumentCount());
    assertEquals("1", docs.get(0).getChildDocuments().get(0).get("id"));
    assertEquals("1", docs.get(0).getChildDocuments().get(1).get("id"));
    assertEquals("1", docs.get(0).getChildDocuments().get(2).get("id"));
    Set<String> letters = new HashSet<>();
    for (SolrDocument doc : docs.get(0).getChildDocuments()) {
      letters.add((String)doc.get("letter"));
    }
    assertEquals(new HashSet<String>(Arrays.asList("A", "D", "E")), letters);
    
    assertEquals(true, docs.get(1).get(DuplicateDocumentList.MERGE_PARENT_FIELD));
    assertEquals(2, docs.get(1).getChildDocumentCount());
    assertEquals("2", docs.get(1).getChildDocuments().get(0).get("id"));
    assertEquals("B", docs.get(1).getChildDocuments().get(0).get("letter"));
    assertEquals("2", docs.get(1).getChildDocuments().get(1).get("id"));
    assertEquals("B", docs.get(1).getChildDocuments().get(1).get("letter"));
  }
}