/* * 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. */ using System; using System.Collections.Generic; using System.Text; using Lucene.Net.Index; namespace Lucene.Net.Search.Vectorhighlight { public class FastVectorHighlighter { public static bool DEFAULT_PHRASE_HIGHLIGHT = true; public static bool DEFAULT_FIELD_MATCH = true; private bool phraseHighlight; private bool fieldMatch; private FragListBuilder fragListBuilder; private FragmentsBuilder fragmentsBuilder; private int phraseLimit = Int32.MaxValue; /// /// the default constructor. /// public FastVectorHighlighter():this(DEFAULT_PHRASE_HIGHLIGHT, DEFAULT_FIELD_MATCH) { } /// /// a constructor. Using SimpleFragListBuilder and ScoreOrderFragmentsBuilder. /// /// true or false for phrase highlighting /// true of false for field matching public FastVectorHighlighter(bool phraseHighlight, bool fieldMatch):this(phraseHighlight, fieldMatch, new SimpleFragListBuilder(), new ScoreOrderFragmentsBuilder()) { } /// /// a constructor. A FragListBuilder and a FragmentsBuilder can be specified (plugins). /// /// true of false for phrase highlighting /// true of false for field matching /// an instance of FragListBuilder /// an instance of FragmentsBuilder public FastVectorHighlighter(bool phraseHighlight, bool fieldMatch, FragListBuilder fragListBuilder, FragmentsBuilder fragmentsBuilder) { this.phraseHighlight = phraseHighlight; this.fieldMatch = fieldMatch; this.fragListBuilder = fragListBuilder; this.fragmentsBuilder = fragmentsBuilder; } /// /// create a FieldQuery object. /// /// a query /// the created FieldQuery object public FieldQuery GetFieldQuery(Query query) { return new FieldQuery(query, phraseHighlight, fieldMatch); } /// /// return the best fragment. /// /// FieldQuery object /// IndexReader of the index /// document id to be highlighted /// field of the document to be highlighted /// the length (number of chars) of a fragment /// the best fragment (snippet) string public String GetBestFragment(FieldQuery fieldQuery, IndexReader reader, int docId, String fieldName, int fragCharSize) { FieldFragList fieldFragList = GetFieldFragList(fieldQuery, reader, docId, fieldName, fragCharSize); return fragmentsBuilder.CreateFragment(reader, docId, fieldName, fieldFragList); } /// /// return the best fragments. /// /// FieldQuery object /// IndexReader of the index /// document id to be highlighted /// field of the document to be highlighted /// the length (number of chars) of a fragment /// maximum number of fragments /// created fragments or null when no fragments created. Size of the array can be less than maxNumFragments public String[] GetBestFragments(FieldQuery fieldQuery, IndexReader reader, int docId, String fieldName, int fragCharSize, int maxNumFragments) { FieldFragList fieldFragList = GetFieldFragList(fieldQuery, reader, docId, fieldName, fragCharSize); return fragmentsBuilder.CreateFragments(reader, docId, fieldName, fieldFragList, maxNumFragments); } private FieldFragList GetFieldFragList(FieldQuery fieldQuery, IndexReader reader, int docId, String fieldName, int fragCharSize) { FieldTermStack fieldTermStack = new FieldTermStack(reader, docId, fieldName, fieldQuery); FieldPhraseList fieldPhraseList = new FieldPhraseList(fieldTermStack, fieldQuery, phraseLimit); return fragListBuilder.CreateFieldFragList(fieldPhraseList, fragCharSize); } /// /// return whether phraseHighlight or not. /// /// return whether phraseHighlight or not. public bool IsPhraseHighlight() { return phraseHighlight; } /// /// return whether fieldMatch or not. /// /// return whether fieldMatch or not. public bool IsFieldMatch() { return fieldMatch; } /// /// The maximum number of phrases to analyze when searching for the highest-scoring phrase. /// The default is 5000. To ensure that all phrases are analyzed, use a negative number or Integer.MAX_VALUE. /// public int PhraseLimit { get{ return phraseLimit; } set{ this.phraseLimit = value; } } } }