If you want to replace a possibly existant document, you've got to
- check whether it exists or not
let's assume your Lucene's primary key field is called "id"
First, open an IndexReader
IndexReader ir = IndexReader.open("your_index_path");
Then, check the TermEnum associated with the value of the primary
key you're looking for (let's assume it s called "pkey") :
TermDocs terms = ir.termDocs(new
org.apache.lucene.index.Term(LUCENE_FIELD_ID, pkey));
if your keys are really primaries, the enumeration will be void or
contain one element.
So :
if (terms.next()) //terms is not empty => the pkey has been found =>
the document already exists => we'll have to delete it before adding it
ir.delete(terms.doc()); // delete the current document in
the TermDocs enumeration
// Here, you perform the normal addition
Gilles
-----Message d'origine-----
De : Don Kaiser [mailto:[EMAIL PROTECTED]
Envoy� : mercredi 19 novembre 2003 18:14
� : Lucene Users List
Objet : RE: Document ID's and duplicates
If you do this will the old version of the document be replaced by the new
one?
-don
> -----Original Message-----
> From: MOYSE Gilles (Cetelem) [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, November 19, 2003 6:57 AM
> To: 'Lucene Users List'
> Subject: RE: Document ID's and duplicates
>
>
> Hi.
>
> You just have to add a field in your document object before
> adding it to the
> index.
> The field should be of "keyword" type. You can use a code of
> that kind :
>
> IndexWriter writer = new IndexWriter("path_to_your_index",
> your_analyzer_object);
>
> Document doc = new Document();
> doc.add(Field.keyword("id"), (String)pkey); // add an "id" field
> containig the "pkey" value (received from the db for instance)
> // you can add other fields here
>
> writer.addDocument(doc).
> writer.optimize();
> writer.close();
>
> Gilles.
> -----Message d'origine-----
> De : jt oob [mailto:[EMAIL PROTECTED]
> Envoy� : mercredi 19 novembre 2003 15:43
> � : Lucene-Users-List
> Objet : Document ID's and duplicates
>
>
> Hi folks,
>
> I've got a feeling the answer to this has either been posted on here
> recently, or is on the site somewhere - but i can't find it. Apologies
> if i'm going over old ground.
>
> What is the best way force documents to be only indexed once?
>
> Is it a case of having a field with a unique value for the
> document and
> searching the index for that field before adding?
>
> If that is the way to do it, would it be a good idea to add an
> additional field type which would take care of this behind the scenes?
> Many people move to lucene after discovering the downfalls of text
> searching in Databases (like me), and would love a "primary key" type
> field.
>
> Regards,
> jt
>
> ______________________________________________________________
> __________
> Want to chat instantly with your online friends? Get the FREE Yahoo!
> Messenger http://mail.messenger.yahoo.co.uk
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]