List of usage examples for org.apache.solr.schema IndexSchema getFieldType
public FieldType getFieldType(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// w w w .j a v a2 s . c o m 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.sindicetech.siren.solr.facet.TestSirenFacetProcessorFactory.java
License:Open Source License
public void testStringField() throws Exception { String json = "{\"knows\": [{\"name\":\"josef\"}, {\"name\":\"szymon\"}]}"; IndexSchema schema = h.getCore().getLatestSchema(); SolrInputDocument d = processAdd("generate-facets-processor", doc(f("id", "1"), f("json", json))); assertNotNull(d);/*from w w w .ja v a 2s .com*/ schema = h.getCore().getLatestSchema(); assertNotNull(schema.getFieldOrNull("string.json.knows.name")); assertEquals("string", schema.getFieldType("string.json.knows.name").getTypeName()); json = "{\"knows\": [{\"name\": null}]}"; d = processAdd("generate-facets-processor", doc(f("id", "2"), f("json", json))); assertNotNull(d); schema = h.getCore().getLatestSchema(); assertNotNull(schema.getFieldOrNull("string.json.knows.name")); assertEquals("string", schema.getFieldType("string.json.knows.name").getTypeName()); json = "{\"knows\": {\"name\": true}}"; d = processAdd("generate-facets-processor", doc(f("id", "3"), f("json", json))); assertNotNull(d); schema = h.getCore().getLatestSchema(); assertNotNull(schema.getFieldOrNull("string.json.knows.name")); assertEquals("string", schema.getFieldType("string.json.knows.name").getTypeName()); json = "{\"age\": 1}"; d = processAdd("generate-facets-processor", doc(f("id", "4"), f("json", json))); assertNotNull(d); schema = h.getCore().getLatestSchema(); assertNotNull(schema.getFieldOrNull("long.json.age")); assertEquals("tlong", schema.getFieldType("long.json.age").getTypeName()); json = "{\"length\": 18.9}"; d = processAdd("generate-facets-processor", doc(f("id", "5"), f("json", json))); assertNotNull(d); schema = h.getCore().getLatestSchema(); assertNotNull(schema.getFieldOrNull("double.json.length")); assertEquals("tdouble", schema.getFieldType("double.json.length").getTypeName()); // no facets should be generated for fields with values longer than SirenFacetProcessor.MAX_FACET_VALUE_LENGTH String tooLongValue = "Every night and every morn Some to misery are born, " + "Every morn and every night Some are born to sweet delight. Some are born to sweet" + " delight, Some are born to endless night."; json = "{\"description\": \"" + tooLongValue + "\"}"; SirenFacetProcessorFactory factory = (SirenFacetProcessorFactory) h.getCore() .getUpdateProcessingChain("generate-facets-processor").getFactories()[0]; TypeMapping stringTypeMapping = factory.getTypeMappingValueClass(FacetDatatype.STRING.xsdDatatype); assertTrue("Bad test. Test value has to be longer than maxFieldSize for TypeMapping of string = " + stringTypeMapping.maxFieldSize + " but its length is only " + tooLongValue.length() + ". You should check the size of " + "SirenFacetProcessor.DEFAULT_MAX_FACET_VALUE_LENGTH or solrconfig*.xml for the maxFieldSize setting of the string typeMapping " + " of the updateRequestProcessorChain", tooLongValue.length() > stringTypeMapping.maxFieldSize); d = processAdd("generate-facets-processor", doc(f("id", "6"), f("json", json))); assertNotNull(d); schema = h.getCore().getLatestSchema(); assertNull(schema.getFieldOrNull("string.json.description")); assertNull(d.getFieldValue("string.json.description")); }
From source file:com.sindicetech.siren.solr.facet.TestSirenFacetProcessorFactory.java
License:Open Source License
public void testCustomDatatypeField() throws Exception { String json = "{\"rating\": {\"_datatype_\": \"http://www.w3.org/2001/XMLSchema#double\", \"_value_\":\"5.4\"}}"; IndexSchema schema = h.getCore().getLatestSchema(); SolrInputDocument d = processAdd("generate-facets-processor", doc(f("id", "1"), f("json", json))); assertNotNull(d);/*from w ww .j a v a2s. c om*/ schema = h.getCore().getLatestSchema(); assertNotNull(schema.getFieldOrNull("double.json.rating")); assertEquals("tdouble", schema.getFieldType("double.json.rating").getTypeName()); assertTrue((5.4 - (double) d.getFieldValue("double.json.rating")) < 0.01); getWrapper().add(d); // add so that we can test facets by querying this.commit(); SolrQuery q = new SolrQuery(); q.setRequestHandler("keyword"); q.setParam("nested", "{!lucene} *:*"); q.setFacet(true); q.addFacetField("double.json.rating"); QueryResponse r = getWrapper().getServer().query(q); // we know there is only one facet field with one value assertEquals(1, r.getFacetFields().get(0).getValues().get(0).getCount()); json = "{\"rating\": {\"_datatype_\": \"http://www.w3.org/2001/XMLSchema#float\", \"_value_\":\"-8.4\"}}"; schema = h.getCore().getLatestSchema(); d = processAdd("generate-facets-processor", doc(f("id", "2"), f("json", json))); assertNotNull(d); schema = h.getCore().getLatestSchema(); assertNotNull(schema.getFieldOrNull("double.json.rating")); assertEquals("tdouble", schema.getFieldType("double.json.rating").getTypeName()); assertTrue((-8.4 + (double) d.getFieldValue("double.json.rating")) < 0.01); getWrapper().add(d); // add so that we can test facets by querying this.commit(); r = getWrapper().getServer().query(q); // there is only one facet field with two different values each with a single count assertEquals(1, r.getFacetFields().get(0).getValues().get(0).getCount()); assertEquals(1, r.getFacetFields().get(0).getValues().get(1).getCount()); }
From source file:com.sindicetech.siren.solr.qparser.SirenQParser.java
License:Open Source License
/** * Check if all fields are of type {@link com.sindicetech.siren.solr.schema.ExtendedJsonField}. *//*from w w w. j a v a 2 s. c o m*/ private static void checkFieldTypes(final IndexSchema indexSchema, final Map<String, Float> queryFields) { for (final String fieldName : queryFields.keySet()) { final FieldType fieldType = indexSchema.getFieldType(fieldName); if (!(fieldType instanceof ExtendedJsonField)) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "FieldType: " + fieldName + " (" + fieldType.getTypeName() + ") do not support SIREn's tree query"); } } }
From source file:org.opencms.search.solr.CmsSolrDocument.java
License:Open Source License
/** * @see org.opencms.search.I_CmsSearchDocument#addSearchField(org.opencms.search.fields.CmsSearchField, java.lang.String) *///from w ww . j a v a 2 s.c o m public void addSearchField(CmsSearchField sfield, String value) { CmsSolrField field = (CmsSolrField) sfield; List<String> fieldsToAdd = new ArrayList<String>(Collections.singletonList(field.getName())); if ((field.getCopyFields() != null) && !field.getCopyFields().isEmpty()) { fieldsToAdd.addAll(field.getCopyFields()); } IndexSchema schema = OpenCms.getSearchManager().getSolrServerConfiguration().getSolrSchema(); for (String fieldName : fieldsToAdd) { try { List<String> splitedValues = new ArrayList<String>(); boolean multi = false; try { SchemaField f = schema.getField(fieldName); if ((f != null) && (!field.getName().startsWith(CmsSearchField.FIELD_CONTENT))) { multi = f.multiValued(); } } catch (@SuppressWarnings("unused") SolrException e) { LOG.warn(Messages.get().getBundle().key(Messages.LOG_SOLR_FIELD_NOT_FOUND_1, field.toString())); } if (multi) { splitedValues = CmsStringUtil.splitAsList(value.toString(), "\n"); } else { splitedValues.add(value); } for (String val : splitedValues) { if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(val)) { try { FieldType type = schema.getFieldType(fieldName); if (type instanceof TrieDateField) { val = DF.format(new Date(new Long(val).longValue())); } } catch (SolrException e) { LOG.debug(e.getMessage(), e); throw new RuntimeException(e); } if (fieldName.endsWith(CmsSearchField.FIELD_EXCERPT)) { // TODO: make the length and the area configurable val = CmsStringUtil.trimToSize(val, 1000, 50, ""); } if (schema.hasExplicitField(fieldName) || (schema.getDynamicPattern(fieldName) != null)) { m_doc.addField(fieldName, val); } else { m_doc.addField(fieldName, val, field.getBoost()); } } } } catch (SolrException e) { LOG.error(e.getMessage(), e); } catch (@SuppressWarnings("unused") RuntimeException e) { // noop } } }
From source file:org.sindice.siren.solr.qparser.SirenQParser.java
License:Apache License
/** * Check if all fields are of type {@link SirenField}. *//* w ww .j a v a2 s.c o m*/ private static void checkFieldTypes(final IndexSchema indexSchema, final Map<String, Float> queryFields) { for (final String fieldName : queryFields.keySet()) { final FieldType fieldType = indexSchema.getFieldType(fieldName); if (!(fieldType instanceof SirenField)) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "FieldType: " + fieldName + " (" + fieldType.getTypeName() + ") do not support Siren's tree query"); } } }