當前位置: 首頁>>代碼示例>>Java>>正文


Java QueryParser類代碼示例

本文整理匯總了Java中org.apache.lucene.queryparser.classic.QueryParser的典型用法代碼示例。如果您正苦於以下問題:Java QueryParser類的具體用法?Java QueryParser怎麽用?Java QueryParser使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


QueryParser類屬於org.apache.lucene.queryparser.classic包,在下文中一共展示了QueryParser類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: search

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
/**
 * Searches the lucene store for a specific query
 *
 * @param <T>       What type of information are we searching
 * @param clazz     The class of the information we are searching
 * @param queryText The query text
 * @return list of entities
 * @throws ParseException the parse exception
 */
public final <T extends BaseEntity> List<Object[]> search(final Class<T> clazz, final String queryText) throws ParseException {
    final FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);

    final SearchFactory searchFactory = fullTextEntityManager.getSearchFactory();

    final QueryParser parser = new MultiFieldQueryParser(getClassLuceneFields(clazz), searchFactory.getAnalyzer(clazz));

    final List<Query> parsedQueries = Arrays.stream(queryText.split("AND"))
            .map(e -> parseQuery(e, parser))
            .filter(Objects::nonNull)
            .collect(Collectors.toList());

    final BooleanQuery.Builder bq = new BooleanQuery.Builder();
    parsedQueries.forEach(e -> bq.add(e, BooleanClause.Occur.MUST));

    final FullTextQuery jpaQuery = fullTextEntityManager.createFullTextQuery(bq.build(), clazz);
    jpaQuery.setProjection(ProjectionConstants.SCORE, ProjectionConstants.EXPLANATION, ProjectionConstants.THIS);

    return (List<Object[]>) jpaQuery.getResultList();
}
 
開發者ID:mhaddon,項目名稱:Sound.je,代碼行數:30,代碼來源:EntitySearch.java

示例2: search

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
/**
 * Given a search field to search,the name of the field to return results in
 * and a query string, return search results up to the limit.
 * 
 * @param searchfieldName
 * @param returnFieldName
 * @param queryStr
 * @param limit
 * @return search results (with confidences)
 * @throws ParseException
 * @throws IOException
 */
public HashMap<String[], Float> search(String searchfieldName,
		String[] returnFieldName, String queryStr, int limit)
		throws ParseException, IOException {
	if (queryStr == null || queryStr.length() == 0)
		return new HashMap<String[], Float>();
	final String clean = QueryParser.escape(queryStr);
	final Query q = new QueryParser(Version.LUCENE_40, searchfieldName,
			analyser).parse(clean);
	final TopScoreDocCollector collector = TopScoreDocCollector.create(
			limit, true);

	searcher.search(q, collector);
	final ScoreDoc[] hits = collector.topDocs().scoreDocs;
	final HashMap<String[], Float> results = new HashMap<String[], Float>();
	for (int i = 0; i < hits.length; ++i) {
		final int docId = hits[i].doc;
		final Document d = searcher.doc(docId);
		String[] rvalues = new String[returnFieldName.length];
		for(int j=0;j<rvalues.length;j++){
			rvalues[j]=d.get(returnFieldName[j]);
		}
		results.put(rvalues, hits[i].score);
	}
	return results;
}
 
開發者ID:openimaj,項目名稱:openimaj,代碼行數:38,代碼來源:QuickSearcher.java

示例3: testTopLevel

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
public void testTopLevel() throws Exception {
    Aggregation result;
    if (randomBoolean()) {
        result = testCase(new MatchAllDocsQuery(), topHits("_name").sort("string", SortOrder.DESC));
    } else {
        Query query = new QueryParser("string", new KeywordAnalyzer()).parse("d^1000 c^100 b^10 a^1");
        result = testCase(query, topHits("_name"));
    }
    SearchHits searchHits = ((TopHits) result).getHits();
    assertEquals(3L, searchHits.getTotalHits());
    assertEquals("3", searchHits.getAt(0).getId());
    assertEquals("type", searchHits.getAt(0).getType());
    assertEquals("2", searchHits.getAt(1).getId());
    assertEquals("type", searchHits.getAt(1).getType());
    assertEquals("1", searchHits.getAt(2).getId());
    assertEquals("type", searchHits.getAt(2).getType());
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:18,代碼來源:TopHitsAggregatorTests.java

示例4: deleteIndexesByField

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
@Override
public void deleteIndexesByField(String field) {
    if (!isNullOrEmpty(field)) {
        try {
            Query query;
            QueryParser parser = new QueryParser(Version.LUCENE_4_9, field, analyzer);
            parser.setAllowLeadingWildcard(true);
            try {
                query = parser.parse("*");
            } catch (ParseException e) {
                throw new IndexingException(errorMessage(
                        "could not remove full-text index for value " + field, 0));
            }

            synchronized (this) {
                indexWriter.deleteDocuments(query);
                commit();
            }
        } catch (IOException ioe) {
            throw new IndexingException(errorMessage(
                    "could not remove full-text index for value " + field, 0));
        } catch (VirtualMachineError vme) {
            handleVirtualMachineError(vme);
        }
    }
}
 
開發者ID:dizitart,項目名稱:nitrite-database,代碼行數:27,代碼來源:LuceneService.java

示例5: main

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
	IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(INDEX_DIRECTORY)));
	IndexSearcher indexSearcher = new IndexSearcher(reader);

	Analyzer analyzer = new StandardAnalyzer();
	QueryParser queryParser = new QueryParser(FIELD_CONTENTS, analyzer);
	String searchString = "shakespeare";
	Query query = queryParser.parse(searchString);

	TopDocs results = indexSearcher.search(query, 5);
	ScoreDoc[] hits = results.scoreDocs;

	int numTotalHits = results.totalHits;
	System.out.println(numTotalHits + " total matching documents");

	for(int i=0;i<hits.length;++i) {
		int docId = hits[i].doc;
		Document d = indexSearcher.doc(docId);
		System.out.println((i + 1) + ". " + d.get("path") + " score=" + hits[i].score);
	}
}
 
