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.QueryParser;
21 import org.apache.lucene.search.Hits;
22 import org.apache.lucene.search.IndexSearcher;
23 import org.apache.lucene.search.Query;
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.artifact.factory.ArtifactFactory;
26 import org.apache.maven.artifact.repository.ArtifactRepository;
28 import java.util.ArrayList;
29 import java.util.List;
33 * This class searches the index for existing artifacts that contains the
34 * specified query string.
36 public class ArtifactRepositoryIndexSearcher
37 implements RepositoryIndexSearcher
39 private static final String NAME = "name";
41 private static final String GROUPID = "groupId";
43 private static final String ARTIFACTID = "artifactId";
45 private static final String VERSION = "version";
47 private IndexSearcher searcher;
49 private ArtifactRepository repository;
51 private ArtifactFactory factory;
54 * Search the artifact that contains the query string in the specified
61 public List search( RepositoryIndex index, String queryString, String searchField )
63 List artifactList = new ArrayList();
67 searcher = new IndexSearcher( index.getIndexPath() );
68 QueryParser parser = new QueryParser( searchField, index.getAnalyzer() );
69 Query qry = parser.parse( queryString );
70 Hits hits = searcher.search( qry );
71 //System.out.println("HITS SIZE --> " + hits.length());
73 for ( int i = 0; i < hits.length(); i++ )
75 Document doc = hits.doc( i );
77 String groupId = doc.get( GROUPID );
78 String artifactId = doc.get( ARTIFACTID );
79 String version = doc.get( VERSION );
80 String name = doc.get( NAME );
81 String packaging = name.substring( name.lastIndexOf( '.' ) + 1 );
82 Artifact artifact = factory.createBuildArtifact( groupId, artifactId, version, packaging );
84 artifactList.add( artifact );