Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package index; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.StopFilter; import org.apache.lucene.analysis.Token; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.queryParser.MultiFieldQueryParser; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.RangeQuery; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocCollector; import org.apache.lucene.search.highlight.Highlighter; import org.apache.lucene.search.highlight.QueryScorer; import org.apache.lucene.search.highlight.SimpleFragmenter; import org.apache.lucene.search.highlight.SimpleHTMLFormatter; import org.mira.lucene.analysis.IK_CAnalyzer; import org.springframework.util.NumberUtils; /** * * @author Administrator */ public class IndexUtils { //0. ? public static void buildIndex(String indexFile, String storeIdFile, String sqlPath, String defaultPath, String classPath, String keyName) { IncrementIndex.buildIndex(indexFile, storeIdFile, sqlPath, defaultPath, classPath, keyName); } //1. ? @SuppressWarnings("deprecation") public static List queryByOneKey(IndexSearcher indexSearcher, String field, String key) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException { try { Date date1 = new Date(); QueryParser queryParser = new QueryParser(field, new StandardAnalyzer()); Query query = queryParser.parse(key); Hits hits = indexSearcher.search(query); Date date2 = new Date(); System.out.println("" + (date2.getTime() - date1.getTime()) + "ms"); List list = new ArrayList(); for (int i = 0; i < hits.length(); i++) { list.add(getIndexResult(hits.doc(i), "index.IndexResult")); } return list; } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } //2. ?and? //?index //?doc.add(new Field("pid", rs.getString("pid"), Field.Store.YES,Field.Index.TOKENIZED)); @SuppressWarnings("deprecation") public static List queryByMultiKeys(IndexSearcher indexSearcher, String[] fields, String[] keys, String entityCLassPath) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException { try { BooleanQuery m_BooleanQuery = new BooleanQuery(); if (keys != null && keys.length > 0) { for (int i = 0; i < keys.length; i++) { // Query query = null; // ???? if (StringUtils.isNotBlank(keys[i])) { // String[] keyFlag = keys[i].split(","); // range?? if ("range".equals(keyFlag[0])) { // numberdate if ("number".equals(keyFlag[1])) { query = new RangeQuery(new Term(fields[i], keyFlag[2]), new Term(fields[i], keyFlag[3]), true); } else { query = new RangeQuery(new Term(fields[i], keyFlag[2]), new Term(fields[i], keyFlag[3]), true); } } else { QueryParser queryParser = new QueryParser(fields[i], new StandardAnalyzer()); query = queryParser.parse(keys[i]); } } ; m_BooleanQuery.add(query, BooleanClause.Occur.MUST);//and? } Hits hits = indexSearcher.search(m_BooleanQuery); List list = new ArrayList(); for (int i = 0; i < hits.length(); i++) { list.add(getIndexResult(hits.doc(i), entityCLassPath)); } return list; } } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } //3. ?? //?? public static List highlight(IndexSearcher indexSearcher, String key) throws ClassNotFoundException { try { QueryParser queryParser = new QueryParser("name", new StandardAnalyzer()); Query query = queryParser.parse(key); TopDocCollector collector = new TopDocCollector(800); indexSearcher.search(query, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; Highlighter highlighter = null; SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color='red'>", "</font>"); highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query)); highlighter.setTextFragmenter(new SimpleFragmenter(200)); List list = new ArrayList(); Document doc; for (int i = 0; i < hits.length; i++) { //System.out.println(hits[i].score); doc = indexSearcher.doc(hits[i].doc); TokenStream tokenStream = new StandardAnalyzer().tokenStream("name", new StringReader(doc.get("name"))); // IndexResult ir = getIndexResult(doc,"index.IndexResult"); // ir.setName(highlighter.getBestFragment(tokenStream, doc.get("name"))); // list.add(ir); } return list; } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } //4. @SuppressWarnings("deprecation") public static List queryByMultiFileds(IndexSearcher indexSearcher, String[] fields, String key) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException { try { MultiFieldQueryParser mfq = new MultiFieldQueryParser(fields, new StandardAnalyzer()); Query query = mfq.parse(key); Hits hits = indexSearcher.search(query); List<IndexResult> list = new ArrayList<IndexResult>(); for (int i = 0; i < hits.length(); i++) { list.add((IndexResult) getIndexResult(hits.doc(i), "index.IndexResult")); } return list; } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } //5. public static void deleteIndex(String indexFile, String id) throws CorruptIndexException, IOException { IndexReader indexReader = IndexReader.open(indexFile); indexReader.deleteDocuments(new Term("id", id)); indexReader.close(); } //6. ? @SuppressWarnings("deprecation") public static String Standard_Analyzer(String str) { Analyzer analyzer = new StandardAnalyzer(); Reader r = new StringReader(str); StopFilter sf = (StopFilter) analyzer.tokenStream("", r); System.out.println("=====StandardAnalyzer===="); System.out.println("????"); Token t; String results = ""; try { while ((t = sf.next()) != null) { System.out.println(t.termText()); results = results + " " + t.termText(); } } catch (IOException e) { e.printStackTrace(); } return results; } //7. ? @SuppressWarnings("deprecation") public static String ik_CAnalyzer(String str) { Analyzer analyzer = new IK_CAnalyzer(); Reader r = new StringReader(str); TokenStream ts = (TokenStream) analyzer.tokenStream("", r); System.out.println("=====IK_CAnalyzer===="); System.out.println("?:?,?????"); Token t; String results = ""; try { while ((t = ts.next()) != null) { System.out.println(t.termText()); results = results + " " + t.termText(); } } catch (IOException e) { e.printStackTrace(); } return results; } //? public static void queryFromResults() { } /** * ?? * @param doc * @param entityPath * @return * @throws ClassNotFoundException * @throws java.beans.IntrospectionException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws java.lang.reflect.InvocationTargetException * @throws InstantiationException * @throws NoSuchMethodException */ public static Object getIndexResult(Document doc, String entityPath) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException { // ?? Class c2 = Class.forName(entityPath); // Object obj = c2.getConstructor(new Class[] {}).newInstance(); // Field[] fields = c2.getDeclaredFields(); // ?? for (Field field : fields) { // ?? PropertyDescriptor pd = new PropertyDescriptor(field.getName(), c2); // setget Method method = pd.getWriteMethod(); // method.invoke(obj, new Object[] { doc.get(field.getName()) }); } return obj; } public static void main(String argsp[]) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException { Document doc = null; getIndexResult(doc, "index.IndexResult"); } }