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

The following examples show how to use org.apache.solr.common.SolrDocument#get() . 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: Hits.java    From document-management-software with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static Hit toHit(SolrDocument sdoc) {
	Hit hit = new Hit();

	hit.setId(Long.parseLong((String) sdoc.get(HitField.ID.getName())));
	if (sdoc.get(HitField.TENANT_ID.getName()) != null) {
		hit.setTenantId((Long) sdoc.getFieldValue(HitField.TENANT_ID.getName()));
	} else
		hit.setTenantId(Tenant.DEFAULT_ID);

	if (sdoc.getFieldValue("score") != null) {
		Float score = (Float) sdoc.getFieldValue("score");
		hit.setScore((int) (score * 100));
	}

	if (sdoc.get(HitField.FOLDER_ID.getName()) != null) {
		Folder folder = new Folder();
		folder.setId((Long) sdoc.get(HitField.FOLDER_ID.getName()));
		hit.setFolder(folder);
	}

	if (sdoc.getFieldValue(HitField.LANGUAGE.getName()) != null) {
		hit.setLanguage(sdoc.getFieldValue(HitField.LANGUAGE.getName()).toString());
	}

	return hit;
}
 
Example 2
Source File: LuceneIndexHandler.java    From FXDesktopSearch with 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 3
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Fails if the number of documents in the given SolrDocumentList differs
 * from the given number of expected values, or if any of the values in the
 * given field don't match the expected values in the same order.
 */
public static void assertFieldValues(SolrDocumentList documents, String fieldName, Object... expectedValues) {
  if (documents.size() != expectedValues.length) {
    fail("Number of documents (" + documents.size()
        + ") is different from number of expected values (" + expectedValues.length);
  }
  for (int docNum = 1 ; docNum <= documents.size() ; ++docNum) {
    SolrDocument doc = documents.get(docNum - 1);
    Object expected = expectedValues[docNum - 1];
    Object actual = doc.get(fieldName);
    if ((null == expected && null != actual) ||
        (null != expected && null == actual) ||
        (null != expected && null != actual && !expected.equals(actual))) {
      fail("Unexpected " + fieldName + " field value in document #" + docNum
          + ": expected=[" + expected + "], actual=[" + actual + "]");
    }
  }
}
 
Example 4
Source File: AlfrescoFieldMapperTransformerIT.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void transformDocument_docTransformer_shouldReturnAllFields() throws Exception
{
    putHandleDefaults();
    
    //Test 2: Running simple query with AlfrescoFieldMapperTransformer, expected to see all fields returned
    QueryResponse resp = query(getDefaultTestClient(), true, ALFRESCO_JSON, params("q", "*", "qt", "/afts", "shards.qt", "/afts","fl","*,[fmap]", "sort", "id asc"));
    SolrDocument docWithAllFields = resp.getResults().get(0);
    assertTrue(docWithAllFields.size() > 3);

    Long version2 = (Long) docWithAllFields.get("_version_");
    assertNotNull(version2);
    String owner = docWithAllFields.get("OWNER").toString();
    assertNotNull(owner);
    assertEquals("[mike]", owner);
    String title = docWithAllFields.get("cm:title").toString();
    assertEquals("[title1]", title);
    assertNotNull(title);
    Long dbid2 = (Long) docWithAllFields.get("DBID");
    assertNotNull(dbid2);
}
 
Example 5
Source File: AlfrescoFieldMapperTransformerIT.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void transformDocument_noDocTransformer_shouldReturnBasicFields() throws Exception 
{
    putHandleDefaults();
    //Test 1: Running a simple query without invoking AlfrescoFieldMapperTransformer, expected to see id,DBID and _version_
    QueryResponse resp = query(getDefaultTestClient(), true, ALFRESCO_JSON, params("q", "*", "qt", "/afts", "shards.qt", "/afts", "sort", "id asc"));
    assertNotNull(resp);
    SolrDocumentList results = resp.getResults();
    assertEquals("Expecting 5 rows",5, results.size());
    SolrDocument doc = results.get(0);
    assertEquals(3, doc.size());
    assertNotNull(doc);
    String id = (String) doc.get("id");
    assertNotNull(id);
    Long version = (Long) doc.get("_version_");
    assertNotNull(version);
    Long dbid = (Long) doc.get("DBID");
    assertNotNull(dbid);
    //Not expected to see below as part of the solr response.
    String title = (String) doc.get("cm:title");
    assertNull(title);
    String owner = (String) doc.get("OWNER");
    assertNull(owner);
}
 
