org.apache.solr.client.solrj.response.LukeResponse Java Examples

The following examples show how to use org.apache.solr.client.solrj.response.LukeResponse. 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: SolrSchemaFieldDao.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private List<LukeResponse> getLukeResponsesForCores(CloudSolrClient solrClient) {
  ZkStateReader zkStateReader = solrClient.getZkStateReader();
  Collection<Slice> activeSlices = zkStateReader.getClusterState().getCollection(solrClient.getDefaultCollection()).getActiveSlices();
  
  List<LukeResponse> lukeResponses = new ArrayList<>();
  for (Slice slice : activeSlices) {
    for (Replica replica : slice.getReplicas()) {
      try (CloseableHttpClient httpClient = HttpClientUtil.createClient(null)) {
        HttpGet request = new HttpGet(replica.getCoreUrl() + LUKE_REQUEST_URL_SUFFIX);
        HttpResponse response = httpClient.execute(request);
        @SuppressWarnings("resource") // JavaBinCodec implements Closeable, yet it can't be closed if it is used for unmarshalling only
        NamedList<Object> lukeData = (NamedList<Object>) new JavaBinCodec().unmarshal(response.getEntity().getContent());
        LukeResponse lukeResponse = new LukeResponse();
        lukeResponse.setResponse(lukeData);
        lukeResponses.add(lukeResponse);
      } catch (IOException e) {
        logger.error("Exception during getting luke responses", e);
      }
    }
  }
  return lukeResponses;
}
 
Example #2
Source File: DynamicCopyFieldsIT.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static Map<String, HashSet<String>> getIndexedFieldsModifiers() throws IOException, SolrServerException {
    SolrClient solrClient = getStandaloneClients().get(0);
    LukeRequest request = new LukeRequest();
    LukeResponse response = request.process(solrClient);
    Map<String, HashSet<String>> collect = response.getFieldInfo().keySet().stream()
            .map(k -> k.split("@|\\{|\\}"))
            .filter(e -> e.length == 5)
            .collect(Collectors.toMap(
                    a -> a[4],
                    a -> Sets.newHashSet(a[1]),
                    (value1, value2) -> {
                        value1.addAll(value2);
                        return value1;
                    }));
    return collect;
}
 
Example #3
Source File: SolrColumnMetadataDao.java    From metron with Apache License 2.0 6 votes vote down vote up
protected List<Map<String, Object>> getIndexFields(String index)
    throws IOException, SolrServerException {
  List<Map<String, Object>> indexFields = new ArrayList<>();

  // Get all the fields in use, including dynamic fields
  LukeRequest lukeRequest = new LukeRequest();
  LukeResponse lukeResponse = lukeRequest.process(client, index);
  for (Entry<String, LukeResponse.FieldInfo> field : lukeResponse.getFieldInfo().entrySet()) {
    Map<String, Object> fieldData = new HashMap<>();
    fieldData.put("name", field.getValue().getName());
    fieldData.put("type", field.getValue().getType());
    indexFields.add(fieldData);

  }

  // Get all the schema fields
  SchemaRepresentation schemaRepresentation = new SchemaRequest().process(client, index)
      .getSchemaRepresentation();
  indexFields.addAll(schemaRepresentation.getFields());

  return indexFields;
}
 
Example #4
Source File: SolrSchemaFieldDao.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
private void populateSchemaFields(CloudSolrClient solrClient, Map<String, String> schemaFieldNameMap,
    Map<String, String> schemaFieldTypeMap) {
  if (solrClient != null) {
    logger.debug("Started thread to get fields for collection=" + solrClient.getDefaultCollection());
    List<LukeResponse> lukeResponses = null;
    SchemaResponse schemaResponse = null;
    try {
      lukeResponses = getLukeResponsesForCores(solrClient);

      SolrRequest<SchemaResponse> schemaRequest = new SchemaRequest();
      schemaRequest.setMethod(SolrRequest.METHOD.GET);
      schemaRequest.setPath("/schema");
      schemaResponse = schemaRequest.process(solrClient);
      
      logger.debug("populateSchemaFields() collection=" + solrClient.getDefaultCollection() + ", luke=" + lukeResponses +
          ", schema= " + schemaResponse);
    } catch (SolrException | SolrServerException | IOException e) {
      logger.error("Error occured while popuplating field. collection=" + solrClient.getDefaultCollection(), e);
    }

    if (schemaResponse != null) {
      extractSchemaFieldsName(lukeResponses, schemaResponse, schemaFieldNameMap, schemaFieldTypeMap);
      logger.debug("Populate fields for collection " + solrClient.getDefaultCollection()+ " was successful, next update it after " +
          solrMetadataPropsConfig.getPopulateIntervalMins() + " minutes");
      retryCount = 0;
      skipCount = (solrMetadataPropsConfig.getPopulateIntervalMins() * 60) / RETRY_SECOND - 1;
    }
    else {
      retryCount++;
      logger.error("Error while populating fields for collection " + solrClient.getDefaultCollection() + ", retryCount=" + retryCount);
    }
  }
}
 
