• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Special Characters Lucene

 
Ranch Hand
Posts: 87
Hibernate Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When I tried to do a lucene search using escape character with other
special character like the following:



public class PharserQuery {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException, ParseException {
StandardAnalyzer analyzer = new StandardAnalyzer();
Directory index = new RAMDirectory();

IndexWriter w = new IndexWriter(index, analyzer, true,
IndexWriter.MaxFieldLength.UNLIMITED);
addDoc(w, "Lucene in Act^ion");
addDoc(w, "Lucene lucene Act:ion");
addDoc(w, "Managing Act?ion");
addDoc(w, "The Art of Computer Act-ion");
addDoc(w, "Lucene");

w.close();

// 2. query

String querystr = args.length > 0 ? args[0] : "Act-ion";
querystr = querystr.toLowerCase();
String parserQueryStr = "";
Query query;
IndexSearcher searcher = new IndexSearcher(index, true);
//if (queryStr.IndexOf())
// Hits hits;
if (querystr.indexOf("*") < 0) {
String escaped = QueryParser.escape(querystr);

QueryParser parser = new QueryParser("title", analyzer);
query = parser.parse(querystr);
//query = parser.parse(escaped);
} else {
Term term = new Term("title", querystr);
query = new WildcardQuery(term);
System.out.println("qury : " + query);
// Hits hits = indexSearcher.search(query);
}

/*
* System.out.println("Query: " + query.toString()); hits =
* searcher.search(query); System.out.println("Found " + hits.length() +
* " hits.");
*/

int hitsPerPage = 10;

TopScoreDocCollector collector = TopScoreDocCollector.create(
hitsPerPage, true);
searcher.search(query, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
System.out.println("ScoreDoc[] hits: " + hits);

// 4. display results
System.out.println("Found " + hits.length + " hits.");
for (int i = 0; i < hits.length; ++i) {
int docId = hits[i].doc;
Document d = searcher.doc(docId);
System.out.println((i + 1) + ". " + d.get("title"));
}

// searcher can only be closed when there
// is no need to access the documents any more.

searcher.close();
}

private static void addDoc(IndexWriter w, String value) throws IOException {
Document doc = new Document();
doc
.add(new Field("title", value, Field.Store.YES,
Field.Index.ANALYZED, Field.TermVector.YES));

w.addDocument(doc);
}

}

when I search
Act-ion both with escape and without ( just parsing as it is) is displaying all the 4 hits.
Act^ion : Error wihout escape and with escape all the four. why is it not able to parse ^ as it is same as all the other special charactes.
Act?ion and Act:ion: 0 hits without escape and all the 4 with escape

Could anyone let me know how should i get the exact serch ie.if i try "Act-ion" then only 4th one should be displayed and similarly all others.

thanks
Smitha
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!