lucene-solr-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig Stires" <>
Subject Creating a TrieDateField (and other Trie fields) from Lucene Java
Date Wed, 20 Apr 2011 03:17:26 GMT

Wanted to share this, as I've seen a couple discussions on different boards.
The solution has been either:

1. use the solrj client

2. import as csv

3. use the streamingupdatesolrserver


The barrier I have is that I need to build this offline (without using a
solr server, solrconfig.xml, or schema.xml), instead using the solr java
classes to create the fields in a Lucene index.  And, the Solr API for
FieldType doesn't seem to expose enough to allow this.  To get done what I
needed, I created a class under the package org.apache.solr.schema (in my
own jar).  There is probably a better way to get this done, but I just
needed to explicitly create TrieDateFields, and this got it done.


Sharing, if this helps someone in the same situation.  Or, if someone has
got a better approach, I'd most appreciate it.







package org.apache.solr.schema;


import java.util.Map;



import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;


import org.apache.lucene.document.Field;

import org.apache.solr.core.SolrConfig;


public class MY_SolrField {


   // bit values for boolean field properties.

   final public static int INDEXED             = FieldProperties.INDEXED;

   final public static int TOKENIZED           = FieldProperties.TOKENIZED;

   final public static int STORED              = FieldProperties.STORED;

   final public static int BINARY              = FieldProperties.BINARY;

   final public static int OMIT_NORMS          = FieldProperties.OMIT_NORMS;

   final public static int OMIT_TF_POSITIONS   =

   final public static int STORE_TERMVECTORS   =

   final public static int STORE_TERMPOSITIONS =

   final public static int STORE_TERMOFFSETS   =


   private FieldType              ft          = null;

   private SchemaField            sf          = null;


   public MY_SolrField(String fieldname, FieldType fieldtype, int
properties, Map<String,String> args, String defaultValue) {

      ft    = fieldtype;


      sf = new SchemaField(fieldname, ft, properties, defaultValue);



   private void init(Map<String,String> args) {

      IndexSchema ischema = null;

      try {

         String configMin = "<?xml version=\"1.0\" ?><config

         ByteArrayInputStream bisc = new

         InputSource isc = new InputSource(bisc);

         SolrConfig sconfig = new SolrConfig("x", isc);


         String schemaMin = "<?xml version=\"1.0\" ?><schema name=\"min\"

         ByteArrayInputStream biss = new

         InputSource iss = new InputSource(biss);

         ischema = new IndexSchema(sconfig, "x", iss);


      catch (ParserConfigurationException epc) {



     catch (SAXException epc) {



      catch (IOException eio) {




      ft.setArgs(ischema, args);



   public Field createField(String fieldval, float boost) {

      Field field = ft.createField(sf, fieldval, boost);

      return (field);








And to create a TrieDateField



   String name = "changed";

   TrieDateField tdate = new TrieDateField();

   int properties = (MY_SolrField.INDEXED | MY_SolrField.TOKENIZED |
MY_SolrField.STORED | MY_SolrField.OMIT_NORMS |

   Map<String,String> myargs = new HashMap<String,String>();

   myargs.put("precisionStep",        "6");

   myargs.put("positionIncrementGap", "0");

   String defaultValue = "1970-01-01T00:00:00Z";


   MY_SolrField myfield = new MY_SolrField(name, tdate, properties, myargs,

   Field field = myfield.createField("2011-04-18T18:30:00Z", 0);






  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message