Lucene – Horatio says ’tis but our fantasy

Useful links to start with Lucene:
http://www.lucenetutorial.com/lucene-in-5-minutes.html
http://stackoverflow.com/questions/468405/how-to-incorporate-multiple-fields-in-queryparser
Here an example how to index and search text:

package ch.prait.lucene;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.KeywordAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

public class PrettyLucene {

	public void index(Directory index, List<String> stringsToIndex, String teller) throws Exception {
		// create an analyzer
		StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);

		// Create the index writer
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
		IndexWriter w = new IndexWriter(index, config);

		int idCounter = 0;
		for (String stringToIndex : stringsToIndex) {

			// create a document and add three fields to it whereas 2 are analyzed and one is just
			// information stored
			Document myDocument = new Document();
			myDocument.add(new Field("myString", stringToIndex, Field.Store.YES, Field.Index.ANALYZED));
			myDocument.add(new Field("id", String.valueOf(idCounter), Field.Store.YES, Field.Index.ANALYZED));
			myDocument.add(new Field("teller", teller, Field.Store.YES, Field.Index.NO));
			w.addDocument(myDocument);
		}
		// close all handles
		w.close();
	}

	public void searchIndex(Directory index, String searchTerm) throws Exception {
		// create an analyzer
		KeywordAnalyzer analyzer = new KeywordAnalyzer();

		// create a topScore Collection and display the results
		int hitsPerPage = 10;
		IndexReader reader = IndexReader.open(index);

		IndexSearcher searcher = new IndexSearcher(reader);
		TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
		QueryParser parser = new QueryParser(Version.LUCENE_36, "myString", analyzer);
		Query q = parser.parse(searchTerm);
		searcher.search(q, collector);
		ScoreDoc[] hits = collector.topDocs().scoreDocs;

		System.out.println("Found " + hits.length + " hits.");
		for (int i = 0; i < hits.length; ++i) {
			int docId = hits[i].doc;
			Document d = searcher.doc(docId);
			System.out.println((i + 1) + ". " + "Line " + d.get("id") + " told by " + d.get("teller") + ": "
					+ d.get("myString"));
		}
		reader.close();
	}

	public static void main(String[] args) {

		PrettyLucene prettyLucene = new PrettyLucene();
		try {
			// makes a directory luceneIndex where you start the program. In this folder
			// lucene will put the index files
			Directory index = new SimpleFSDirectory(new File("luceneIndex"));

			List<String> marcellus = new ArrayList<String>();
			marcellus.add("Horatio says 'tis but our fantasy,");
			marcellus.add("And will not let belief take hold of him");
			marcellus.add("Touching this dreaded sight, twice seen of us:");
			marcellus.add("Therefore I have entreated him along");
			marcellus.add("With us to watch the minutes of this night;");
			marcellus.add("That if again this apparition come,");
			marcellus.add("He may approve our eyes and speak to it.");
			prettyLucene.index(index, marcellus, "Marcellus");

			List<String> horatio = new ArrayList<String>();
			horatio.add("What art thou that usurp'st this time of night,");
			horatio.add("Together with that fair and warlike form");
			horatio.add("In which the majesty of buried Denmark");
			horatio.add("Did sometimes march? by heaven I charge thee, speak!");
			prettyLucene.index(index, horatio, "Horatio");

			prettyLucene.searchIndex(index, "speak*");

			index.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Gradle – 0.9 Breaking Changes

Do you have problems migrating from 0.8 to 0.9 ? This will help you a looot:

http://wiki.gradle.org/display/GRADLE/Gradle+0.9+Breaking+Changes

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Git – Making bare repo from working copy

Suppose “repo” is your working copy and you just wanted to make a bare repo out of it.

cd repos
mv .git repos.git
cd repos.git
git config --bool core.bare true # from another answer
cd ../
mv repos.git ../
cd ../
rm -rf repos/ # or delete using a file manager if you like

If you just want to create a bare repo on the server from your local repo, Ralph posted a nice way to do this:
http://rwehner.wordpress.com/2010/03/01/a-simple-way-to-create-git-repository-on-a-server-machine-connecting-via-ssh/

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Separating Application Logs

You can segment logging output by assigning log4j categories to specific appenders in
the conf/log4j.xml configuration. For example, the following

<appender name="App1Log" class="org.apache.log4j.FileAppender">
      <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"></errorHandler>
      <param name="Append" value="false"/>
      <param name="File" value="${jboss.server.home.dir}/log/app1.log"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
      </layout>
   </appender>

...

   <category name="com.app1">
     <appender-ref ref="App1Log"></appender-ref>
   </category>
   <category name="com.util">
     <appender-ref ref="App1Log"></appender-ref>
   </category>

If you have multiple apps with shared classes/categories, and/or want the JBoss categories to show up in your app log then this approach will not work. There is a new appender filter called TCLFilter (attached) that will be in the 3.2.4 release that can help with this. You add the filter to the appender and specify what deployment url should logging be restricted to. For example, if your app1 deployment was app1.ear, you would use the following additions to the conf/log4j.xml:

<appender name="App1Log" class="org.apache.log4j.FileAppender">
      <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"></errorHandler>
      <param name="Append" value="false"/>
      <param name="File" value="${jboss.server.home.dir}/log/app1.log"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
      </layout>
      <filter class="org.jboss.logging.filter.TCLFilter">
         <param name="AcceptOnMatch" value="true"/>
         <param name="DeployURL" value="app1.ear"/>
      </filter>
   </appender>

...

   <root>
      <appender-ref ref="CONSOLE"></appender-ref>
      <appender-ref ref="FILE"></appender-ref>
      <appender-ref ref="App1Log"></appender-ref>
   </root>

article has been copied from: https://community.jboss.org/wiki/SeparatingApplicationLogs

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Gradle – Filter with property file

And another one from the fantastic Albatros: Reading a property file and make replacements with the properties from the file:

Properties props = new Properties()
props.load(new FileInputStream('myProperty.properties'))

task mapResources(type: Copy) {
            from mappingResources
            into resourcesDir
      include '**'
            filter(ReplaceTokens, tokens: props)
}

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Gradle – Let eclipse task depend on something

Some day it could be that you need to create a resource classpath which is not checked into your repository. Then when you call the eclipse task your classpath is incomplete because for example your src/main/resources is missing. For this case you can create your resources before the eclipse task is executed:

resourcesDir = new File('src/main/resources')
task createResourcesEclipse << {
    resourcesDir.mkdirs()
}

tasks.eclipse.dependsOn(createResourcesEclipse)

many thanks again to Albatros!!!

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Gradle – Jar Manifest with Dependencies

Here is the way to add all your compile dependencies into your manifest. The dependant jars must be in the same location as the jar itself:

manifest.mainAttributes("Main-Class" : "ch.prait.MyMainClass")
manifest.mainAttributes("Implementation-Title": "Gallery", "Implementation-Version": version)

afterEvaluate {
	Collection classpath = configurations.compile.collect {"$it.name" }
 	manifest.mainAttributes("Class-Path" : classpath.join(' '))
}

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Gradle – War Task: Add files from WebContent

If you inlcuded some files like an index.html into your WebContent Directory and you want to have them deployed in your war file. Add the follwing to your gradle build file:

webAppDirName = 'WebContent'

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Gradle – Replace Tokens

Sometimes it might happen that you want to replace some placeholders in your properties. You could do this by using a ReplaceTokens filter in your processResources step:

processResources  {
      filter ReplaceTokens, tokens: [
            timestamp: new Date().dateTimeString,
            LOGFILE: "/var/logdir/mylog.log",
            MYWSURL: "http://localhost:8010/?wsdl"
      ]
}

The token on the left side of the : will be replaced by the value on the right side.

Tanks a lot to Albatros

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Tomcat – Mime Types for x Office Extensions

If you add in a web app on tomcat (version < 7) an office file with the extensen xlsx, docx or pptx the browser will recognize the file as a zip (what it really is) and not as Office document. You can change that by adding the right mime type to the file tomcat/conf/web.xml:

    <mime-mapping>
        <extension>xlsx</extension>
        <mime-type>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>docx</extension>
        <mime-type>application/vnd.openxmlformats-officedocument.wordprocessingml.document</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>pptx</extension>
        <mime-type>application/vnd.openxmlformats-officedocument.presentationml.presentation</mime-type>
    </mime-mapping>

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS