1 package org.apache.maven.repository.indexing;
4 * Copyright 2005-2006 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
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.lucene.document.Document;
20 import org.apache.lucene.queryParser.ParseException;
21 import org.apache.lucene.queryParser.QueryParser;
22 import org.apache.lucene.search.Hits;
23 import org.apache.lucene.search.IndexSearcher;
24 import org.apache.lucene.search.Query;
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.artifact.factory.ArtifactFactory;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
34 * This class searches the index for existing artifacts that contains the
35 * specified query string.
37 * @plexus.component role="org.apache.maven.repository.indexing.RepositoryIndexSearcher" role-hint="artifact"
39 public class ArtifactRepositoryIndexSearcher
40 implements RepositoryIndexSearcher
42 private static final String NAME = "name";
44 private static final String GROUPID = "groupId";
46 private static final String ARTIFACTID = "artifactId";
48 private static final String VERSION = "version";
53 private ArtifactFactory factory;
56 * Search the artifact that contains the query string in the specified
62 public List search( RepositoryIndex index, String queryString, String searchField )
63 throws RepositoryIndexSearchException
65 List artifactList = new ArrayList();
69 IndexSearcher searcher = new IndexSearcher( index.getIndexPath() );
70 QueryParser parser = new QueryParser( searchField, index.getAnalyzer() );
71 Query qry = parser.parse( queryString );
72 Hits hits = searcher.search( qry );
74 for ( int i = 0; i < hits.length(); i++ )
76 Document doc = hits.doc( i );
78 String groupId = doc.get( GROUPID );
79 String artifactId = doc.get( ARTIFACTID );
80 String version = doc.get( VERSION );
81 String name = doc.get( NAME );
82 String packaging = name.substring( name.lastIndexOf( '.' ) + 1 );
83 Artifact artifact = factory.createBuildArtifact( groupId, artifactId, version, packaging );
85 artifactList.add( artifact );
88 catch ( IOException e )
90 throw new RepositoryIndexSearchException( e.getMessage(), e );
92 catch ( ParseException e )
94 throw new RepositoryIndexSearchException( "Error parsing query: " + e.getMessage() );