lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ian Lea <ian....@gmail.com>
Subject Re: How to get file names instead of paths?
Date Fri, 11 Jun 2010 10:30:11 GMT
Something like this

File f = new File(path);
String fn = f.getName();
return fn.substring(0, fn.lastIndexOf("."));


--
Ian.


On Fri, Jun 11, 2010 at 11:20 AM, manjula wijewickrema
<manjula53@gmail.com> wrote:
> Hi,
>
> Using the following programme I was able to get the entire file path of
> indexed files which matched with the given queries. But my intention is to
> get only the file names even without .txt extention as I need to send these
> file names as labels to another application. So, pls. let me know how can I
> get only the file names in the following code.
>
> Thanx in advance!
> Manjula.
>
>
> My code:
>
> *
>
> public* *class* LuceneDemo {
>
> *public* *static* *final* String *FILES_TO_INDEX_DIRECTORY* = "filesToIndex"
> ;
>
> *public* *static* *final* String *INDEX_DIRECTORY* = "indexDirectory";
>
> *public* *static* *final* String *FIELD_PATH* = "path";
>
> *public* *static* *final* String *FIELD_CONTENTS* = "contents";
>
> *public* *static* *void* main(String[] args) *throws* Exception {
>
> *createIndex*();
>
> *searchIndex*("rice");
>
> *searchIndex*("milk");
>
> *searchIndex*("banana");
>
> *searchIndex*("foo");
>
>  }
>
> *public* *static* *void* createIndex() *throws* CorruptIndexException,
> LockObtainFailedException, IOException {
>
>  SnowballAnalyzer analyzer = *new* SnowballAnalyzer( "English",
> StopAnalyzer.ENGLISH_STOP_WORDS);
>
> *boolean* recreateIndexIfExists = *true*;
>
> IndexWriter indexWriter = *new* IndexWriter(*INDEX_DIRECTORY*, analyzer,
> recreateIndexIfExists);
>
> File dir = *new* File(*FILES_TO_INDEX_DIRECTORY*);
>
> File[] files = dir.listFiles();
>
> *for* (File file : files) {
>
> Document document = *new* Document();
>
> String path = file.getCanonicalPath();
>
> document.add(*new* Field(*FIELD_PATH*, path, Field.Store.*YES*, Field.Index.
> UN_TOKENIZED,Field.TermVector.*YES*));
>
> Reader reader = *new* FileReader(file);
>
> document.add(*new* Field(*FIELD_CONTENTS*, reader));
>
> indexWriter.addDocument(document);
>
>  }
>
> indexWriter.optimize();
>
> indexWriter.close();
>
> }
>
> *public* *static* *void* searchIndex(String searchString)
> *throws*IOException, ParseException {
>
> System.*out*.println("Searching for '" + searchString + "'");
>
> Directory directory = FSDirectory.getDirectory(*INDEX_DIRECTORY*);
>
> IndexReader indexReader = IndexReader.open(directory);
>
> IndexSearcher indexSearcher = *new* IndexSearcher(indexReader);
>
>  SnowballAnalyzer analyzer = *new* SnowballAnalyzer( "English",
> StopAnalyzer.ENGLISH_STOP_WORDS);
>
> QueryParser queryParser = *new* QueryParser(*FIELD_CONTENTS*, analyzer);
>
> Query query = queryParser.parse(searchString);
>
> Hits hits = indexSearcher.search(query);
>
> System.*out*.println("Number of hits: " + hits.length());
>
> TopDocs results = indexSearcher.search(query,10);
>
> ScoreDoc[] hits1 = results.scoreDocs;
>
> *for* (ScoreDoc hit : hits1) {
>
> Document doc = indexSearcher.doc(hit.doc);
>
> System.*out*.printf("%5.3f %s\n",hit.score,doc.get(*FIELD_CONTENTS*));
>
> }
>
>  Iterator<Hit> it = hits.iterator();
>
> *while* (it.hasNext()) {
>
> Hit hit = it.next();
>
> Document document = hit.getDocument();
>
> String path = document.get(*FIELD_PATH*);
>
> System.*out*.println("Hit: " + path);
>
> }
>
> }
>
> }
>

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message