開發者ID:PacktPublishing,項目名稱:Java-Data-Science-Cookbook,代碼行數:22,代碼來源:SearchFiles.java

示例6: search

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
public SearchResult search(String index, String queryString, int page) {
    SearchResult searchResult = null;

    try {
        IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(Properties.getProperties().getProperty(Values.INDEX_LOCATION, Values.DEFAULT_INDEX_LOCATION))));
        IndexSearcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new StandardAnalyzer();

        // Search over the titles only for the moment
        QueryParser parser = new QueryParser(index, analyzer);
        Query query = parser.parse(queryString);

        searchResult = this.doPagingSearch(reader, searcher, query, queryString, page);
        reader.close();
    }
    catch(Exception ex) {}

    return searchResult;
}
 
開發者ID:boyter,項目名稱:freemoz,代碼行數:20,代碼來源:Searcher.java

示例7: synTokenQuery

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
private void synTokenQuery(String search, final int numbOfResults, final double minLuceneScore,
		Map<String, Float> result, IndexSearcher searcher) throws ParseException, IOException {

	QueryParser parser = new QueryParser(Version.LUCENE_46, "surfaceFormTokens",
			new StandardAnalyzer(Version.LUCENE_46));

	search = QueryParser.escape(search);

	Query q = parser.parse(search);
	/*
	 * Works only in String field!!
	 */
	// Query q = new FuzzyQuery(new Term("surfaceFormTokens",
	// QueryParser.escape(search)), 2);

	TopDocs top = searcher.search(q, numbOfResults);

	for (ScoreDoc doc : top.scoreDocs) {
		if (doc.score >= minLuceneScore) {
			final String key = searcher.doc(doc.doc).get("conceptID");
			if (result.getOrDefault(key, 0f) < doc.score) {
				result.put(key, doc.score);
			}
		}
	}
}
 
開發者ID:ag-sc,項目名稱:JLink,代碼行數:27,代碼來源:LuceneRetrieval.java

示例8: FbEntitySearcher

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
public FbEntitySearcher(String indexDir, int numOfDocs, String searchingStrategy) throws IOException {

    LogInfo.begin_track("Constructing Searcher");
    if (!searchingStrategy.equals("exact") && !searchingStrategy.equals("inexact"))
      throw new RuntimeException("Bad searching strategy: " + searchingStrategy);
    this.searchStrategy = searchingStrategy;

    queryParser = new QueryParser(
        Version.LUCENE_44,
        FbIndexField.TEXT.fieldName(),
        searchingStrategy.equals("exact") ? new KeywordAnalyzer() : new StandardAnalyzer(Version.LUCENE_44));
    LogInfo.log("Opening index dir: " + indexDir);
    IndexReader indexReader = DirectoryReader.open(SimpleFSDirectory.open(new File(indexDir)));
    indexSearcher = new IndexSearcher(indexReader);
    LogInfo.log("Opened index with " + indexReader.numDocs() + " documents.");

    this.numOfDocs = numOfDocs;
    LogInfo.end_track();
  }
 
開發者ID:cgraywang,項目名稱:TextHIN,代碼行數:20,代碼來源:FbEntitySearcher.java

示例9: Search

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
/**
 * 查詢方法
 * @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;
}
 
開發者ID:zhangjikai,項目名稱:sdudoc,代碼行數:26,代碼來源:LuceneIndexSearch.java

示例10: getQuery

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
@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);
    }
    q.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));
    return q;
  } catch (ParseException e1) {
    throw new ParserException(e1.getMessage());
  }
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:23,代碼來源:UserInputQueryBuilder.java

示例11: parseQuery

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
/**
 * The standard query parser does not seem to parse queries how i expect.
 * The problem occurs with integers and ranges,
 * It also doesnt allow the user to normally say NOT ... at the start of the search.
 * This method provides a simple abstraction over the normal query language and adds a tiny bit better support
 * <p>
 * Integer queries are wrapped in []
 * <p>
 * It allows a few things:
 * 1. Prefixing queries with NOT, is treated like *:* AND NOT
 * 2. Support for integers greater than [name:>integer] ie: [times:>100]
 * 3. Support for integers lesser than [name:<integer] ie: [times:<100]
 * 4. Support for integer ranges [name:[small_integer TO large_integer]] ie: [times:[1 TO 10]]
 * 5. Support for integer range queries and normal queries in same query. ie: "a:1 AND [times:>1]"
 *
 * @param queryText our query string
 * @param parser    query parser
 * @return query element
 */
private Query parseQuery(final String queryText, final QueryParser parser) {
    final Mutable<Query> query = Mutable.of(null);
    final Mutable<String> queryMessage = Mutable.of(queryText.trim());

    // Ensure that NOT prefixes are instead *:* AND NOT
    PatternMatcher.of(startsWithNotPattern, queryMessage.get())
            .then(startsWithNotMatcher ->
                    queryMessage.mutate(queryMessage.get().replaceFirst("NOT", "*:* AND NOT")));

    PatternMatcher.of(queryNumericPattern, queryMessage.get())
            // Process integer range
            .then(queryNumericMatcher -> {
                final String fieldName = queryNumericMatcher.group(1).trim();
                final String fieldQuery = queryNumericMatcher.group(2).trim();
                final Long[] ranges = getRangeValues(fieldQuery);

                // query.mutate(LongPoint.newRangeQuery(fieldName, ranges[0], ranges[1]));
                query.mutate(NumericRangeQuery.newLongRange(fieldName, ranges[0], ranges[1], true, true));
            })
            // This is a normal string query
            .otherwise(queryNumericMatcher -> query.mutate(wrappedQueryParserParse(queryMessage.get(), parser)));

    return query.get();
}
 
開發者ID:mhaddon,項目名稱:Sound.je,代碼行數:44,代碼來源:EntitySearch.java

示例12: search

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
/**
 * Search sample. 
 * 
 * @param directory the index directory.
 * @throws IOException in case of I/O failure.
 * @throws ParseException in case of Query parse exception.
 */	
public static void search(Directory directory) throws IOException, ParseException {
	IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));
	
	Query query = new QueryParser("title", new StandardAnalyzer()).parse("title:Solr");
	TopDocs matches = searcher.search(query, 10);
	
	System.out.println("Search returned " + matches.totalHits + " matches.");
	Arrays.stream(matches.scoreDocs)
		.map(scoreDoc -> luceneDoc(scoreDoc, searcher))
		.forEach(doc -> {
			System.out.println("-------------------------------------");				
			System.out.println("ID:\t" + doc.get("id"));
			System.out.println("TITLE:\t" + doc.get("title"));
			System.out.println("AUTHOR:\t" + doc.get("author"));
			System.out.println("SCORE:\t" + doc.get("score"));
			
		});
}
 
開發者ID:agazzarini,項目名稱:as-full-text-search-server,代碼行數:26,代碼來源:LuceneBasicFlowExample.java

示例13: findAll

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
/**
 * Executes a query for all documents in the index. 
 * 
 * @throws Exception never, otherwise the test fails.
 */
@Test
public void findAll() throws Exception {
	IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));
	
	Query query = new QueryParser("title", new StandardAnalyzer()).parse("Solr");
	TopDocs matches = searcher.search(query, 10);
	
	assertEquals(3, matches.totalHits);
	
	Set<String> expected = new HashSet<String>(); 
	expected.add("1");
	expected.add("2");
	expected.add("3");
	
	Set<String> result = Arrays.stream(matches.scoreDocs)
		.map(scoreDoc -> luceneDoc(scoreDoc.doc, searcher))
		.map(doc -> doc.get("id"))
		.collect(Collectors.toCollection(HashSet::new));
	
	assertEquals(expected, result);
}
 
開發者ID:agazzarini,項目名稱:as-full-text-search-server,代碼行數:27,代碼來源:LuceneBasicFlowExampleTestCase.java

示例14: findByAuthorSurname

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
/**
 * Search all books of a given author. 
 * 
 * @throws Exception never, otherwise the test fails.
 */
@Test
public void findByAuthorSurname() throws Exception {
	IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));
	
	Query query = new QueryParser("author", new StandardAnalyzer()).parse("Gazzarini");
	TopDocs matches = searcher.search(query, 10);
	
	assertEquals(1, matches.totalHits);
			
	final String id = Arrays.stream(matches.scoreDocs)
		.map(scoreDoc -> luceneDoc(scoreDoc.doc, searcher))
		.map(doc -> doc.get("id"))
		.findFirst()
		.get();
	
	assertEquals("1", id);
}
 
開發者ID:agazzarini,項目名稱:as-full-text-search-server,代碼行數:23,代碼來源:LuceneBasicFlowExampleTestCase.java

示例15: initialize

import org.apache.lucene.queryparser.classic.QueryParser; //導入依賴的package包/類
@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);
}
 
開發者ID:oaqa,項目名稱:bioasq,代碼行數:23,代碼來源:LuceneInMemorySentenceRetrievalExecutor.java


注:本文中的org.apache.lucene.queryparser.classic.QueryParser類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。