List of usage examples for org.apache.solr.schema IndexSchema getField
public SchemaField getField(String fieldName)
From source file:GeonamesQParserPlugin.java
License:Apache License
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) { return new QParser(qstr, localParams, params, req) { @Override/*from w w w . j a v a 2 s . c om*/ public Query parse() throws ParseException { final int rows = localParams.getInt("rows", 1); final int start = localParams.getInt("start", 0); String topo = localParams.get("topo"); String distFunc = localParams.get("dist"); String latFieldName = localParams.get("lat"); String lonFieldName = localParams.get("lon"); String ghFieldName = localParams.get("gh"); String units = localParams.get("unit", "M"); float boost = localParams.getFloat("boost", 1.0f); double radius = Constants.EARTH_RADIUS_MI; if (units.equalsIgnoreCase("KM")) { radius = Constants.EARTH_RADIUS_KM; } ValueSource vs = null, latVS = null, lonVS = null, ghVS = null; IndexSchema schema = req.getSchema(); DistType distType = DistType.NORM; float power = 2; List<ValueSource> latLon = new ArrayList<ValueSource>(2); if (ghFieldName != null && ghFieldName.equals("") == false) { FieldType ft = schema.getFieldType(ghFieldName); SchemaField sf = schema.getField(ghFieldName); ghVS = ft.getValueSource(sf, this);//Should we pass this here? distType = DistType.GHHSIN; } else if (latFieldName != null && latFieldName.equals("") == false && lonFieldName != null && lonFieldName.equals("") == false) { FieldType ftLat = schema.getFieldType(latFieldName); FieldType ftLon = schema.getFieldType(lonFieldName); SchemaField sfLat = schema.getField(latFieldName); SchemaField sfLon = schema.getField(lonFieldName); latVS = ftLat.getValueSource(sfLat, this); lonVS = ftLon.getValueSource(sfLon, this); latLon.add(latVS); latLon.add(lonVS); try { power = Float.parseFloat(distFunc); distType = DistType.NORM; } catch (NumberFormatException e) { if (distFunc.equals("hsin")) { distType = DistType.HSIN; } else if (distFunc.equals("ghsin")) { distType = DistType.GHHSIN; } } } else { throw new ParseException("Either gh or both lat and lon must be specified."); } ToponymSearchCriteria searchCriteria = new ToponymSearchCriteria(); searchCriteria.setQ(topo); searchCriteria.setMaxRows(rows); searchCriteria.setStartRow(start); Query query = null; try { ToponymSearchResult searchResult = WebService.search(searchCriteria); List<Toponym> topos = searchResult.getToponyms(); if (topos.size() > 1) { BooleanQuery tmp = new BooleanQuery(); for (Toponym toponym : topos) { double lat = toponym.getLatitude(); double lon = toponym.getLongitude(); FunctionQuery fq = getFunction(distType, ghVS, power, latLon, lat, lon, radius); tmp.add(fq, BooleanClause.Occur.SHOULD); } query = tmp; } else if (topos.size() == 1) { Toponym curr = topos.get(0); query = getFunction(distType, ghVS, power, latLon, curr.getLatitude(), curr.getLongitude(), radius); } } catch (Exception e) { //TODO: deal with errors } query.setBoost(boost); return query; } }; }
From source file:com.gu.solr.MergeUtils.java
License:Apache License
public static SolrInputDocument merge(SolrInputDocument solrInputDocument, SolrDocument existing, IndexSchema schema, boolean overwriteMultivalues) { SolrInputDocument merged = copy(existing); for (SolrInputField field : solrInputDocument) { String fieldName = field.getName(); if (!overwriteMultivalues && schema.getField(fieldName).multiValued()) { // leave for additions } else {/* w ww. j a va 2s . c o m*/ merged.removeField(fieldName); } } for (SolrInputField field : solrInputDocument) { merged.addField(field.getName(), field.getValue()); } return merged; }
From source file:com.gu.solr.MergeUtils.java
License:Apache License
private static void addFields(Document luceneDoc, SolrDocument solrDoc, IndexSchema schema) { for (Fieldable f : (List<Fieldable>) luceneDoc.getFields()) { SchemaField sf = schema.getField(f.name()); if (!schema.isCopyFieldTarget(sf)) { Object externalVal = sf.getType().toObject(f); solrDoc.addField(f.name(), externalVal); }// w ww . ja v a 2 s.c o m } }
From source file:com.sindicetech.siren.solr.schema.TestConciseJsonField.java
License:Open Source License
@Test public void testConciseSirenFieldType() throws Exception { final IndexSchema schema = h.getCore().getLatestSchema(); SchemaField json = schema.getField("concise"); assertNotNull(json);/* ww w.j av a2s. c o m*/ FieldType tmp = json.getType(); assertTrue(tmp instanceof ConciseJsonField); json = schema.getField("concise-attribute-wildcard"); assertNotNull(json); tmp = json.getType(); assertTrue(tmp instanceof ConciseJsonField); }
From source file:com.sindicetech.siren.solr.schema.TestConciseJsonField.java
License:Open Source License
@Test public void testConciseSirenFieldAnalyzer() throws Exception { final IndexSchema schema = h.getCore().getLatestSchema(); final SchemaField json = schema.getField("concise"); final FieldType tmp = json.getType(); assertTrue(tmp.getAnalyzer() instanceof TokenizerChain); final TokenizerChain ts = (TokenizerChain) tmp.getAnalyzer(); assertNotNull(ts.getTokenizerFactory()); assertTrue(ts.getTokenizerFactory() instanceof ConciseJsonTokenizerFactory); // 4 filters for index analyzer assertNotNull(ts.getTokenFilterFactories()); assertEquals(4, ts.getTokenFilterFactories().length); assertTrue(ts.getTokenFilterFactories()[0] instanceof DatatypeAnalyzerFilterFactory); assertTrue(ts.getTokenFilterFactories()[1] instanceof PathEncodingFilterFactory); assertTrue(ts.getTokenFilterFactories()[2] instanceof PositionAttributeFilterFactory); assertTrue(ts.getTokenFilterFactories()[3] instanceof SirenPayloadFilterFactory); }
From source file:com.sindicetech.siren.solr.schema.TestExtendedJsonField.java
License:Open Source License
@Test public void testSirenFieldType() throws Exception { final IndexSchema schema = h.getCore().getLatestSchema(); final SchemaField ntriple = schema.getField(JSON_FIELD); assertNotNull(ntriple);//from w ww . java 2 s.co m final FieldType tmp = ntriple.getType(); assertTrue(tmp instanceof ExtendedJsonField); }
From source file:com.sindicetech.siren.solr.schema.TestExtendedJsonField.java
License:Open Source License
@Test public void testSirenFieldTypeProperties() throws Exception { final IndexSchema schema = h.getCore().getLatestSchema(); final FieldType type = schema.getField("json").getType(); assertTrue(type instanceof ExtendedJsonField); assertFalse(type.isMultiValued());//www.j a va 2 s .c o m assertTrue(type.isTokenized()); assertEquals(type.getPostingsFormat(), Siren10AForPostingsFormat.NAME); }
From source file:com.sindicetech.siren.solr.schema.TestExtendedJsonField.java
License:Open Source License
@Test public void testSirenFieldAnalyzer() throws Exception { final IndexSchema schema = h.getCore().getLatestSchema(); final SchemaField ntriple = schema.getField(JSON_FIELD); final FieldType tmp = ntriple.getType(); assertTrue(tmp.getAnalyzer() instanceof TokenizerChain); final TokenizerChain ts = (TokenizerChain) tmp.getAnalyzer(); assertNotNull(ts.getTokenizerFactory()); assertTrue(ts.getTokenizerFactory() instanceof ExtendedJsonTokenizerFactory); // 3 filters for index analyzer assertNotNull(ts.getTokenFilterFactories()); assertEquals(3, ts.getTokenFilterFactories().length); assertTrue(ts.getTokenFilterFactories()[0] instanceof DatatypeAnalyzerFilterFactory); assertTrue(ts.getTokenFilterFactories()[1] instanceof PositionAttributeFilterFactory); assertTrue(ts.getTokenFilterFactories()[2] instanceof SirenPayloadFilterFactory); }
From source file:com.sindicetech.siren.solr.schema.TestExtendedJsonField.java
License:Open Source License
@Test public void testSirenFieldDatatypeAnalyzer() throws Exception { final IndexSchema schema = h.getCore().getLatestSchema(); final SchemaField ntriple = schema.getField(JSON_FIELD); final FieldType tmp = ntriple.getType(); TokenizerChain ts = (TokenizerChain) tmp.getAnalyzer(); assertTrue(ts.getTokenFilterFactories()[0] instanceof DatatypeAnalyzerFilterFactory); final DatatypeAnalyzerFilterFactory f = (DatatypeAnalyzerFilterFactory) ts.getTokenFilterFactories()[0]; assertNotNull(f.getDatatypeAnalyzers()); assertEquals(9, f.getDatatypeAnalyzers().size()); assertNotNull(f.getDatatypeAnalyzers().get("http://json.org/field")); ts = (TokenizerChain) f.getDatatypeAnalyzers().get("http://json.org/field"); assertNotNull(ts.getTokenizerFactory()); assertTrue(ts.getTokenizerFactory() instanceof WhitespaceTokenizerFactory); assertNotNull(f.getDatatypeAnalyzers().get("http://www.w3.org/2001/XMLSchema#string")); ts = (TokenizerChain) f.getDatatypeAnalyzers().get("http://www.w3.org/2001/XMLSchema#string"); assertNotNull(ts.getTokenizerFactory()); assertTrue(ts.getTokenizerFactory() instanceof UAX29URLEmailTokenizerFactory); assertNotNull(f.getDatatypeAnalyzers().get("http://www.w3.org/2001/XMLSchema#int")); assertTrue(//from w w w .j a v a 2s. co m f.getDatatypeAnalyzers().get("http://www.w3.org/2001/XMLSchema#int") instanceof IntNumericAnalyzer); final IntNumericAnalyzer a = (IntNumericAnalyzer) f.getDatatypeAnalyzers() .get("http://www.w3.org/2001/XMLSchema#int"); assertEquals(8, a.getPrecisionStep()); assertEquals(32, a.getNumericParser().getValueSize()); assertEquals(NumericType.INT, a.getNumericParser().getNumericType()); }
From source file:de.qaware.chronix.solr.compaction.ConverterService.java
License:Apache License
/** * Converts a lucene document to a solr document. * * @param schema the index schema/*from www. j a v a 2 s .com*/ * @param luceneDoc the lucene document * @return solr document */ public SolrDocument toSolrDoc(IndexSchema schema, Document luceneDoc) { SolrDocument solrDoc = new SolrDocument(); luceneDoc.forEach(it -> solrDoc.addField(it.name(), schema.getField(it.name()).getType().toObject(it))); for (String field : solrDoc.getFieldNames()) { Object value = solrDoc.getFieldValue(field); if (value instanceof ByteBuffer) { solrDoc.setField(field, ((ByteBuffer) value).array()); } } return solrDoc; }