org.apache.lucene.queryparser.classic.QueryParser Java Examples
The following examples show how to use
org.apache.lucene.queryparser.classic.QueryParser.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestPersistentProvenanceRepository.java From localization_nifi with Apache License 2.0 | 7 votes |
private List<Document> runQuery(final File indexDirectory, final List<File> storageDirs, final String query) throws IOException, ParseException { try (final DirectoryReader directoryReader = DirectoryReader.open(FSDirectory.open(indexDirectory))) { final IndexSearcher searcher = new IndexSearcher(directoryReader); final Analyzer analyzer = new SimpleAnalyzer(); final org.apache.lucene.search.Query luceneQuery = new QueryParser("uuid", analyzer).parse(query); final Query q = new Query(""); q.setMaxResults(1000); final TopDocs topDocs = searcher.search(luceneQuery, 1000); final List<Document> docs = new ArrayList<>(); for (final ScoreDoc scoreDoc : topDocs.scoreDocs) { final int docId = scoreDoc.doc; final Document d = directoryReader.document(docId); docs.add(d); } return docs; } }
Example #2
Source File: LuceneCondition.java From stratio-cassandra with Apache License 2.0 | 6 votes |
/** {@inheritDoc} */ @Override public Query query(Schema schema) { if (query == null) { throw new IllegalArgumentException("Query statement required"); } try { Analyzer analyzer = schema.getAnalyzer(); QueryParser queryParser = new QueryParser(defaultField, analyzer); queryParser.setAllowLeadingWildcard(true); queryParser.setLowercaseExpandedTerms(false); Query luceneQuery = queryParser.parse(query); luceneQuery.setBoost(boost); return luceneQuery; } catch (ParseException e) { throw new RuntimeException("Error while parsing lucene syntax query", e); } }
Example #3
Source File: LuceneIndexSearch.java From sdudoc with MIT License | 6 votes |
/** * 查询方法 * @throws IOException * @throws CorruptIndexException * @throws ParseException */ public List Search(String searchString,LuceneResultCollector luceneResultCollector) throws CorruptIndexException, IOException, ParseException{ //方法一: System.out.println(this.indexSettings.getAnalyzer().getClass()+"----分词选择"); QueryParser q = new QueryParser(Version.LUCENE_44, "summary", this.indexSettings.getAnalyzer()); String search = new String(searchString.getBytes("ISO-8859-1"),"UTF-8"); System.out.println(search+"----------搜索的词语dd"); Query query = q.parse(search); //方法二: /* Term t = new Term("title", searchString); TermQuery query = new TermQuery(t); */ System.out.println(query.toString()+"--------query.tostring"); ScoreDoc[] docs = this.indexSearcher.search(query,100).scoreDocs; System.out.println("一共有:"+docs.length+"条记录"); List result = luceneResultCollector.collect(docs, this.indexSearcher); return result; }
Example #4
Source File: LuceneInMemorySentenceRetrievalExecutor.java From bioasq with Apache License 2.0 | 6 votes |
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); // initialize sentence chunker TokenizerFactory tokenizerFactory = UimaContextHelper.createObjectFromConfigParameter(context, "tokenizer-factory", "tokenizer-factory-params", IndoEuropeanTokenizerFactory.class, TokenizerFactory.class); SentenceModel sentenceModel = UimaContextHelper.createObjectFromConfigParameter(context, "sentence-model", "sentence-model-params", IndoEuropeanSentenceModel.class, SentenceModel.class); chunker = new SentenceChunker(tokenizerFactory, sentenceModel); // initialize hits hits = UimaContextHelper.getConfigParameterIntValue(context, "hits", 200); // initialize query analyzer, index writer config, and query parser analyzer = UimaContextHelper.createObjectFromConfigParameter(context, "query-analyzer", "query-analyzer-params", StandardAnalyzer.class, Analyzer.class); parser = new QueryParser("text", analyzer); // initialize query string constructor queryStringConstructor = UimaContextHelper.createObjectFromConfigParameter(context, "query-string-constructor", "query-string-constructor-params", BooleanBagOfPhraseQueryStringConstructor.class, QueryStringConstructor.class); }
Example #5
Source File: ImprovedLuceneInMemorySentenceRetrievalExecutor.java From bioasq with Apache License 2.0 | 6 votes |
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); TokenizerFactory tokenizerFactory = UimaContextHelper.createObjectFromConfigParameter(context, "tokenizer-factory", "tokenizer-factory-params", IndoEuropeanTokenizerFactory.class, TokenizerFactory.class); SentenceModel sentenceModel = UimaContextHelper.createObjectFromConfigParameter(context, "sentence-model", "sentence-model-params", IndoEuropeanSentenceModel.class, SentenceModel.class); chunker = new SentenceChunker(tokenizerFactory, sentenceModel); // initialize hits hits = UimaContextHelper.getConfigParameterIntValue(context, "hits", 200); // initialize query analyzer, index writer config, and query parser analyzer = UimaContextHelper.createObjectFromConfigParameter(context, "query-analyzer", "query-analyzer-params", StandardAnalyzer.class, Analyzer.class); parser = new QueryParser("text", analyzer); // initialize query string constructor queryStringConstructor = UimaContextHelper.createObjectFromConfigParameter(context, "query-string-constructor", "query-string-constructor-params", BagOfPhraseQueryStringConstructor.class, QueryStringConstructor.class); String parserProviderName = UimaContextHelper .getConfigParameterStringValue(context, "parser-provider"); parserProvider = ProviderCache.getProvider(parserProviderName, ParserProvider.class); lemma = new StanfordLemmatizer(); }
Example #6
Source File: LuceneFileSearch.java From tutorials with MIT License | 6 votes |
public List<Document> searchFiles(String inField, String queryString) { try { Query query = new QueryParser(inField, analyzer).parse(queryString); IndexReader indexReader = DirectoryReader.open(indexDirectory); IndexSearcher searcher = new IndexSearcher(indexReader); TopDocs topDocs = searcher.search(query, 10); List<Document> documents = new ArrayList<>(); for (ScoreDoc scoreDoc : topDocs.scoreDocs) { documents.add(searcher.doc(scoreDoc.doc)); } return documents; } catch (IOException | ParseException e) { e.printStackTrace(); } return null; }
Example #7
Source File: LogRegDocumentReranker.java From bioasq with Apache License 2.0 | 6 votes |
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); hits = UimaContextHelper.getConfigParameterIntValue(context, "hits", 100); analyzer = UimaContextHelper.createObjectFromConfigParameter(context, "query-analyzer", "query-analyzer-params", StandardAnalyzer.class, Analyzer.class); queryStringConstructor = UimaContextHelper.createObjectFromConfigParameter(context, "query-string-constructor", "query-string-constructor-params", LuceneQueryStringConstructor.class, QueryStringConstructor.class); parser = new QueryParser("text", analyzer); // load parameters String param = UimaContextHelper.getConfigParameterStringValue(context, "doc-logreg-params"); try { docFeatWeights = Resources.readLines(getClass().getResource(param), UTF_8).stream().limit(1) .map(line -> line.split("\t")).flatMap(Arrays::stream) .mapToDouble(Double::parseDouble).toArray(); } catch (IOException e) { throw new ResourceInitializationException(e); } }
Example #8
Source File: CodePatternSearcher.java From SnowGraph with Apache License 2.0 | 6 votes |
private static List<String> search(List<String> contents, String query, int n) throws IOException, ParseException { List<String> r=new ArrayList<>(); Directory dir=new RAMDirectory(); IndexWriter indexWriter=new IndexWriter(dir, new IndexWriterConfig(new EnglishAnalyzer())); for (String method:contents){ Document document=new Document(); document.add(new TextField("content",method, Field.Store.YES)); indexWriter.addDocument(document); } indexWriter.close(); QueryParser qp = new QueryParser("content", new EnglishAnalyzer()); IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(dir)); TopDocs topDocs = indexSearcher.search(qp.parse(query), n); for (ScoreDoc scoreDoc : topDocs.scoreDocs) { r.add(indexSearcher.doc(scoreDoc.doc).get("content")); } return r; }
Example #9
Source File: LuceneTextIndexReader.java From incubator-pinot with Apache License 2.0 | 6 votes |
/** * Get docIds from the text inverted index for a given raw value * @param value value to look for in the inverted index * @return docIDs in bitmap */ @Override public MutableRoaringBitmap getDocIds(Object value) { String searchQuery = (String) value; MutableRoaringBitmap docIds = new MutableRoaringBitmap(); Collector docIDCollector = new LuceneDocIdCollector(docIds, _docIdTranslator); try { // Lucene Query Parser is JavaCC based. It is stateful and should // be instantiated per query. Analyzer on the other hand is stateless // and can be created upfront. QueryParser parser = new QueryParser(_column, _standardAnalyzer); Query query = parser.parse(searchQuery); _indexSearcher.search(query, docIDCollector); return docIds; } catch (Exception e) { String msg = "Caught excepttion while searching the text index for column:" + _column + " search query:" + searchQuery; throw new RuntimeException(msg, e); } }
Example #10
Source File: LuceneSearcher.java From jpress with GNU Lesser General Public License v3.0 | 6 votes |
private static Query buildQuery(String keyword) { try { Analyzer analyzer = createAnalyzer(); //这里使用text,防止搜索出html的tag或者tag中属性 QueryParser queryParser1 = new QueryParser("text", analyzer); Query termQuery1 = queryParser1.parse(keyword); BooleanClause booleanClause1 = new BooleanClause(termQuery1, BooleanClause.Occur.SHOULD); QueryParser queryParser2 = new QueryParser("title", analyzer); Query termQuery2 = queryParser2.parse(keyword); BooleanClause booleanClause2 = new BooleanClause(termQuery2, BooleanClause.Occur.SHOULD); BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.add(booleanClause1).add(booleanClause2); return builder.build(); } catch (ParseException e) { LOG.error(e.toString(), e); } return null; }
Example #11
Source File: DefaultLuceneQueryBuilder.java From javaee-lab with Apache License 2.0 | 6 votes |
@Override public Query build(FullTextEntityManager fullTextEntityManager, SearchParameters searchParameters, List<SingularAttribute<?, ?>> availableProperties) { List<String> clauses = getAllClauses(searchParameters, searchParameters.getTerms(), availableProperties); StringBuilder query = new StringBuilder(); query.append("+("); for (String clause : clauses) { if (query.length() > 2) { query.append(" AND "); } query.append(clause); } query.append(")"); if (query.length() == 3) { return null; } //log.debug("Lucene query: {}", query); try { return new QueryParser(availableProperties.get(0).getName(), fullTextEntityManager.getSearchFactory().getAnalyzer("custom")) .parse(query.toString()); } catch (Exception e) { throw propagate(e); } }
Example #12
Source File: LuceneExample.java From yuzhouwan with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { // index try (Directory index = new NIOFSDirectory(Paths.get("/tmp/index"))) { // add try (IndexWriter writer = new IndexWriter(index, new IndexWriterConfig(new StandardAnalyzer()))) { Document doc = new Document(); doc.add(new TextField("blog", "yuzhouwan.com", Field.Store.YES)); doc.add(new StringField("github", "asdf2014", Field.Store.YES)); writer.addDocument(doc); writer.commit(); } // search try (DirectoryReader reader = DirectoryReader.open(index)) { IndexSearcher searcher = new IndexSearcher(reader); QueryParser parser = new QueryParser("blog", new StandardAnalyzer()); Query query = parser.parse("yuzhouwan.com"); ScoreDoc[] hits = searcher.search(query, 1000).scoreDocs; for (ScoreDoc hit : hits) { Document hitDoc = searcher.doc(hit.doc); System.out.println(hitDoc.get("blog")); } } } }
Example #13
Source File: UserInputQueryBuilder.java From lucene-solr with Apache License 2.0 | 6 votes |
@Override public Query getQuery(Element e) throws ParserException { String text = DOMUtils.getText(e); try { Query q = null; if (unSafeParser != null) { //synchronize on unsafe parser synchronized (unSafeParser) { q = unSafeParser.parse(text); } } else { String fieldName = DOMUtils.getAttribute(e, "fieldName", defaultField); //Create new parser QueryParser parser = createQueryParser(fieldName, analyzer); q = parser.parse(text); } float boost = DOMUtils.getAttribute(e, "boost", 1.0f); return new BoostQuery(q, boost); } catch (ParseException e1) { throw new ParserException(e1.getMessage()); } }
Example #14
Source File: InMemoryLuceneIndex.java From tutorials with MIT License | 6 votes |
public List<Document> searchIndex(String inField, String queryString) { try { Query query = new QueryParser(inField, analyzer).parse(queryString); IndexReader indexReader = DirectoryReader.open(memoryIndex); IndexSearcher searcher = new IndexSearcher(indexReader); TopDocs topDocs = searcher.search(query, 10); List<Document> documents = new ArrayList<>(); for (ScoreDoc scoreDoc : topDocs.scoreDocs) { documents.add(searcher.doc(scoreDoc.doc)); } return documents; } catch (IOException | ParseException e) { e.printStackTrace(); } return null; }
Example #15
Source File: RetrievalApp.java From lucene4ir with Apache License 2.0 | 6 votes |
public ScoreDoc[] runQuery(String qno, String queryTerms){ ScoreDoc[] hits = null; System.out.println("Query No.: " + qno + " " + queryTerms); try { Query query = parser.parse(QueryParser.escape(queryTerms)); try { TopDocs results = searcher.search(query, p.maxResults); hits = results.scoreDocs; } catch (IOException ioe){ ioe.printStackTrace(); System.exit(1); } } catch (ParseException pe){ pe.printStackTrace(); System.exit(1); } return hits; }
Example #16
Source File: RetrievalApp.java From lucene4ir with Apache License 2.0 | 6 votes |
public RetrievalApp(String retrievalParamFile){ System.out.println("Retrieval App"); System.out.println("Param File: " + retrievalParamFile); readParamsFromFile(retrievalParamFile); try { reader = DirectoryReader.open(FSDirectory.open( new File(p.indexName).toPath()) ); searcher = new IndexSearcher(reader); // create similarity function and parameter selectSimilarityFunction(sim); searcher.setSimilarity(simfn); parser = new QueryParser(Lucene4IRConstants.FIELD_ALL, analyzer); } catch (Exception e){ System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage()); } }
Example #17
Source File: GenericFTSLuceneImpl.java From yes-cart with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} */ @Override public List<Long> fullTextSearchRaw(final String query) { final QueryParser queryParser = new QueryParser("", new AsIsAnalyzer(false)); org.apache.lucene.search.Query parsed; try { parsed = queryParser.parse(query); } catch (Exception e) { final String msg = "Cant parse query : " + query + " Error : " + e.getMessage(); LOG.warn(msg); throw new IllegalArgumentException(msg, e); } if (parsed == null) { return Collections.emptyList(); } return fullTextSearch(parsed); }
Example #18
Source File: LuceneFulltextImplementation.java From ontopia with Apache License 2.0 | 6 votes |
@Override public SearchResultIF search(String query) throws IOException { synchronized (READER_LOCK) { openReader(); IndexSearcher searcher = new IndexSearcher(reader); try { logger.debug("Searching for: '" + query + "'"); Query _query = new QueryParser(defaultField, ANALYZER).parse(query); return new LuceneSearchResult(searcher, searcher.search(_query, Integer.MAX_VALUE)); } catch (org.apache.lucene.queryparser.classic.ParseException e) { logger.error("Error parsing query: '" + e.getMessage() + "'"); throw new IOException(e.getMessage(), e); } } }
Example #19
Source File: PageDocumentSearcher.java From gravitee-management-rest-api with Apache License 2.0 | 6 votes |
@Override public SearchResult search(io.gravitee.rest.api.service.search.query.Query query) throws TechnicalException { QueryParser parser = new MultiFieldQueryParser(new String[]{ "name", "content" }, analyzer); parser.setFuzzyMinSim(0.6f); try { final Query parse = parser.parse(QueryParserBase.escape(query.getQuery())); BooleanQuery.Builder bq = new BooleanQuery.Builder(); bq.add(parse, BooleanClause.Occur.MUST); bq.add(new TermQuery(new Term(FIELD_TYPE, FIELD_TYPE_VALUE)), BooleanClause.Occur.MUST); return search(bq.build()); } catch (ParseException pe) { logger.error("Invalid query to search for page documents", pe); throw new TechnicalException("Invalid query to search for page documents", pe); } }
Example #20
Source File: FTConnLucene.java From openprodoc with GNU Affero General Public License v3.0 | 6 votes |
/** * * @param Type * @param sDocMetadata * @param sBody * @param sMetadata * @return * @throws PDException */ @Override protected ArrayList<String> Search(String Type, String sDocMetadata, String sBody, String sMetadata) throws PDException { ArrayList<String> Res=new ArrayList(); IndexSearcher isearcher=null; try { isearcher=SM.acquire(); sBody=sBody.toLowerCase(); Query query = new QueryParser(F_FULLTEXT,analyzer).parse(sBody); ScoreDoc[] hits = isearcher.search(query, MAXRESULTS).scoreDocs; for (ScoreDoc hit : hits) Res.add(isearcher.doc(hit.doc).get(F_ID)); SM.release(isearcher); //ireader.close(); //directory.close(); } catch (Exception ex) { try { SM.release(isearcher); } catch (Exception e) {} PDException.GenPDException("Error_Searching_doc_FT:", ex.getLocalizedMessage()); } return(Res); }
Example #21
Source File: HelpManagerImpl.java From sakai with Educational Community License v2.0 | 5 votes |
/** * Search Lucene * * @param queryStr * @param defaultField * @return * @throws ParseException */ protected Set<Resource> searchResources(String queryStr, String defaultField) throws ParseException { Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); QueryParser parser = new QueryParser(Version.LUCENE_40, defaultField, analyzer); Query query = parser.parse(queryStr); return searchResources(query); }
Example #22
Source File: TestLucene.java From bookshop with MIT License | 5 votes |
public static void main(String[] args) throws IOException, ParseException { // 1. 准备中文分词器 IKAnalyzer analyzer = new IKAnalyzer(); // 2. 索引 List<String> productNames = new ArrayList<>(); productNames.add("飞利浦led灯泡e27螺口暖白球泡灯家用照明超亮节能灯泡转色温灯泡"); productNames.add("飞利浦led灯泡e14螺口蜡烛灯泡3W尖泡拉尾节能灯泡暖黄光源Lamp"); productNames.add("雷士照明 LED灯泡 e27大螺口节能灯3W球泡灯 Lamp led节能灯泡"); productNames.add("飞利浦 led灯泡 e27螺口家用3w暖白球泡灯节能灯5W灯泡LED单灯7w"); productNames.add("飞利浦led小球泡e14螺口4.5w透明款led节能灯泡照明光源lamp单灯"); productNames.add("飞利浦蒲公英护眼台灯工作学习阅读节能灯具30508带光源"); productNames.add("欧普照明led灯泡蜡烛节能灯泡e14螺口球泡灯超亮照明单灯光源"); productNames.add("欧普照明led灯泡节能灯泡超亮光源e14e27螺旋螺口小球泡暖黄家用"); productNames.add("聚欧普照明led灯泡节能灯泡e27螺口球泡家用led照明单灯超亮光源"); Directory index = createIndex(analyzer, productNames); // 3. 查询器 String keyword = "护眼带光源"; Query query = new QueryParser("name",analyzer).parse(keyword); // 4. 搜索 IndexReader reader = DirectoryReader.open(index); IndexSearcher searcher = new IndexSearcher(reader); int numberPerPage = 1000; System.out.printf("当前一共有%d条数据%n",productNames.size()); System.out.printf("查询关键字是:\"%s\"%n",keyword); ScoreDoc[] hits = searcher.search(query,numberPerPage).scoreDocs; // 5. 显示查询结果 showSearchResults(searcher, hits, query, analyzer); }
Example #23
Source File: SearchBuilder.java From taoshop with Apache License 2.0 | 5 votes |
public static void doSearch(String indexDir , String queryStr) throws IOException, ParseException, InvalidTokenOffsetsException { Directory directory = FSDirectory.open(Paths.get(indexDir)); DirectoryReader reader = DirectoryReader.open(directory); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new SmartChineseAnalyzer(); QueryParser parser = new QueryParser("tcontent",analyzer); Query query = parser.parse(queryStr); long startTime = System.currentTimeMillis(); TopDocs docs = searcher.search(query,10); System.out.println("查找"+queryStr+"所用时间:"+(System.currentTimeMillis()-startTime)); System.out.println("查询到"+docs.totalHits+"条记录"); //加入高亮显示的 SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<b><font color=red>","</font></b>"); QueryScorer scorer = new QueryScorer(query);//计算查询结果最高的得分 Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);//根据得分算出一个片段 Highlighter highlighter = new Highlighter(simpleHTMLFormatter,scorer); highlighter.setTextFragmenter(fragmenter);//设置显示高亮的片段 //遍历查询结果 for(ScoreDoc scoreDoc : docs.scoreDocs){ Document doc = searcher.doc(scoreDoc.doc); System.out.println(doc.get("title")); String tcontent = doc.get("tcontent"); if(tcontent != null){ TokenStream tokenStream = analyzer.tokenStream("tcontent", new StringReader(tcontent)); String summary = highlighter.getBestFragment(tokenStream, tcontent); System.out.println(summary); } } reader.close(); }
Example #24
Source File: SearchUtil.java From everywhere with Apache License 2.0 | 5 votes |
public static Query buildQuery(String searchText, String searchField) { Query query = null; try { if (searchField.equals(LuceneConstants.CONTENT)) { QueryParser qp = new QueryParser(searchField, analyzer); query = qp.parse(searchText); } else { Term term = new Term(LuceneConstants.PATH, searchText); query = new TermQuery(term); } } catch (Exception e) { e.printStackTrace(); } return query; }
Example #25
Source File: QueryFactory.java From incubator-atlas with Apache License 2.0 | 5 votes |
private QueryExpression create(Request request, ResourceDefinition resourceDefinition) throws InvalidQueryException { String queryString; if (request.getCardinality() == Request.Cardinality.INSTANCE) { String idPropertyName = resourceDefinition.getIdPropertyName(); queryString = String.format("%s:%s", idPropertyName, request.<String>getProperty(idPropertyName)); } else { queryString = request.getQueryString(); } QueryExpression queryExpression; if (queryString != null && !queryString.isEmpty()) { QueryParser queryParser = new QueryParser(Version.LUCENE_48, "name", new KeywordAnalyzer()); queryParser.setLowercaseExpandedTerms(false); queryParser.setAllowLeadingWildcard(true); Query query; try { query = queryParser.parse((String) escape(queryString)); } catch (ParseException e) { throw new InvalidQueryException(e.getMessage()); } LOG.info("LuceneQuery: {}", query); queryExpression = create(query, resourceDefinition); } else { queryExpression = new AlwaysQueryExpression(); } // add query properties to request so that they are returned request.addAdditionalSelectProperties(queryExpression.getProperties()); return queryExpression; }
Example #26
Source File: BlogLuceneIndexManager.java From BlogSystem with Apache License 2.0 | 5 votes |
/** * 搜索博文 * * @param word 关键字 * @param count 返回数量 * @return 符合的博文id */ public int[] search(String word, int count) throws IOException, ParseException { if (StringUtils.isEmpty(word) || count <= 0) return null; String path = propertiesManager.getLuceneIndexDir(); Directory dir = FSDirectory.open(Paths.get(path)); IndexReader reader = DirectoryReader.open(dir); IndexSearcher is = new IndexSearcher(reader); BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer(); // 或 查询 booleanQuery.add((new QueryParser(INDEX_BLOG_TITLE, analyzer)).parse(word), BooleanClause.Occur.SHOULD); booleanQuery.add((new QueryParser(INDEX_BLOG_KEY_WORDS, analyzer)).parse(word), BooleanClause.Occur.SHOULD); booleanQuery.add((new QueryParser(INDEX_BLOG_SUMMARY, analyzer)).parse(word), BooleanClause.Occur.SHOULD); booleanQuery.add((new QueryParser(INDEX_BLOG_CONTENT, analyzer)).parse(word), BooleanClause.Occur.SHOULD); //检索 TopDocs top = is.search(booleanQuery.build(), count); Integer[] result = new Integer[count]; int sum = 0; for (ScoreDoc doc : top.scoreDocs) { Document document = is.doc(doc.doc); result[sum++] = Integer.parseInt(document.get(INDEX_BLOG_ID)); } if (sum == 0) return null; int[] rs = new int[sum]; for (int i = 0; i < sum; i++) { rs[i] = result[i]; } dir.close(); analyzer.close(); reader.close(); return rs; }
Example #27
Source File: NLPIRTokenizerTest.java From nlpir-analysis-cn-ictclas with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { // NLPIR NLPIRTokenizerAnalyzer nta = new NLPIRTokenizerAnalyzer("", 1, "", "", false); // Index IndexWriterConfig inconf = new IndexWriterConfig(nta); inconf.setOpenMode(OpenMode.CREATE_OR_APPEND); IndexWriter index = new IndexWriter(FSDirectory.open(Paths.get("index/")), inconf); Document doc = new Document(); doc.add(new TextField("contents", "特朗普表示,很高兴汉堡会晤后再次同习近平主席通话。我同习主席就重大问题保持沟通和协调、两国加强各层级和各领域交往十分重要。当前,美中关系发展态势良好,我相信可以发展得更好。我期待着对中国进行国事访问。", Field.Store.YES)); index.addDocument(doc); index.flush(); index.close(); // Search String field = "contents"; IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get("index/"))); IndexSearcher searcher = new IndexSearcher(reader); QueryParser parser = new QueryParser(field, nta); Query query = parser.parse("特朗普习近平"); TopDocs top = searcher.search(query, 100); System.out.println("总条数:" + top.totalHits); ScoreDoc[] hits = top.scoreDocs; for (int i = 0; i < hits.length; i++) { System.out.println("doc=" + hits[i].doc + " score=" + hits[i].score); Document d = searcher.doc(hits[i].doc); System.out.println(d.get("contents")); } }
Example #28
Source File: Search.java From dacapobench with Apache License 2.0 | 5 votes |
public void run() throws java.io.IOException { Analyzer analyzer = new StandardAnalyzer(); QueryParser parser = new QueryParser(field, analyzer); while (true) { String line = in.readLine(); if (line == null || line.length() == -1) break; line = line.trim(); if (line.length() == 0) break; Query query = null; try { query = parser.parse(line); } catch (Exception e) { e.printStackTrace(); } searcher.search(query, 10); doPagingSearch(query); } reader.close(); out.flush(); out.close(); synchronized (parent) { parent.completed++; if (parent.completed % 4 == 0) { System.out.println(parent.completed + " query batches completed"); } parent.notify(); } }
Example #29
Source File: IndexManagerBase.java From winter with Apache License 2.0 | 5 votes |
protected QueryParser getQueryParserFromCache() { QueryParser queryParser = null; if(!queryParserCache.containsKey(Thread.currentThread())) { queryParser = getNewQueryParser(); queryParserCache.put(Thread.currentThread(), queryParser); } else queryParser = queryParserCache.get(Thread.currentThread()); return queryParser; }
Example #30
Source File: SearcherTest.java From cjs_ssms with GNU General Public License v2.0 | 5 votes |
public static void search(String indexDir, String q) throws Exception { Directory dir = FSDirectory.open(Paths.get(indexDir)); IndexReader reader = DirectoryReader.open(dir); IndexSearcher is = new IndexSearcher(reader); // Analyzer analyzer=new StandardAnalyzer(); // 标准分词器 SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer(); QueryParser parser = new QueryParser("desc", analyzer); Query query = parser.parse(q); long start = System.currentTimeMillis(); TopDocs hits = is.search(query, 10); long end = System.currentTimeMillis(); System.out.println("匹配 " + q + " ,总共花费" + (end - start) + "毫秒" + "查询到" + hits.totalHits + "个记录"); QueryScorer scorer = new QueryScorer(query); Fragmenter fragmenter = new SimpleSpanFragmenter(scorer); SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<b><font color='red'>", "</font></b>"); Highlighter highlighter = new Highlighter(simpleHTMLFormatter, scorer); highlighter.setTextFragmenter(fragmenter); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("city")); System.out.println(doc.get("desc")); String desc = doc.get("desc"); if (desc != null) { TokenStream tokenStream = analyzer.tokenStream("desc", new StringReader(desc)); System.out.println(highlighter.getBestFragment(tokenStream, desc)); } } reader.close(); }