0

I have observed an odd behaviour but I don't see what I am doing wrong.

I created via multiple BooleanQueries the following query:

+(-(Request.zipCode:18055 Request.zipCode:33333 Request.zipCode:99999) +Request.zipCode:[* TO *]) *:*

...this is what I get via toString

Update: this way I created a part of the BooleanQuery which is responsible to create this snippet +Request.zipCode:[* TO *])

Query fieldOccursQuery = new TermQuery(new Term(queryFieldName, "[* TO *]"));

I have created exaclty same (per my understanding) Query via QueryParser like this:

String querystr = "+(-(Request.zipCode:18055 Request.zipCode:33333 Request.zipCode:99999) +Request.zipCode:[* TO *]) *:*";
Query query = new QueryParser(Version.LUCENE_46, "title", LuceneServiceI.analyzer).parse(querystr);

I processed both of them the same way like this:

IndexReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
int max = reader.maxDoc();
TopScoreDocCollector collector = TopScoreDocCollector.create(max > 0 ? max : 1, true);
searcher.search(query, collector);
....
    ScoreDoc[] hits = collector.topDocs().scoreDocs;
    Map<Integer, Document> docMap = new TreeMap<Integer, Document>();
    for (int i = 0; i < hits.length; i++) {
        docMap.put(hits[i].doc, indexSearcher.doc(hits[i].doc));
    }

Different results

On a index like: stored,indexed,tokenized,omitNorms,indexOptions=DOCS_ONLY<Request.zipCode:04103>

  • The Query via QueryParser deliver one document as expected

  • The Query via BooleanQuery does not deliver 1 expected document

Questions

  • Are there possibilities that both same queries deliver different results? Set certain attributes to my BooleanQuery etc.
  • How can I get the same wanted result for BooleanQuery?
  • I could not found anything about differences only in concern of performance (http://www.gossamer-threads.com/lists/lucene/java-user/144374)
timguy
  • 2,063
  • 2
  • 21
  • 40
  • 1
    You haven't provided the code used to construct the BooleanQuery. You'd need to do that in order for someone to find the problem with it. I do wonder, though: What is the purpose of the match all on the end? Doesn't look to me like it does anything useful... – femtoRgon Jan 03 '17 at 15:36
  • Is it possible that you used a different analyzer when constructing the BooleanQuery? I'd advse you to inspect in debug mode the query constructed by the parser, and see what is the structure (start by looking at the toString()) of what was generated – Yossi Vainshtein Jan 04 '17 at 09:39
  • @femtoRgon: Thanks, I added the code for the BooleanQuery and I think I found the part of the query which led to different results. As this is legacy code I just want to change the query to "don't accept documents with MUST_NOT if the field is in the document itselft" so I suppose the MatchAllQuery *:* is there if MUST_NOT the `-(...)` is the only single criteria. But I will check if it makes sense. @Yossi Vainshtein : Thanks, but Analyzer is the same for both of them: `StandardAnalyzer(Version.LUCENE_46)` – timguy Jan 04 '17 at 14:01

1 Answers1

1

I found the solution to my problem. Instead of creating this for the BooleanQuery:

Query fieldOccursQuery = new TermQuery(new Term(queryFieldName, "[* TO *]"));

I used this:

ConstantScoreQuery constantScoreQuery = new ConstantScoreQuery(new FieldValueFilter(queryFieldName));
    query.add(constantScoreQuery, Occur.MUST);

Now my query looks different but I only get documents with fields with my queryFieldName.

Issue seems to be the leading wildcard in my first solution: Find all Lucene documents having a certain field

Community
  • 1
  • 1
timguy
  • 2,063
  • 2
  • 21
  • 40