Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
FastVectorHighlighter.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using System.Collections.Generic;
20 using System.Text;
21 
22 using Lucene.Net.Index;
23 
24 namespace Lucene.Net.Search.Vectorhighlight
25 {
26  public class FastVectorHighlighter
27  {
28 
29  public static bool DEFAULT_PHRASE_HIGHLIGHT = true;
30  public static bool DEFAULT_FIELD_MATCH = true;
31  private bool phraseHighlight;
32  private bool fieldMatch;
33  private FragListBuilder fragListBuilder;
34  private FragmentsBuilder fragmentsBuilder;
35  private int phraseLimit = Int32.MaxValue;
36 
37  /// <summary>
38  /// the default constructor.
39  /// </summary>
40  public FastVectorHighlighter():this(DEFAULT_PHRASE_HIGHLIGHT, DEFAULT_FIELD_MATCH)
41  {
42  }
43 
44  /// <summary>
45  /// a constructor. Using SimpleFragListBuilder and ScoreOrderFragmentsBuilder.
46  /// </summary>
47  /// <param name="phraseHighlight">true or false for phrase highlighting</param>
48  /// <param name="fieldMatch">true of false for field matching</param>
49  public FastVectorHighlighter(bool phraseHighlight, bool fieldMatch):this(phraseHighlight, fieldMatch, new SimpleFragListBuilder(), new ScoreOrderFragmentsBuilder())
50  {
51  }
52 
53  /// <summary>
54  /// a constructor. A FragListBuilder and a FragmentsBuilder can be specified (plugins).
55  /// </summary>
56  /// <param name="phraseHighlight">true of false for phrase highlighting</param>
57  /// <param name="fieldMatch">true of false for field matching</param>
58  /// <param name="fragListBuilder">an instance of FragListBuilder</param>
59  /// <param name="fragmentsBuilder">an instance of FragmentsBuilder</param>
60  public FastVectorHighlighter(bool phraseHighlight, bool fieldMatch,
61  FragListBuilder fragListBuilder, FragmentsBuilder fragmentsBuilder)
62  {
63  this.phraseHighlight = phraseHighlight;
64  this.fieldMatch = fieldMatch;
65  this.fragListBuilder = fragListBuilder;
66  this.fragmentsBuilder = fragmentsBuilder;
67  }
68 
69  /// <summary>
70  /// create a FieldQuery object.
71  /// </summary>
72  /// <param name="query">a query</param>
73  /// <returns>the created FieldQuery object</returns>
74  public FieldQuery GetFieldQuery(Query query)
75  {
76  return new FieldQuery(query, phraseHighlight, fieldMatch);
77  }
78 
79 
80  /// <summary>
81  /// return the best fragment.
82  /// </summary>
83  /// <param name="fieldQuery">FieldQuery object</param>
84  /// <param name="reader">IndexReader of the index</param>
85  /// <param name="docId">document id to be highlighted</param>
86  /// <param name="fieldName">field of the document to be highlighted</param>
87  /// <param name="fragCharSize">the length (number of chars) of a fragment</param>
88  /// <returns>the best fragment (snippet) string</returns>
89  public String GetBestFragment(FieldQuery fieldQuery, IndexReader reader, int docId,
90  String fieldName, int fragCharSize)
91  {
92  FieldFragList fieldFragList = GetFieldFragList(fieldQuery, reader, docId, fieldName, fragCharSize);
93  return fragmentsBuilder.CreateFragment(reader, docId, fieldName, fieldFragList);
94  }
95 
96  /// <summary>
97  /// return the best fragments.
98  /// </summary>
99  /// <param name="fieldQuery">FieldQuery object</param>
100  /// <param name="reader">IndexReader of the index</param>
101  /// <param name="docId">document id to be highlighted</param>
102  /// <param name="fieldName">field of the document to be highlighted</param>
103  /// <param name="fragCharSize">the length (number of chars) of a fragment</param>
104  /// <param name="maxNumFragments">maximum number of fragments</param>
105  /// <returns>created fragments or null when no fragments created. Size of the array can be less than maxNumFragments</returns>
106  public String[] GetBestFragments(FieldQuery fieldQuery, IndexReader reader, int docId,
107  String fieldName, int fragCharSize, int maxNumFragments)
108  {
109  FieldFragList fieldFragList = GetFieldFragList(fieldQuery, reader, docId, fieldName, fragCharSize);
110  return fragmentsBuilder.CreateFragments(reader, docId, fieldName, fieldFragList, maxNumFragments);
111  }
112 
113  private FieldFragList GetFieldFragList(FieldQuery fieldQuery, IndexReader reader, int docId,
114  String fieldName, int fragCharSize)
115  {
116  FieldTermStack fieldTermStack = new FieldTermStack(reader, docId, fieldName, fieldQuery);
117  FieldPhraseList fieldPhraseList = new FieldPhraseList(fieldTermStack, fieldQuery, phraseLimit);
118  return fragListBuilder.CreateFieldFragList(fieldPhraseList, fragCharSize);
119  }
120 
121  /// <summary>
122  /// return whether phraseHighlight or not.
123  /// </summary>
124  /// <returns>return whether phraseHighlight or not.</returns>
125  public bool IsPhraseHighlight()
126  {
127  return phraseHighlight;
128  }
129 
130  /// <summary>
131  /// return whether fieldMatch or not.
132  /// </summary>
133  /// <returns>return whether fieldMatch or not.</returns>
134  public bool IsFieldMatch()
135  {
136  return fieldMatch;
137  }
138 
139  /// <summary>
140  /// The maximum number of phrases to analyze when searching for the highest-scoring phrase.
141  /// The default is 5000. To ensure that all phrases are analyzed, use a negative number or Integer.MAX_VALUE.
142  /// </summary>
143 
144  public int PhraseLimit
145  {
146  get{ return phraseLimit; }
147  set{ this.phraseLimit = value; }
148  }
149  }
150 }