From 86a4215d76f9d15ef1867ff77c20c6eaf8eb8881 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Wed, 23 Jan 2013 09:35:53 +0000 Subject: [PATCH] add simple FVH test git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1437308 13f79535-47bb-0310-9956-ffa450edef68 --- .../FastVectorHighlighterTest.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FastVectorHighlighterTest.java diff --git a/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FastVectorHighlighterTest.java b/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FastVectorHighlighterTest.java new file mode 100644 index 00000000000..44e8b06a2be --- /dev/null +++ b/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FastVectorHighlighterTest.java @@ -0,0 +1,65 @@ +package org.apache.lucene.search.vectorhighlight; +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import java.io.IOException; + +import org.apache.lucene.analysis.MockAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; +import org.apache.lucene.document.TextField; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.TermQuery; +import org.apache.lucene.store.Directory; +import org.apache.lucene.util.LuceneTestCase; + + +public class FastVectorHighlighterTest extends LuceneTestCase { + + public void testSimpleHighlightTest() throws IOException { + Directory dir = newDirectory(); + IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()))); + Document doc = new Document(); + FieldType type = new FieldType(TextField.TYPE_STORED); + type.setStoreTermVectorOffsets(true); + type.setStoreTermVectorPositions(true); + type.setStoreTermVectors(true); + type.freeze(); + Field field = new Field("field", "This is a test where foo is highlighed and should be highlighted", type); + + doc.add(field); + writer.addDocument(doc); + FastVectorHighlighter highlighter = new FastVectorHighlighter(); + + IndexReader reader = DirectoryReader.open(writer, true); + int docId = 0; + FieldQuery fieldQuery = highlighter.getFieldQuery( new TermQuery(new Term("field", "foo")), reader ); + String[] bestFragments = highlighter.getBestFragments(fieldQuery, reader, docId, "field", 54, 1); + // highlighted results are centered + assertEquals("This is a test where foo is highlighed and should be highlighted", bestFragments[0]); + bestFragments = highlighter.getBestFragments(fieldQuery, reader, docId, "field", 52, 1); + assertEquals("This is a test where foo is highlighed and should be", bestFragments[0]); + bestFragments = highlighter.getBestFragments(fieldQuery, reader, docId, "field", 30, 1); + assertEquals("a test where foo is highlighed", bestFragments[0]); + reader.close(); + writer.close(); + dir.close(); + } +}