2013年11月1日星期五

lucene4.x how to filter a field ?

Field publish is StringField type , add the following method of the field


doc.add(new StringField("publish", publish, Field.Store.YES));


now have to solve is how to filter the field , such as search out the results only publish = " microblogging " result.
How can I do ?

I tried IndexSearcher 's search methods used FieldValueFilter or TermsFilter class , it seems we can not do .

thank
------ Solution --------------------------------- -----------
IndexSearcher + Query query directly on the line , do not Filter.

        try {
            FSDirectory fsdir = FSDirectory.open(new File("索引路径"));
            IndexReader reader = DirectoryReader.open(fsdir);
            fsdir.close();
            IndexSearcher searcher = new IndexSearcher(reader);
            Query query = new TermQuery(new Term("publish", "微博"));
            ScoreDoc[] hits = searcher.search(query, 5000).scoreDocs;
            int size = hits.length;
            for (int i = 0; i < size; i++) {
                Document doc = searcher.doc(hits[i].doc);
                System.out.println(doc.get("publish"));
            }
        } catch (IOException ex) {
            ex.printStackTrace(System.err);
        }

------ For reference only ----------------------------------- ----




Thank

没有评论:

发表评论