Example 6
Source File: AlfrescoSearchHandler.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
public final SolrDocument toSolrDocument(Document doc, IndexSchema schema) {
	SolrDocument out = new SolrDocument();
	for (IndexableField f : doc) {
		// Make sure multivalued fields are represented as lists
		Object existing = out.get(f.name());
		if (existing == null) {
			SchemaField sf = schema.getFieldOrNull(f.name());
			if (sf != null && sf.multiValued()) {
				List<Object> vals = new ArrayList<>();
				vals.add(f);
				out.setField(f.name(), vals);
			} else {
				out.setField(f.name(), f);
			}
		} else {
			out.addField(f.name(), f);
		}
	}
	return out;
}
 
Example 7
Source File: SolrConnector.java    From TagRec with GNU Affero General Public License v3.0 6 votes vote down vote up
public String getTweetTextOfRecentTweets(String user, int intValue) {
	String tweetText = "";
	SolrQuery solrParams = new SolrQuery();
	solrParams.set("q", "userid:" + user);
	solrParams.set("sort", "timestamp desc");
	solrParams.set("fl", "text");
	solrParams.set("rows", intValue);
	QueryResponse r = null;
	try {
		r = this.server.query(solrParams);
		SolrDocumentList docs = r.getResults();
		for (SolrDocument d : docs) {
			tweetText += ((String) d.get("text") + " ");
		}
	} catch (Exception e) {
		e.printStackTrace();
	}		
	return tweetText;
}
 
Example 8
Source File: SecureRealTimeGetComponent.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
private static SolrDocument toSolrDoc(Document doc, IndexSchema schema) {
  SolrDocument out = new SolrDocument();
  for ( IndexableField f : doc.getFields() ) {
    // Make sure multivalued fields are represented as lists
    Object existing = out.get(f.name());
    if (existing == null) {
      SchemaField sf = schema.getFieldOrNull(f.name());

      // don't return copyField targets
      if (sf != null && schema.isCopyFieldTarget(sf)) continue;

      if (sf != null && sf.multiValued()) {
        List<Object> vals = new ArrayList<>();
        vals.add( f );
        out.setField( f.name(), vals );
      }
      else{
        out.setField( f.name(), f );
      }
    }
    else {
      out.addField( f.name(), f );
    }
  }
  return out;
}
 
Example 9
Source File: SearchDaoImpl.java    From learning-taotaoMall with MIT License 5 votes vote down vote up
@Override
public SearchResult search(SolrQuery query) throws Exception {
	//返回值对象
	SearchResult result = new SearchResult();
	//根据查询条件查询索引库
	QueryResponse queryResponse = solrServer.query(query);
	//取查询结果
	SolrDocumentList solrDocumentList = queryResponse.getResults();
	//取查询结果总数量
	result.setRecordCount(solrDocumentList.getNumFound());
	//商品列表
	List<Item> itemList = new ArrayList<>();
	//取高亮显示
	Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
	//取商品列表
	for (SolrDocument solrDocument : solrDocumentList) {
		//创建一商品对象
		Item item = new Item();
		item.setId((String) solrDocument.get("id"));
		//取高亮显示的结果
		List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
		String title;
		if (list != null && list.size() > 0) {
			title = list.get(0);
		} else {
			title = (String) solrDocument.get("item_title");
		}
		item.setTitle(title);
		item.setImage((String) solrDocument.get("item_image"));
		item.setPrice((long) solrDocument.get("item_price"));
		item.setSell_point((String) solrDocument.get("item_sell_point"));
		item.setCategory_name((String) solrDocument.get("item_category_name"));
		//添加的商品列表
		itemList.add(item);
	}
	result.setItemList(itemList);
	return result;
}
 
Example 10
Source File: SolrAPI.java    From common-project with Apache License 2.0 5 votes vote down vote up
/**
 * response解析数组模型
 * @param queryResponse
 * @param tClass
 * @param <T>
 * @return
 */
