NOT operator doesn't work in query lucene -
i use lucene version 3.0.3.0, expression search, doesn't work properly. example if search "!fiesta or astra" on field "model", "vauxhallastra" returned , "fordfocus" not returned. code below:
var fordfiesta = new document(); fordfiesta.add(new field("id", "1", field.store.yes, field.index.not_analyzed)); fordfiesta.add(new field("make", "ford", field.store.yes, field.index.analyzed)); fordfiesta.add(new field("model", "fiesta", field.store.yes, field.index.analyzed)); var fordfocus = new document(); fordfocus.add(new field("id", "2", field.store.yes, field.index.not_analyzed)); fordfocus.add(new field("make", "ford", field.store.yes, field.index.analyzed)); fordfocus.add(new field("model", "focus", field.store.yes, field.index.analyzed)); var vauxhallastra = new document(); vauxhallastra.add(new field("id", "3", field.store.yes, field.index.not_analyzed)); vauxhallastra.add(new field("make", "vauxhall", field.store.yes, field.index.analyzed)); vauxhallastra.add(new field("model", "astra", field.store.yes, field.index.analyzed)); directory directory = fsdirectory.open(new directoryinfo(environment.currentdirectory + "\\luceneindex")); analyzer analyzer = new standardanalyzer(version.lucene_30); var writer = new indexwriter(directory, analyzer, true, indexwriter.maxfieldlength.limited); writer.adddocument(fordfiesta); writer.adddocument(fordfocus); writer.adddocument(vauxhallastra); writer.optimize(); writer.close(); indexreader indexreader = indexreader.open(directory, true); searcher indexsearch = new indexsearcher(indexreader); var queryparser = new queryparser(version.lucene_30, "model", analyzer); var query = queryparser.parse("!fiesta or astra"); console.writeline("searching for: " + query.tostring()); topdocs resultdocs = indexsearch.search(query, 200); console.writeline("results found: " + resultdocs.maxscore); var hits = resultdocs.scoredocs; foreach (var hit in hits) { var documentfromsearcher = indexsearch.doc(hit.doc); console.writeline(documentfromsearcher.get("make") + " " + documentfromsearcher.get("model")); } indexsearch.close(); directory.close(); console.readkey();
!fiesta or astra
doesn't mean think means. !fiesta
portion not mean, "get except fiesta", might expect, rather more "forbid fiesta". not
term in lucene query filters out results, not find anything.
the query have defined fetch results astra
. containing astra found, fiesta
filtered out.
in order perform query believe expecting, need like:
astra or (*:* !fiesta)
*:*
matchalldocsquery
. since need match documents perform sort of query, can expected perform poorly.
confusing interpretation of "boolean" logic why don't and/or/not syntax lucene. +/- clearer, more powerful, , doesn't introduce oddball gotchas this.
this excellent article on topic clarifies why should thinking in terms of must
/must_not
/should
, rather traditional boolean logic.
Comments
Post a Comment