1 package org.apache.maven.repository.indexing;
4 * Copyright 2001-2005 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
25 import org.apache.lucene.analysis.Analyzer;
26 import org.apache.lucene.analysis.PerFieldAnalyzerWrapper;
27 import org.apache.lucene.analysis.SimpleAnalyzer;
28 import org.apache.lucene.analysis.StopAnalyzer;
29 import org.apache.lucene.analysis.standard.StandardAnalyzer;
30 import org.apache.lucene.document.Document;
31 import org.apache.lucene.queryParser.ParseException;
32 import org.apache.lucene.queryParser.QueryParser;
33 import org.apache.lucene.search.Hits;
34 import org.apache.lucene.search.IndexSearcher;
35 import org.apache.lucene.search.Query;
36 import org.apache.maven.artifact.Artifact;
37 import org.apache.maven.artifact.DefaultArtifact;
38 import org.apache.maven.artifact.factory.ArtifactFactory;
39 import org.apache.maven.artifact.factory.DefaultArtifactFactory;
40 import org.apache.maven.artifact.handler.ArtifactHandler;
41 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
42 import org.apache.maven.artifact.repository.ArtifactRepository;
43 import org.apache.maven.artifact.versioning.VersionRange;
46 * This class searches the index for existing artifacts that contains the
47 * specified query string.
50 public class ArtifactRepositoryIndexSearcher
51 implements RepositoryIndexSearcher
53 private static final String NAME = "name";
54 private static final String GROUPID = "groupId";
55 private static final String ARTIFACTID = "artifactId";
56 private static final String VERSION = "version";
58 private IndexSearcher searcher;
59 private ArtifactRepository repository;
60 private ArtifactFactory factory;
63 * Search the artifact that contains the query string in the specified
70 public List search( RepositoryIndex index, String queryString, String searchField )
72 List artifactList = new ArrayList();
75 searcher = new IndexSearcher( index.getIndexPath() );
76 QueryParser parser = new QueryParser( searchField, index.getAnalyzer() );
77 Query qry = parser.parse( queryString );
78 Hits hits = searcher.search( qry );
79 //System.out.println("HITS SIZE --> " + hits.length());
81 for ( int i = 0; i < hits.length(); i++ )
83 Document doc = hits.doc( i );
85 String groupId = doc.get( GROUPID );
86 String artifactId = doc.get( ARTIFACTID );
87 String version = doc.get( VERSION );
88 String name = doc.get( NAME);
89 String packaging = name.substring( name.lastIndexOf( '.' ) + 1 );
90 Artifact artifact = factory.createBuildArtifact( groupId, artifactId, version, packaging );
92 artifactList.add( artifact );