public static <T> List<T> parseList(QueryResponse queryResponse, Class<T> tClass) {
    SolrDocumentList results = queryResponse.getResults();
    List<T> list = new ArrayList<T>();
    for (SolrDocument document : results) {
        Field[] fields = tClass.getDeclaredFields();
        try {
            T instance = tClass.newInstance();
            for (Field field : fields) {
                String fieldName = field.getName();
                Object value = document.get(fieldName);
                if (value == null) {
                    continue;
                }
                String methodName = ClassUtil.createFieldMethodName("set", fieldName);
                Method declaredMethod = tClass.getDeclaredMethod(methodName, field.getType());
                value = ClassUtil.parse(field.getType(), value);
                declaredMethod.invoke(instance, value);
            }
            list.add(instance);
        } catch (Exception e) {
            logger.error("SolrAPI数据解析出错啦!------ " + e);
            return null;
        }

    }
    return list;
}
 
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_docTransformerAndScoreRequested_shouldReturnScore() throws Exception
{
    putHandleDefaults();

    QueryResponse resp = query(getDefaultTestClient(), true, ALFRESCO_JSON, params("q", "*", "qt", "/afts", "shards.qt", "/afts","fl","cm_name, score, [fmap]", "sort", "id asc"));
    assertNotNull(resp);
    SolrDocumentList results = resp.getResults();
    SolrDocument docWithAllFields = results.get(0);
    assertEquals(2, docWithAllFields.size());
    assertNotNull(docWithAllFields.get("cm_name"));
    Float score = (Float) docWithAllFields.get("score");
    assertNotNull(score);
}
 
Example 12
Source File: SolrReader.java    From hive-solr with MIT License 5 votes vote down vote up
/**
 * 将solr的document封装进MapWritable里面
 * @param doc 转换的solr document
 * @param value MapWritable
 */
public void collect(SolrDocument doc,MapWritable value){
    for (String col : cols) {
        Object vObj = doc.get(col);
        Writable v = (vObj == null) ? NullWritable.get() : new Text(vObj.toString());
        value.put(new Text(col), v);
    }
}
 
Example 13
Source File: RenameFieldTransformer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void transform(SolrDocument doc, int docid) {
  Object v = (copy)?doc.get(from) : doc.remove( from );
  if( v != null ) {
    doc.setField(to, v);
  }
}
 
Example 14
Source File: TestInPlaceUpdateWithRouteField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdatingDocValuesWithRouteField() throws Exception {

   new UpdateRequest()
    .deleteByQuery("*:*").commit(cluster.getSolrClient(), COLLECTION);
  
   new UpdateRequest().add(createDocs(NUMBER_OF_DOCS)).commit(cluster.getSolrClient(), COLLECTION);

  int id = TestUtil.nextInt(random(), 1, NUMBER_OF_DOCS - 1);
  SolrDocument solrDocument = queryDoc(id);
  Long initialVersion = (Long) solrDocument.get("_version_");
  Integer luceneDocId = (Integer) solrDocument.get("[docid]");
  String shardName = (String) solrDocument.get("shardName");
  Assert.assertThat(solrDocument.get("inplace_updatable_int"), is(id));

  int newDocValue = TestUtil.nextInt(random(), 1, 2 * NUMBER_OF_DOCS - 1);
  SolrInputDocument sdoc = sdoc("id", ""+id,
      // use route field in update command
      "shardName", shardName,
      "inplace_updatable_int", map("set", newDocValue));
  
  UpdateRequest updateRequest = new UpdateRequest()
      .add(sdoc);
  updateRequest.commit(cluster.getSolrClient(), COLLECTION);
  solrDocument = queryDoc(id);
  Long newVersion = (Long) solrDocument.get("_version_");
  Assert.assertTrue("Version of updated document must be greater than original one",
      newVersion > initialVersion);
  Assert.assertThat( "Doc value must be updated", solrDocument.get("inplace_updatable_int"), is(newDocValue));
  Assert.assertThat("Lucene doc id should not be changed for In-Place Updates.", solrDocument.get("[docid]"), is(luceneDocId));

  sdoc.remove("shardName");
  checkWrongCommandFailure(sdoc);

  sdoc.addField("shardName",  map("set", "newShardName"));
  checkWrongCommandFailure(sdoc);
}
 
