lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Armnotstrong <zhaoxmu...@gmail.com>
Subject Re: unable to delete document via the IndexWriter.deleteDocuments(term) method
Date Fri, 17 Feb 2017 12:26:40 GMT
Thanks, Ian:

You saved my day!

And there is a further question to ask:

Since the analyzer could only be configured through the IndexWriter,
using  different
analyzers for different Fields is not possible, right? I only want
this '_id' field to identify
the document in index, so I could update or delete the specific
document from index
when needed, the real searching field is a text field, which should be
analysed by
smart_cn analyser.

Thus, I think it will good to have such an configure option as
IndexOptions.NOT_ANALYSED.
I remember to have that in the old version of lucene, but not found in
version 5.x

Any suggestion to bypass that?

Sorry for my bad English.

2017-02-17 19:40 GMT+08:00 Ian Lea <ian.lea@gmail.com>:
> Hi
>
>
> SimpleAnalyzer uses LetterTokenizer which divides text at non-letters.
> Your add and search methods use the analyzer but the delete method doesn't.
>
> Replacing SimpleAnalyzer with KeywordAnalyzer in your program fixes it.
> You'll need to make sure that your id field is left alone.
>
>
> Good to see a small self-contained test program.  A couple of suggestions
> to make it even better if there's a next time:
>
> Use final static String ID = "_id" and ... KEY =
> "5836962b0293a47b09d345f1".  Minimises the risk of typos.
>
> And use RAMDirectory.  Means your program doesn't leave junk on my disk if
> I run it, and also means it starts with an empty index each time.
>
>
> --
> Ian.
>
>
> On Fri, Feb 17, 2017 at 10:04 AM, Armnotstrong <zhaoxmustc@gmail.com> wrote:
>
>> Hi, all:
>>
>> I am Using version 5.5.4, and find can't delete a document via the
>> IndexWriter.deleteDocuments(term) method.
>>
>> Here is the test code:
>>
>> import org.apache.lucene.analysis.core.SimpleAnalyzer;
>> import org.apache.lucene.document.Document;
>> import org.apache.lucene.document.Field;
>> import org.apache.lucene.document.FieldType;
>> import org.apache.lucene.index.*;
>> import org.apache.lucene.queryparser.classic.ParseException;
>> import org.apache.lucene.queryparser.classic.QueryParser;
>> import org.apache.lucene.search.IndexSearcher;
>> import org.apache.lucene.search.Query;
>> import org.apache.lucene.search.ScoreDoc;
>> import org.apache.lucene.store.Directory;
>> import org.apache.lucene.store.FSDirectory;
>>
>> import java.io.IOException;
>> import java.nio.file.Paths;
>>
>> public class TestSearch {
>>     static SimpleAnalyzer analyzer = new SimpleAnalyzer();
>>
>>     public static void main(String[] argvs) throws IOException,
>> ParseException {
>>         generateIndex("5836962b0293a47b09d345f1");
>>         query("5836962b0293a47b09d345f1");
>>         delete("5836962b0293a47b09d345f1");
>>         query("5836962b0293a47b09d345f1");
>>
>>     }
>>
>>     public static void generateIndex(String id) throws IOException {
>>         Directory directory = FSDirectory.open(Paths.get("/
>> tmp/test/lucene"));
>>         IndexWriterConfig config = new IndexWriterConfig(analyzer);
>>         IndexWriter iwriter = new IndexWriter(directory, config);
>>         FieldType fieldType = new FieldType();
>>         fieldType.setStored(true);
>>         fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_
>> AND_POSITIONS_AND_OFFSETS);
>>         Field idField = new Field("_id", id, fieldType);
>>         Document doc = new Document();
>>         doc.add(idField);
>>         iwriter.addDocument(doc);
>>         iwriter.close();
>>
>>     }
>>
>>     public static void query(String id) throws ParseException, IOException
>> {
>>         Query query = new QueryParser("_id", analyzer).parse(id);
>>         Directory directory = FSDirectory.open(Paths.get("/
>> tmp/test/lucene"));
>>         IndexReader ireader  = DirectoryReader.open(directory);
>>         IndexSearcher isearcher = new IndexSearcher(ireader);
>>         ScoreDoc[] scoreDoc = isearcher.search(query, 100).scoreDocs;
>>         for(ScoreDoc scdoc: scoreDoc){
>>             Document doc = isearcher.doc(scdoc.doc);
>>             System.out.println(doc.get("_id"));
>>         }
>>     }
>>
>>     public static void delete(String id){
>>         try {
>>              Directory directory =
>> FSDirectory.open(Paths.get("/tmp/test/lucene"));
>>             IndexWriterConfig config = new IndexWriterConfig(analyzer);
>>             IndexWriter iwriter = new IndexWriter(directory, config);
>>             Term term = new Term("_id", id);
>>             iwriter.deleteDocuments(term);
>>             iwriter.commit();
>>             iwriter.close();
>>         }catch (IOException e){
>>             e.printStackTrace();
>>         }
>>     }
>> }
>>
>>
>> --
>> ========================================
>> best regards & a nice day
>> Zhao Ximing
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>> For additional commands, e-mail: java-user-help@lucene.apache.org
>>
>>



-- 
========================================
best regards & a nice day
Zhao Ximing

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message