org.apache.lucene.queryparser.classic.ParseException Java Examples

The following examples show how to use org.apache.lucene.queryparser.classic.ParseException. 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 Project: arcusplatform   Author: arcus-smart-home   File: ProductIndex.java    License: Apache License 2.0 6 votes vote down vote up
public List<ProductCatalogEntry> search(String queryString) throws IOException, ParseException {
	List<ProductCatalogEntry> results = new ArrayList<ProductCatalogEntry>();
	
	IndexReader reader = DirectoryReader.open(dir);
	IndexSearcher searcher = new IndexSearcher(reader);
	Analyzer analyzer = new SimpleAnalyzer();
	
	QueryParser parser = new QueryParser(searchField, analyzer);
	Query query = parser.parse(queryString);
	
	TopDocs docs = searcher.search(query, 100);
	ScoreDoc[] hits = docs.scoreDocs;
	
	for (ScoreDoc sd: hits) {
		Document doc = searcher.doc(sd.doc);
		results.add(prodcat.getProductById(doc.get("id")));
	}
	reader.close();
	
	return results;
}
 
Example #2
Source Project: stratio-cassandra   Author: Stratio   File: LuceneCondition.java    License: Apache License 2.0 6 votes vote down vote up
/** {@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 Project: incubator-retired-blur   Author: apache   File: SuperParserTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test4() throws ParseException {
  Query query = parser.parse("<a.a:a a.d:e a.b:b>  -<b.c:c b.d:d>");

  BooleanQuery booleanQuery1 = new BooleanQuery();
  booleanQuery1.add(new TermQuery(new Term("a.a", "a")), Occur.SHOULD);
  booleanQuery1.add(new TermQuery(new Term("a.d", "e")), Occur.SHOULD);
  booleanQuery1.add(new TermQuery(new Term("a.b", "b")), Occur.SHOULD);

  BooleanQuery booleanQuery2 = new BooleanQuery();
  booleanQuery2.add(new TermQuery(new Term("b.c", "c")), Occur.SHOULD);
  booleanQuery2.add(new TermQuery(new Term("b.d", "d")), Occur.SHOULD);

  SuperQuery superQuery1 = new SuperQuery(booleanQuery1, ScoreType.SUPER, new Term("_primedoc_"));
  SuperQuery superQuery2 = new SuperQuery(booleanQuery2, ScoreType.SUPER, new Term("_primedoc_"));

  BooleanQuery booleanQuery = new BooleanQuery();
  booleanQuery.add(superQuery1, Occur.SHOULD);
  booleanQuery.add(superQuery2, Occur.MUST_NOT);

  assertQuery(booleanQuery, query);
}
 
Example #4
Source Project: orientdb-lucene   Author: orientechnologies   File: OLuceneIndexType.java    License: Apache License 2.0 6 votes vote down vote up
public static Query createFullQuery(OIndexDefinition index, Object key, Analyzer analyzer, Version version) throws ParseException {

    String query = "";
    if (key instanceof OCompositeKey) {
      Object params = ((OCompositeKey) key).getKeys().get(0);
      if (params instanceof Map) {
        Object q = ((Map) params).get("q");
        if (q != null) {
          query = q.toString();
        }
      } else {
        query = params.toString();

      }
    } else {
      query = key.toString();
    }

    return getQueryParser(index, query, analyzer, version);

  }
 
Example #5
Source Project: Stargraph   Author: Lambda-3   File: LuceneSearchQueryGenerator.java    License: MIT License 6 votes vote down vote up
private static Query fuzzyPhraseSearch(String field, String searchTerm, int maxEdits) {
    StringBuilder queryStr = new StringBuilder();
    queryStr.append(field).append(":(");

    String words[] = searchTerm.split("\\s+");
    for (int i = 0; i < words.length; i++) {
        if (i > 0) {
            queryStr.append(" AND ");
        }
        queryStr.append(ComplexPhraseQueryParser.escape(words[i])).append("~").append(maxEdits);
    }
    queryStr.append(")");

    try {
        return new ComplexPhraseQueryParser("value", new StandardAnalyzer()).parse(queryStr.toString());
    } catch (ParseException e) {
        throw new StarGraphException(e);
    }
}
 
Example #6
Source Project: SearchServices   Author: Alfresco   File: AlfrescoSolrDataModel.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
public Query getCMISQuery(CMISQueryMode mode, Pair<SearchParameters, Boolean> searchParametersAndFilter, SolrQueryRequest req, org.alfresco.repo.search.impl.querymodel.Query queryModelQuery, CmisVersion cmisVersion, String alternativeDictionary) throws ParseException
{
    SearchParameters searchParameters = searchParametersAndFilter.getFirst();
    Boolean isFilter = searchParametersAndFilter.getSecond();

    CmisFunctionEvaluationContext functionContext = getCMISFunctionEvaluationContext(mode, cmisVersion, alternativeDictionary);

    Set<String> selectorGroup = queryModelQuery.getSource().getSelectorGroups(functionContext).get(0);

    LuceneQueryBuilderContext<Query, Sort, ParseException> luceneContext = getLuceneQueryBuilderContext(searchParameters, req, alternativeDictionary, FTSQueryParser.RerankPhase.SINGLE_PASS);
    @SuppressWarnings("unchecked")
    LuceneQueryBuilder<Query, Sort, ParseException> builder = (LuceneQueryBuilder<Query, Sort, ParseException>) queryModelQuery;
    org.apache.lucene.search.Query luceneQuery = builder.buildQuery(selectorGroup, luceneContext, functionContext);

    return new ContextAwareQuery(luceneQuery, Boolean.TRUE.equals(isFilter) ? null : searchParameters);
}
 
Example #7
Source Project: lucene-solr   Author: apache   File: ComplexPhraseQueryParser.java    License: Apache License 2.0 6 votes vote down vote up
protected void parsePhraseElements(ComplexPhraseQueryParser qp) throws ParseException {
  // TODO ensure that field-sensitivity is preserved ie the query
  // string below is parsed as
  // field+":("+phrasedQueryStringContents+")"
  // but this will need code in rewrite to unwrap the first layer of
  // boolean query

  String oldDefaultParserField = qp.field;
  try {
    //temporarily set the QueryParser to be parsing the default field for this phrase e.g author:"fred* smith"
    qp.field = this.field;
    contents[0] = qp.parse(phrasedQueryStringContents);
  }
  finally {
    qp.field = oldDefaultParserField;
  }
}
 
Example #8
Source Project: lucene4ir   Author: lucene4ir   File: RetrievalApp.java    License: Apache License 2.0 6 votes vote down vote up
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 #9
Source Project: scava   Author: crossminer   File: SORecommender.java    License: Eclipse Public License 2.0 6 votes vote down vote up
public TopDocs executeQuery(org.apache.lucene.search.Query query) throws IOException, ParseException {
	Directory indexDir = FSDirectory.open(Paths.get(INDEX_DIRECTORY));
	try {
		IndexReader reader = DirectoryReader.open(indexDir);
		IndexSearcher searcher = new IndexSearcher(reader);
		if (isBm25 == false) {
			ClassicSimilarity CS = new ClassicSimilarity();
			searcher.setSimilarity(CS);
		}
		TopDocs docs = searcher.search(query, hitsPerPage);
		return docs;
	} catch (Exception e) {
		logger.error(e.getMessage());
		return null;
	}
}
 
Example #10
Source Project: incubator-retired-blur   Author: apache   File: SuperParserTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test28() throws ParseException {
  SpatialContext ctx = SpatialContext.GEO;
  ShapeReadWriter<SpatialContext> shapeReadWriter = new ShapeReadWriter<SpatialContext>(ctx);
  int maxLevels = 11;
  SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
  RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(grid, "a.id_gis", false);
  Circle circle = ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(10, DistanceUtils.EARTH_MEAN_RADIUS_KM));
  SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, circle);

  String writeSpatialArgs = SpatialArgsParser.writeSpatialArgs(args, shapeReadWriter);

  // This has to be done because of rounding.
  SpatialArgs spatialArgs = SpatialArgsParser.parse(writeSpatialArgs, shapeReadWriter);
  Query q1 = sq(strategy.makeQuery(spatialArgs));
  Query q = parseSq("a.id_gis:\"" + writeSpatialArgs + "\"");
  boolean equals = q1.equals(q);
  assertTrue(equals);
}
 
Example #11
@Test
public void testVariants() throws ParseException {
	final String[] queries = {"carry", "bismillah", "me", "nothing else matters", "nothing AND matters"};
	final boolean[] bools = {Boolean.FALSE, Boolean.TRUE};
	final int maxMaxNumberOfResults = 10;
	ScoreDoc after = null;

	GlobalSearchCategory[] categories = null;
	for (String query : queries) {
		for (boolean highlightResult : bools) {
			for (int maxNumberOfResults = 1; maxNumberOfResults <= maxMaxNumberOfResults; maxNumberOfResults++) {
				for (boolean simpleMode : bools) {
					GlobalSearchResultBuilder gsr = new GlobalSearchResultBuilder(query).setHighlightResult(highlightResult).setMaxNumberOfResults(maxNumberOfResults).setSimpleMode(simpleMode).setSearchOffset(after).setSearchCategories(categories);
					GlobalSearchResult searchResult = gsr.runSearch();
					Assert.assertNotNull(searchResult);
					if (highlightResult) {
						Assert.assertEquals(searchResult.getNumberOfResults(), searchResult.getBestFragments().size());
					} else {
						Assert.assertNull(searchResult.getBestFragments());
					}
					Assert.assertTrue("Should not find more results than expected", searchResult.getNumberOfResults() <= maxMaxNumberOfResults);
				}
			}
		}
	}
}
 
Example #12
Source Project: tutorials   Author: eugenp   File: InMemoryLuceneIndex.java    License: MIT License 6 votes vote down vote up
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 #13
Source Project: jpress   Author: JPressProjects   File: LuceneSearcher.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
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 #14
Source Project: lucene-solr   Author: apache   File: UserInputQueryBuilder.java    License: Apache License 2.0 6 votes vote down vote up
@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 #15
Source Project: bookshop   Author: DanielLin07   File: SearchController.java    License: MIT License 5 votes vote down vote up
@RequestMapping("searchBook.do")
public ModelAndView searchBook(Book book) throws IOException, ParseException {

    ModelAndView mav = new ModelAndView("searchBook");

    // 关键字
    String keyword = book.getName();
    System.out.println(keyword);
    // 准备中文分词器
    IKAnalyzer analyzer = new IKAnalyzer();
    // 索引
    Directory index = createIndex(analyzer);
    // 查询器
    Query query = new QueryParser("name",analyzer).parse(keyword);
    // 搜索
    IndexReader reader = DirectoryReader.open(index);
    IndexSearcher searcher = new IndexSearcher(reader);
    int numberPerPage = 10;
    ScoreDoc[] hits = searcher.search(query,numberPerPage).scoreDocs;
    List<Book> books = new ArrayList<>();
    for (int i = 0; i < hits.length; i++) {
        ScoreDoc scoreDoc = hits[i];
        int docId = scoreDoc.doc;
        Document document = searcher.doc(docId);
        Book tmpBook = bookService.get(Integer.parseInt(document.get("id")));
        books.add(tmpBook);
    }

    mav.addObject("books",books);
    return mav;
}
 
Example #16
Source Project: adam   Author: echocat   File: UserProfileDataQueryMapper.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Query convertToLuceneQuery(@Nonnull UserProfileDataQuery query) {
    final QueryParser parser = new MultiFieldQueryParser(LUCENE_VERSION, toFieldsArray(query), _luceneAnalyzerFactory.createAnalyzer());
    parser.setDefaultOperator(AND);
    final String searchTerm = query.getSearchTerm();
    try {
        return parser.parse(searchTerm != null ? searchTerm : "");
    } catch (final ParseException e) {
        throw new RuntimeException("Unable to parse query: " + searchTerm, e);
    }
}
 
Example #17
Source Project: Quelea   Author: quelea-projection   File: BibleSearchIndex.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Search for bible chapters that match the given filter.
 *
 * @param queryString the query string to filter.
 * @param type ignored - may be null.
 * @return a list of all bible chapters that match the given filter.
 */
