简体   繁体   中英

how to delete a document (by LongField) in lucene 4.10.0

I'm using Lucene 4.10.0 and I wanna know how to delete a document by LongField. In practise I index documents that are identified by unique id (something like ISBN). I store the id with LongField :

doc.add(new LongField(Book.ID, item.getISBN(),Field.Store.YES));

How is possible to delete it by this unique id?

I know that for my version exist methods

 deleteDocuments(Query queries)

 deleteDocuments(Term terms) 

of class IndexWriter, but the conscructor of Term doesn't allow Long value (and using the "trick" of parse long in String doesn't work) and with queries I'm not too much able. Any help or suggestion is really appreciated.

Simply transforming your Long to string doesn't work because Lucene uses a special encoding for numeric types. As such, you should use a query. A NumericRangeQuery specifically. Something like:

Query query = NumericRangeQuery.newLongRange(Book.ID, item.getISBN(), item.getISBN(), true, true)
writer.deleteDocuments(query);

For a point of interest, in order to delete using terms, instead of a query, you would need to transform the numbers to lucene's prefix-coded string representation, using NumericUtils , something like:

BytesRefBuilder bytes = new BytesRefBuilder();
NumericUtils.longToPrefixCoded(item.getISBN(), 0, bytes);
Term term = new Term(Book.ID, bytes.toBytesRef());
writer.deleteDocuments(term);

You generally shouldn't do that though. NumericUtils is really only intended for internal use. But perhaps it does elucidate why simple to-string approaches don't work.

You can use a NumericRangeQuery to delete one or more documents. Set the long range accordingly, for that you want to use the newLongRange factory method.

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