Example #5
Source File: SolrExampleTests.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testLukeHandler() throws Exception
 {    
   SolrClient client = getSolrClient();
   
   // Empty the database...
   client.deleteByQuery("*:*");// delete everything!
   
   SolrInputDocument[] doc = new SolrInputDocument[5];
   for( int i=0; i<doc.length; i++ ) {
     doc[i] = new SolrInputDocument();
     doc[i].setField( "id", "ID"+i );
     client.add(doc[i]);
   }
   client.commit();
   assertNumFound( "*:*", doc.length ); // make sure it got in
   
   LukeRequest luke = new LukeRequest();
   luke.setShowSchema( false );
   LukeResponse rsp = luke.process( client );
   assertNull( rsp.getFieldTypeInfo() ); // if you don't ask for it, the schema is null
   assertNull( rsp.getDynamicFieldInfo() );
   
   luke.setShowSchema( true );
   rsp = luke.process( client );
   assertNotNull( rsp.getFieldTypeInfo() );
   assertNotNull(rsp.getFieldInfo().get("id").getSchemaFlags());
   assertTrue(rsp.getFieldInfo().get("id").getSchemaFlags().contains(FieldFlag.INDEXED));
   assertNotNull( rsp.getDynamicFieldInfo() );
 }
 
Example #6
Source File: SolrSchema.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private Map<String, LukeResponse.FieldInfo> getFieldInfo(String collection) {
  String zk = this.properties.getProperty("zk");
  CloudSolrClient cloudSolrClient = solrClientCache.getCloudSolrClient(zk);
  try {
    LukeRequest lukeRequest = new LukeRequest();
    lukeRequest.setNumTerms(0);
    LukeResponse lukeResponse = lukeRequest.process(cloudSolrClient, collection);
    return lukeResponse.getFieldInfo();
  } catch (SolrServerException | IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #7
Source File: SolrSearchEngine.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public List<String> getDynamicFieldNames() throws SearchEngineException {
	List<String> fields = new ArrayList<>();
	
	LukeRequest request = new LukeRequest();
	request.setNumTerms(0);
	request.setShowSchema(false);
	try {
		LukeResponse response = request.process(getServer());
		NamedList<Object> flds = (NamedList<Object>) response.getResponse().get("fields");
		if (flds != null) {
			for (Map.Entry<String, Object> field : flds) {
				String name = field.getKey();
				for (Entry<String, Object> prop : (NamedList<Object>)field.getValue()) {
					if ("dynamicBase".equals(prop.getKey())) {
						fields.add(name);
						break;
					}
				}
			}
		}

	} catch (SolrServerException | IOException e) {
		throw new SearchEngineException(e);
	}
	
	return fields;
}
 
Example #8
Source File: LukeRequest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected LukeResponse createResponse(SolrClient client) {
  return new LukeResponse();
}
 
Example #9
Source File: SolrSchema.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
RelProtoDataType getRelDataType(String collection) {
  // Temporary type factory, just for the duration of this method. Allowable
  // because we're creating a proto-type, not a type; before being used, the
  // proto-type will be copied into a real type factory.
  final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
  Map<String, LukeResponse.FieldInfo> luceneFieldInfoMap = getFieldInfo(collection);

  for(Map.Entry<String, LukeResponse.FieldInfo> entry : luceneFieldInfoMap.entrySet()) {
    LukeResponse.FieldInfo luceneFieldInfo = entry.getValue();

    String luceneFieldType = luceneFieldInfo.getType();
    // SOLR-13414: Luke can return a field definition with no type in rare situations
    if(luceneFieldType == null) {
      continue;
    }

    RelDataType type;
    switch (luceneFieldType) {
      case "string":
        type = typeFactory.createJavaType(String.class);
        break;
      case "tint":
      case "tlong":
      case "int":
      case "long":
      case "pint":
      case "plong":
        type = typeFactory.createJavaType(Long.class);
        break;
      case "tfloat":
      case "tdouble":
      case "float":
      case "double":
      case "pfloat":
      case "pdouble":
        type = typeFactory.createJavaType(Double.class);
        break;
      default:
        type = typeFactory.createJavaType(String.class);
    }

    /*
    EnumSet<FieldFlag> flags = luceneFieldInfo.parseFlags(luceneFieldInfo.getSchema());
    if(flags != null && flags.contains(FieldFlag.MULTI_VALUED)) {
      type = typeFactory.createArrayType(type, -1);
    }
    */

    fieldInfo.add(entry.getKey(), type).nullable(true);
  }
  fieldInfo.add("_query_",typeFactory.createJavaType(String.class));
  fieldInfo.add("score",typeFactory.createJavaType(Double.class));

  return RelDataTypeImpl.proto(fieldInfo.build());
}