HelpSearch.java (57c10a96) HelpSearch.java (7fb4469b)
1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance

--- 26 unchanged lines hidden (view full) ---

35import com.sun.star.uno.AnyConverter;
36
37import org.apache.lucene.analysis.Analyzer;
38import org.apache.lucene.analysis.standard.StandardAnalyzer;
39import org.apache.lucene.analysis.cjk.CJKAnalyzer;
40import org.apache.lucene.document.Document;
41import org.apache.lucene.index.IndexReader;
42import org.apache.lucene.index.Term;
1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance

--- 26 unchanged lines hidden (view full) ---

35import com.sun.star.uno.AnyConverter;
36
37import org.apache.lucene.analysis.Analyzer;
38import org.apache.lucene.analysis.standard.StandardAnalyzer;
39import org.apache.lucene.analysis.cjk.CJKAnalyzer;
40import org.apache.lucene.document.Document;
41import org.apache.lucene.index.IndexReader;
42import org.apache.lucene.index.Term;
43import org.apache.lucene.search.Hits;
43import org.apache.lucene.search.TopDocs;
44import org.apache.lucene.search.IndexSearcher;
45import org.apache.lucene.search.Query;
46import org.apache.lucene.search.Searcher;
44import org.apache.lucene.search.IndexSearcher;
45import org.apache.lucene.search.Query;
46import org.apache.lucene.search.Searcher;
47import org.apache.lucene.search.ScoreDoc;
47import org.apache.lucene.search.TermQuery;
48import org.apache.lucene.search.WildcardQuery;
48import org.apache.lucene.search.TermQuery;
49import org.apache.lucene.search.WildcardQuery;
50import org.apache.lucene.util.Version;
51import org.apache.lucene.store.NIOFSDirectory;
49
50import com.sun.star.script.XInvocation;
51import com.sun.star.beans.XIntrospectionAccess;
52
52
53import com.sun.star.script.XInvocation;
54import com.sun.star.beans.XIntrospectionAccess;
55
56import java.io.File;
57
53/** This class capsulates the class, that implements the minimal component and a
54 * factory for creating the service (<CODE>__getComponentFactory</CODE>).
55 */
56public class HelpSearch
57{
58 /** This class implements the component. At least the interfaces XServiceInfo,
59 * XTypeProvider, and XInitialization should be provided by the service.
60 */

--- 183 unchanged lines hidden (view full) ---

244 String[] aDocs = queryImpl( aLanguageStr, aIndexStr, aQueryStr, bCaptionOnly, aScoreOutArray );
245
246 return aDocs;
247 }
248
249 private static String[] queryImpl( String aLanguageStr, String aIndexStr, String aQueryStr,
250 boolean bCaptionOnly, Object[] aScoreOutArray ) throws Exception
251 {
58/** This class capsulates the class, that implements the minimal component and a
59 * factory for creating the service (<CODE>__getComponentFactory</CODE>).
60 */
61public class HelpSearch
62{
63 /** This class implements the component. At least the interfaces XServiceInfo,
64 * XTypeProvider, and XInitialization should be provided by the service.
65 */

--- 183 unchanged lines hidden (view full) ---

249 String[] aDocs = queryImpl( aLanguageStr, aIndexStr, aQueryStr, bCaptionOnly, aScoreOutArray );
250
251 return aDocs;
252 }
253
254 private static String[] queryImpl( String aLanguageStr, String aIndexStr, String aQueryStr,
255 boolean bCaptionOnly, Object[] aScoreOutArray ) throws Exception
256 {
252 IndexReader reader = IndexReader.open( aIndexStr );
257 File aIndexFile = new File( aIndexStr );
258 IndexReader reader = IndexReader.open( NIOFSDirectory.open( aIndexFile ), true );
253 Searcher searcher = new IndexSearcher( reader );
259 Searcher searcher = new IndexSearcher( reader );
254 Analyzer analyzer = aLanguageStr.equals("ja") ? (Analyzer)new CJKAnalyzer() : (Analyzer)new StandardAnalyzer();
260 Analyzer analyzer = aLanguageStr.equals("ja") ? (Analyzer)new CJKAnalyzer(Version.LUCENE_29) : (Analyzer)new StandardAnalyzer(Version.LUCENE_29);
255
256 String aField;
257 if( bCaptionOnly )
258 aField = "caption";
259 else
260 aField = "content";
261
262 Query aQuery;
263 if( aQueryStr.endsWith( "*" ) )
264 aQuery = new WildcardQuery( new Term( aField, aQueryStr ) );
265 else
266 aQuery = new TermQuery( new Term( aField, aQueryStr ) );
267
268 // Perform search
261
262 String aField;
263 if( bCaptionOnly )
264 aField = "caption";
265 else
266 aField = "content";
267
268 Query aQuery;
269 if( aQueryStr.endsWith( "*" ) )
270 aQuery = new WildcardQuery( new Term( aField, aQueryStr ) );
271 else
272 aQuery = new TermQuery( new Term( aField, aQueryStr ) );
273
274 // Perform search
269 Hits aHits = searcher.search( aQuery );
270 int nHitCount = aHits.length();
275 TopDocs aHits = searcher.search( aQuery, 100 );
276 int nHitCount = aHits.totalHits;
271
272 String aDocs[] = new String[nHitCount];
273 float aScores[] = null;
274 aScores = new float[nHitCount];
275 for( int iHit = 0 ; iHit < nHitCount ; iHit++ )
276 {
277
278 String aDocs[] = new String[nHitCount];
279 float aScores[] = null;
280 aScores = new float[nHitCount];
281 for( int iHit = 0 ; iHit < nHitCount ; iHit++ )
282 {
277 Document aDoc = aHits.doc( iHit );
278 String aPath = aDoc.get( "path" );
283 ScoreDoc aDoc = aHits.scoreDocs[iHit];
284 String aPath = searcher.doc(aDoc.doc).get( "path" );
279 aDocs[iHit] = ( aPath != null ) ? aPath : "";
285 aDocs[iHit] = ( aPath != null ) ? aPath : "";
280 aScores[iHit] = aHits.score( iHit );
286 aScores[iHit] = aDoc.score;
281 }
282 aScoreOutArray[0] = aScores;
283
284 reader.close();
285
286 return aDocs;
287 }
288 }

--- 37 unchanged lines hidden ---
287 }
288 aScoreOutArray[0] = aScores;
289
290 reader.close();
291
292 return aDocs;
293 }
294 }

--- 37 unchanged lines hidden ---