@Override
public BibleChapter[] filter(String queryString, FilterType type) {
    String sanctifyQueryString = SearchIndexUtils.makeLuceneQuery(queryString);
    if(chapters.isEmpty() || sanctifyQueryString.isEmpty()) {
        return chapters.values().toArray(new BibleChapter[chapters.size()]);
    }
    List<BibleChapter> ret;
    try (DirectoryReader dr = DirectoryReader.open(index)) {
        IndexSearcher searcher = new IndexSearcher(dr);
        BooleanQuery.setMaxClauseCount(Integer.MAX_VALUE);
        Query q = new ComplexPhraseQueryParser("text", analyzer).parse(sanctifyQueryString);
        TopScoreDocCollector collector = TopScoreDocCollector.create(10000,10000);
        searcher.search(q, collector);
        ScoreDoc[] hits = collector.topDocs().scoreDocs;
        ret = new ArrayList<>();
        for(int i = 0; i < hits.length; ++i) {
            int docId = hits[i].doc;
            Document d = searcher.doc(docId);
            BibleChapter chapter = chapters.get(Integer.parseInt(d.get("number")));
            ret.add(chapter);
        }
        return ret.toArray(new BibleChapter[ret.size()]);
    }
    catch (ParseException | IOException ex) {
        LOGGER.log(Level.WARNING, "Invalid query string: " + sanctifyQueryString, ex);
        return new BibleChapter[0];
    }
}
 