Example 15
Source File: TriLevelCompositeIdRoutingTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private Set<String> fetchUniqueKeysFromShard(final String shardId) throws Exception {
  // NUM_ADDS is an absolute upper bound on the num docs in the index
  QueryResponse rsp = cloudClient.query(params("q", "*:*", "rows", ""+NUM_ADDS, "shards", shardId));
  Set<String> uniqueKeys = new HashSet<>();
  for (SolrDocument doc : rsp.getResults()) {
    final String id = (String) doc.get("id");
    assertNotNull("null id WTF? " + doc.toString(), id);
    uniqueKeys.add(id);
  }
  return uniqueKeys;
}
 
Example 16
Source File: SolrConnector.java    From TagRec with GNU Affero General Public License v3.0 5 votes vote down vote up
private void processSolrDocument(SolrDocument d, Map<String, Double> hashtagMap, int limit) {
	double score = (float) d.get("score");
	List<String> hashtags = (List<String>) d.get("hashtags");
	for (String h : hashtags) {
		if (hashtagMap.size() < limit) {
			if (!hashtagMap.containsKey(h.toLowerCase())) {
				hashtagMap.put(h.toLowerCase(), score);
			}
			//Double val = hashtagMap.get(h.toLowerCase());
			//hashtagMap.put(h.toLowerCase(), val == null ? score : val.doubleValue() + score);
		} else {
			break;
		}
	}
}
 
Example 17
Source File: TestSubQueryTransformerDistrib.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("serial")
@Test
public void test() throws Exception {
  int peopleMultiplier = atLeast(1);
  int deptMultiplier = atLeast(1);
  
  createIndex(people, peopleMultiplier, depts, deptMultiplier);
  
  Random random1 = random();
  
  final ModifiableSolrParams params = params(
      new String[]{"q","name_s:dave", "indent","true",
          "fl","*,depts:[subquery "+((random1.nextBoolean() ? "" : "separator=,"))+"]",
          "rows","" + peopleMultiplier,
          "depts.q","{!terms f=dept_id_s v=$row.dept_ss_dv "+((random1.nextBoolean() ? "" : "separator=,"))+"}",
          "depts.fl","text_t"+(differentUniqueId?",id:notid":""),
          "depts.sort", "dept_id_i desc",
          "depts.indent","true",
          "depts.collection","departments",
          differentUniqueId ? "depts.distrib.singlePass":"notnecessary","true",
          "depts.rows",""+(deptMultiplier*2),
          "depts.logParamsList","q,fl,rows,row.dept_ss_dv",
          random().nextBoolean()?"depts.wt":"whatever",anyWt(),
          random().nextBoolean()?"wt":"whatever",anyWt()});

  final SolrDocumentList hits;
  {
    final QueryRequest qr = new QueryRequest(params);
    final QueryResponse  rsp = new QueryResponse();
    rsp.setResponse(cluster.getSolrClient().request(qr, people+","+depts));
    hits = rsp.getResults();
    
    assertEquals(peopleMultiplier, hits.getNumFound());
    
    int engineerCount = 0;
    int supportCount = 0;
    
    for (int res : new int [] {0, (peopleMultiplier-1) /2, peopleMultiplier-1}) {
      SolrDocument doc = hits.get(res);
      assertEquals("dave", doc.getFieldValue("name_s_dv"));
      SolrDocumentList relDepts = (SolrDocumentList) doc.getFieldValue("depts");
      assertEquals("dave works in both depts "+rsp,
          deptMultiplier * 2, relDepts.getNumFound());
      for (int deptN = 0 ; deptN < relDepts.getNumFound(); deptN++ ) {
        SolrDocument deptDoc = relDepts.get(deptN);
        String actual = (String) deptDoc.get("text_t");
        assertTrue(deptDoc + "should be either "+engineering +" or "+support,
            (engineering.equals(actual) && ++engineerCount>0) || 
                 (support.equals(actual) && ++supportCount>0));
      }
    }
    assertEquals(hits.toString(), engineerCount, supportCount); 
  }

  params.set("wt", "json");
  final URL node = new URL(cluster.getRandomJetty(random()).getBaseUrl().toString()
   +"/"+people+"/select"+params.toQueryString());

  try(final InputStream jsonResponse = node.openStream()){
    final ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    IOUtils.copy(jsonResponse, outBuffer);

    final Object expected = ((SolrDocumentList) hits.get(0).getFieldValue("depts")).get(0).get("text_t");
    final String err = JSONTestUtil.match("/response/docs/[0]/depts/docs/[0]/text_t"
        ,outBuffer.toString(Charset.forName("UTF-8").toString()),
        "\""+expected+"\"");
    assertNull(err,err);
  }
  
}
 
Example 18
Source File: TestInPlaceUpdatesDistrib.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
Object getReplicaValue(SolrClient client, int doc, String field) throws SolrServerException, IOException {
  SolrDocument sdoc = client.getById(String.valueOf(doc), params("distrib", "false"));
  return sdoc==null? null: sdoc.get(field);
}
 
Example 19
Source File: TestCloudNestedDocsSort.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test 
public void test() throws SolrServerException, IOException {
  final boolean asc = random().nextBoolean();
  final String dir = asc ? "asc": "desc";
  final String parentFilter = "+parentFilter_s:("+matchingParent+" "+anyValsSpaceDelim(2)+")^=0";
  String childFilter = "+childFilter_s:("+matchingChild+" "+anyValsSpaceDelim(4)+")^=0";
  final String fl = "id,type_s,parent_id_s1,val_s1,score,parentFilter_s,childFilter_s,parentTie_s1";
  String sortClause = "val_s1 "+dir+", "+"parent_id_s1 "+ascDesc();
  if(rarely()) {
    sortClause ="parentTie_s1 "+ascDesc()+","+sortClause;
  }
  final SolrQuery q = new SolrQuery("q", "+type_s:child^=0 "+parentFilter+" "+
        childFilter ,
      "sort", sortClause, 
      "rows", ""+maxDocs,
      "fl",fl);

  final QueryResponse children = client.query(q);
  
  final SolrQuery bjq = random().nextBoolean() ? 
       new SolrQuery(// top level bjq
         "q", "{!parent which=type_s:parent}(+type_s:child^=0 "+parentFilter+" "+ childFilter+")",
         "sort", sortClause.replace("val_s1", "childfield(val_s1)"),
         "rows", ""+maxDocs, "fl", fl)
       :
      new SolrQuery(// same bjq as a subordinate clause
         "q", "+type_s:parent "+parentFilter+" +{!v=$parentcaluse}",
         "parentcaluse","{!parent which=type_s:parent v='"+(childFilter).replace("+", "")+"'}",
         "sort", sortClause.replace("val_s1", "childfield(val_s1,$parentcaluse)"),
         "rows", ""+maxDocs, "fl", fl);

  final QueryResponse parents = client.query(bjq);
  
  Set<String> parentIds = new LinkedHashSet<>();
  assertTrue("it can never be empty for sure", parents.getResults().size()>0);
  for(Iterator<SolrDocument> parentIter = parents.getResults().iterator(); parentIter.hasNext();) {
    for (SolrDocument child : children.getResults()) {
      assertEquals("child", child.getFirstValue("type_s"));
      final String parentId = (String) child.getFirstValue("parent_id_s1");
      if( parentIds.add(parentId) ) { // in children the next parent appears, it should be next at parents 
        final SolrDocument parent = parentIter.next();
        assertEquals("parent", parent.getFirstValue("type_s"));
        final String actParentId = ""+ parent.get("id");
        if (!actParentId.equals(parentId)) {
          final String chDump = children.toString().replace("SolrDocument","\nSolrDocument");
          System.out.println("\n\n"+chDump+"\n\n");
          System.out.println("\n\n"+parents.toString().replace("SolrDocument","\nSolrDocument")
              +"\n\n");
        }
        assertEquals(""+child+"\n"+parent,actParentId, parentId);
      }
    }
  }

  
}
 
Example 20
Source File: ConverterService.java    From chronix.server with Apache License 2.0 3 votes vote down vote up
/**
 * Converts a solr document to a time series.
 * <p>
 * The resulting time series does not contain user defined attributes present in the solr document
 * (see {@link de.qaware.chronix.Schema#isUserDefined(String)} ).
 *
 * @param solrDoc the solr document
 * @return time series representing the given solr document
 */
public MetricTimeSeries toTimeSeries(SolrDocument solrDoc) {
    BinaryTimeSeries.Builder btsBuilder = new BinaryTimeSeries.Builder();
    solrDoc.forEach(field -> btsBuilder.field(field.getKey(), field.getValue()));
    BinaryTimeSeries bts = btsBuilder.build();
    long start = (long) solrDoc.get(START);
    long end = (long) solrDoc.get(END);
    return converter.from(bts, start, end);
}