lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Christian Kaufhold" <>
Subject synonyms
Date Tue, 25 Jul 2017 06:52:34 GMT

I am not able to add synonyms to the lucene index.
I condensed my problem into the following class which is based on a Hello World example.
The idea behind the code was to add a document with universität and the synonym 'Hochschule'
so that lucene finds universität wenn I query Hochschule.
But it doesn't and I checked the index contents with a term iterator.
Hochschule is not added to the index:

package test;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.core.LowerCaseFilter;
import org.apache.lucene.analysis.standard.ClassicTokenizer;
import org.apache.lucene.analysis.standard.StandardFilter;
import org.apache.lucene.analysis.synonym.SynonymFilter;
import org.apache.lucene.analysis.synonym.SynonymMap;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.CharsRef;
import org.apache.lucene.util.Version;


public class LuceneHelloWorld {

    public static void main(String[] args) throws Exception {

        Analyzer analyzer = getAnalyzer();
        Directory directory = new RAMDirectory();
        IndexWriterConfig config = new IndexWriterConfig( Version.LUCENE_48, analyzer);

        IndexWriter writer = new IndexWriter(directory, config);
        addDoc( writer, "people", "Hello Universität" );
        addDoc( writer, "world", "Hello World" );
        addDoc( writer, "people", "Hello people" );

        IndexReader reader =;
        IndexSearcher searcher = new IndexSearcher (reader);
        QueryParser parser = new QueryParser ( Version.LUCENE_48, "content", analyzer);

        test( parser, searcher, "Hochschule");
        test( parser, searcher, "Hello");
        test( parser, searcher, "people");
        test( parser, searcher, "universität");

        printIndexTerms( reader);


    public static void addDoc( IndexWriter writer, String title, String content) throws Exception
        Document document = new Document ();
        document.add(new TextField("title", title, Field.Store.YES));
        document.add(new TextField("content", content, Field.Store.YES));

    public static Analyzer getAnalyzer(){
        //return new StandardAnalyzer(Version.LUCENE_48);
        //return new SynonymAnalyzer();
        //return new SynonymFromStandardAnalyzer(Version.LUCENE_48);
        return new Analyzer() {
            protected TokenStreamComponents createComponents(String fieldName, Reader reader)
                // TODO Auto-generated method stub
                Tokenizer source = new ClassicTokenizer(Version.LUCENE_48, reader);
                TokenStream filter = new StandardFilter(Version.LUCENE_48, source);
                filter = new LowerCaseFilter(Version.LUCENE_48,filter);
                SynonymMap mySynonymMap = null;
                try {
                    //mySynonymMap = buildSynonym();
                    SynonymMap.Builder builder = new SynonymMap.Builder(true);
                    builder.add(new CharsRef("Hochschule"), new CharsRef("Universität"),
                    builder.add(new CharsRef("Universität"), new CharsRef("Hochschule"),
                    mySynonymMap =;
                } catch (IOException e) {
                    // TODO Auto-generated catch bl               filter = new SynonymFilter(filter,
mySynonymMap, false);
                return new TokenStreamComponents(source, filter);

    static void test( QueryParser parser, IndexSearcher searcher, String queryStr) throws
Exception {
        Query query = parser.parse(queryStr);
        TopDocs results =, 10);
        System.out.println(  "#Hits: " + results.totalHits + " : " + queryStr);


    public static void printIndexTerms( IndexReader reader) throws Exception {
        Terms terms = SlowCompositeReaderWrapper.wrap(reader).terms("content");
        TermsEnum iterator =terms.iterator(TermsEnum.EMPTY);
        BytesRef byteRef;
        while ( (byteRef =!=null){
            String term = byteRef.utf8ToString();
            System.out.println("  term: " + term);


#Hits: 0 : Hochschule
#Hits: 3 : Hello
#Hits: 1 : people
#Hits: 1 : universität
  term: hello
  term: people
  term: universität
  term: world

thanks in advance

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message