lucene-solr-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig Stires" <craig.sti...@gmail.com>
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.

 

Thanks,

-Craig

 

 

<code>

package org.apache.solr.schema;

 

import java.util.Map;

import java.io.ByteArrayInputStream;

import java.io.IOException;

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   =
FieldProperties.OMIT_TF_POSITIONS;

   final public static int STORE_TERMVECTORS   =
FieldProperties.STORE_TERMVECTORS;

   final public static int STORE_TERMPOSITIONS =
FieldProperties.STORE_TERMPOSITIONS;

   final public static int STORE_TERMOFFSETS   =
FieldProperties.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;

      init(args);

      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
name=\"min\"/>";

         ByteArrayInputStream bisc = new
ByteArrayInputStream(configMin.getBytes());

         InputSource isc = new InputSource(bisc);

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

 

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

         ByteArrayInputStream biss = new
ByteArrayInputStream(schemaMin.getBytes());

         InputSource iss = new InputSource(biss);

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

      }

      catch (ParserConfigurationException epc) {

         epc.printStackTrace(System.out);

      }

     catch (SAXException epc) {

         epc.printStackTrace(System.out);

      }

      catch (IOException eio) {

         eio.printStackTrace(System.out);

      }

 

      ft.setArgs(ischema, args);

   }

 

   public Field createField(String fieldval, float boost) {

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

      return (field);

   }

 

}

 

</code>

 

 

And to create a TrieDateField

 

<code>

   String name = "changed";

   TrieDateField tdate = new TrieDateField();

   int properties = (MY_SolrField.INDEXED | MY_SolrField.TOKENIZED |
MY_SolrField.STORED | MY_SolrField.OMIT_NORMS |
MY_SolrField.STORE_TERMVECTORS);

   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,
defaultValue);

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

   doc.add(field);

</code>

 

 

 


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