Example #18
Source Project: incubator-retired-blur   Author: apache   File: AliasBlurFilterCache.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Filter storePreFilter(String table, String filterStr, Filter filter, FilterParser filterParser)
    throws ParseException {
  if (filter instanceof QueryWrapperFilter) {
    QueryWrapperFilter queryWrapperFilter = (QueryWrapperFilter) filter;
    Query query = queryWrapperFilter.getQuery();
    Filter newFilter = buildNewFilter(query, _tableAliasFilterMap.get(table), filterParser);
    FilterKey key = new FilterKey(table, filterStr);
    _preFilterCacheMap.put(key, newFilter);
    return newFilter;
  }
  return filter;
}
 
Example #19
Source Project: rapidminer-studio   Author: rapidminer   File: ActionsGlobalSearchManager.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Deletes all perspective actions from the index.
 *
 */
private void deleteAllPerspectiveActions() {
	// search for all elements that start with "workspace_"
	GlobalSearchResultBuilder builder = new GlobalSearchResultBuilder(FIELD_VIEW_TYPE + GlobalSearchUtilities.QUERY_FIELD_SPECIFIER + String.valueOf(Boolean.TRUE));
	builder.setMaxNumberOfResults(Integer.MAX_VALUE).setSearchCategories(GlobalSearchRegistry.INSTANCE.getSearchCategoryById(getSearchCategoryId()));
	try {
		GlobalSearchResult result = builder.runSearch();
		removeDocumentsFromIndex(result.getResultDocuments());
	} catch (ParseException e) {
		LogService.getRoot().log(Level.WARNING, "com.rapidminer.gui.actions.search.ActionsGlobalSearchManager.error.delete_views_error", e);
	}
}
 
Example #20
Source Project: incubator-retired-blur   Author: apache   File: SuperParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test2() throws ParseException {
  Query query = parser.parse("<a.c:c a.d:d>");

  BooleanQuery booleanQuery = new BooleanQuery();
  booleanQuery.add(new TermQuery(new Term("a.c", "c")), Occur.SHOULD);
  booleanQuery.add(new TermQuery(new Term("a.d", "d")), Occur.SHOULD);
  SuperQuery superQuery = new SuperQuery(booleanQuery, ScoreType.SUPER, new Term("_primedoc_"));

  assertQuery(superQuery, query);
}
 
Example #21
Source Project: incubator-retired-blur   Author: apache   File: SuperParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test47() throws ParseException {
  SpatialContext ctx = SpatialContext.GEO;
  int maxLevels = 11;
  SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
  RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(grid, "a.id_gis", false);
  Circle circle = ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(10, DistanceUtils.EARTH_MEAN_RADIUS_MI));
  SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, circle);

  Query q1 = sq(strategy.makeQuery(args));
  Query q = parseSq("<a.id_gis:\"Intersects(Circle(33.000000,-80.000000 d=10.0m))\">");
  boolean equals = q1.equals(q);
  assertTrue(equals);
}
 
Example #22
Source Project: oodt   Author: apache   File: CatalogSearch.java    License: Apache License 2.0 5 votes vote down vote up
public static Query ParseQuery(String query) {
    // note that "__FREE__" is a control work for free text searching
    QueryParser parser = new QueryParser(freeTextBlock, new CASAnalyzer());
    Query luceneQ = null;
    try {
        luceneQ = (Query) parser.parse(query);
    } catch (ParseException e) {
        System.out.println("Error parsing query text.");
        System.exit(-1);
    }
    return luceneQ;
}
 
Example #23
Source Project: Easy-Cassandra-samples   Author: otaviojava   File: CurriculoService.java    License: Apache License 2.0 5 votes vote down vote up
public List<Resume> procurarCV(String texto) {
	try {
		List<String> ids = search.findByBio(texto);
		return (List<Resume>) repository.findAll(ids);
	} catch (ParseException | IOException e) {
		e.printStackTrace();
	}
	return Collections.emptyList();
}
 
Example #24
Source Project: sakai   Author: sakaiproject   File: HelpManagerImpl.java    License: Educational Community License v2.0 5 votes vote down vote up
/**
 * @see org.sakaiproject.api.app.help.HelpManager#searchResources(java.lang.String)
 */
public Set<Resource> searchResources(String queryStr)
{
	initialize();

	try
	{
		return searchResources(queryStr, "content");
	}
	catch (ParseException e)
	{
		log.debug("ParseException parsing Help search query  " + queryStr, e);
		return null;
	}
}
 
Example #25
Source Project: lucene-solr   Author: apache   File: TestExtendableQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
public void testUnescapedExtDelimiter() throws Exception {
  Extensions ext = newExtensions(':');
  ext.add("testExt", new ExtensionStub());
  ExtendableQueryParser parser = (ExtendableQueryParser) getParser(null, ext);
  expectThrows(ParseException.class, () -> {
    parser.parse("aField:testExt:\"foo \\& bar\"");
  });
}
 
Example #26
Source Project: vxquery   Author: apache   File: CaseSensitiveQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Query getPrefixQuery(String field, String termStr) throws ParseException {
    if (!getAllowLeadingWildcard() && termStr.startsWith("*"))
        throw new ParseException("'*' not allowed as first character in PrefixQuery");
    Term t = new Term(field, termStr);
    return newPrefixQuery(t);
}
 
Example #27
Source Project: incubator-retired-blur   Author: apache   File: SuperParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test45() throws ParseException {
  SpatialContext ctx = SpatialContext.GEO;
  int maxLevels = 11;
  SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
  RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(grid, "a.id_gis", false);
  Circle circle = ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(10, DistanceUtils.EARTH_MEAN_RADIUS_MI));
  SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, circle);

  Query q1 = sq(bq(bc_m(strategy.makeQuery(args))));
  Query q = parseSq("<+a.id_gis:\"Intersects(Circle(33.000000,-80.000000 d=10.0m))\">");
  boolean equals = q1.equals(q);
  assertTrue(equals);
}
 
Example #28
Source Project: titan1withtp3.1   Author: graben1437   File: LuceneIndex.java    License: Apache License 2.0 5 votes vote down vote up
private Pair<Document, Map<String, Shape>> retrieveOrCreate(String docID, IndexSearcher searcher) throws IOException {
    Document doc;
    TopDocs hits = searcher.search(new TermQuery(new Term(DOCID, docID)), 10);
    Map<String, Shape> geofields = Maps.newHashMap();

    if (hits.scoreDocs.length > 1)
        throw new IllegalArgumentException("More than one document found for document id: " + docID);

    if (hits.scoreDocs.length == 0) {
        if (log.isTraceEnabled())
            log.trace("Creating new document for [{}]", docID);

        doc = new Document();
        doc.add(new StringField(DOCID, docID, Field.Store.YES));
    } else {
        if (log.isTraceEnabled())
            log.trace("Updating existing document for [{}]", docID);

        int docId = hits.scoreDocs[0].doc;
        //retrieve the old document
        doc = searcher.doc(docId);
        for (IndexableField field : doc.getFields()) {
            if (field.stringValue().startsWith(GEOID)) {
                try {
                    geofields.put(field.name(), ctx.readShapeFromWkt(field.stringValue().substring(GEOID.length())));
                } catch (java.text.ParseException e) {
                    throw new IllegalArgumentException("Geoshape was unparsable");
                }
            }
        }
    }

    return new ImmutablePair<Document, Map<String, Shape>>(doc, geofields);
}
 
Example #29
Source Project: incubator-retired-blur   Author: apache   File: SecureAtomicReaderTestBase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTermWalk() throws IOException, ParseException {
  SecureAtomicReader secureReader = getSecureReader();
  Fields fields = secureReader.fields();

  assertEquals(0, getTermCount(fields, "termmask")); // read mask
  assertEquals(0, getTermWithSeekCount(fields, "termmask")); // read mask
  assertEquals(0, getTermCount(fields, "shouldnotsee")); // discover
  assertEquals(0, getTermWithSeekCount(fields, "shouldnotsee")); // discover
  assertEquals(1, getTermCount(fields, "test"));
  assertEquals(1, getTermWithSeekCount(fields, "test"));

  secureReader.close();
}
 
Example #30
Source Project: nifi   Author: apache   File: ITestPersistentProvenanceRepository.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCompressOnRollover() throws IOException, InterruptedException, ParseException {
    assumeFalse(isWindowsEnvironment());
    final RepositoryConfiguration config = createConfiguration();
    config.setMaxEventFileLife(500, TimeUnit.MILLISECONDS);
    config.setCompressOnRollover(true);
    repo = new PersistentProvenanceRepository(config, DEFAULT_ROLLOVER_MILLIS);
    repo.initialize(getEventReporter(), null, null, IdentifierLookup.EMPTY);

    final String uuid = "00000000-0000-0000-0000-000000000000";
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("abc", "xyz");
    attributes.put("xyz", "abc");
    attributes.put("filename", "file-" + uuid);

    final ProvenanceEventBuilder builder = new StandardProvenanceEventRecord.Builder();
    builder.setEventTime(System.currentTimeMillis());
    builder.setEventType(ProvenanceEventType.RECEIVE);
    builder.setTransitUri("nifi://unit-test");
    attributes.put("uuid", uuid);
    builder.fromFlowFile(createFlowFile(3L, 3000L, attributes));
    builder.setComponentId("1234");
    builder.setComponentType("dummy processor");

    for (int i = 0; i < 10; i++) {
        builder.fromFlowFile(createFlowFile(i, 3000L, attributes));
        repo.registerEvent(builder.build());
    }

    repo.waitForRollover();
    final File storageDir = config.getStorageDirectories().values().iterator().next();
    final File compressedLogFile = new File(storageDir, "0.prov.gz");
    assertTrue(compressedLogFile.exists());
}