简体   繁体   中英

Remove Lucene document by query of not analyzed text field

I'm 99% sure I had this working in the past, maybe I'm wrong.

Anyway, I'd like to delete a Lucene document by a Field which is stored but not analyzed and contains text.

So the problem, it seems, is that calling luceneWriter.deleteDocuments(query) doesn't delete the document unless the field referenced in query is Field.Index.ANALYZED or a simple number.

Some code:

Integer myId = 1234;
Document doc = new Document();
Field field = new Field("MyIdField", myId, Field.Store.YES, Field.Index.ANALYZED);
doc.add(field);
indexWriter.add(doc);
indexWriter.commit();

...

QueryParser parser = new QueryParser(VERSION, "MyIdField", ANALYZER);
Query query = parser.parse("MyIdField:1234");
indexWriter.deleteDocuments(query);
indexWriter.commit();

Everything works!
Sweet.. what if the field is not analyzed?

Field field = new Field("MyIdField", myId, Field.Store.YES, Field.Index.NOT_ANALYZED);

Still works!
Awesome, what if it's not just a number?

Field field = new Field("MyIdField", "ID" + myId, Field.Store.YES, Field.Index.NOT_ANALYZED);
...
Query query = parser.parse("MyIdField:ID1234");

Doesn't work!.. darn.
The query doesn't match the document and so it isn't deleted.
What if we do index it?

Field field = new Field("MyIdField", "ID" + myId, Field.Store.YES, Field.Index.ANALYZED);
...
Query query = parser.parse("MyIdField:ID1234");

It works again!

Ok, so if the field is not analyzed it can still be queried if it only contains a number? Am I missing something?

Thanks for taking some time.

Note:
Technically, there are two fields, making it an AND query. As such, I'd prefer to delete the documents with a Query rather than a Term . I'm not sure if that makes a difference but wanted to emphasize I would like to stick with a solution using a Query .

According to this question , you have to use a PhraseQuery to search a not analyzed field. Your code

Query query = parser.parse("MyIdField:ID1234");

would yield a TermQuery instead, and thus won't match.

I recommend you to try a KeywordAnalyzer instead (remember that, even if your field isn't analyzed, the query parser could still analyze your query string and therefore your match could fail anyway).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM