List of usage examples for org.apache.lucene.queries.function.valuesource LongFieldSource LongFieldSource
public LongFieldSource(String field)
From source file:com.difference.historybook.index.lucene.LuceneIndex.java
License:Apache License
@Override public SearchResultWrapper search(String collection, String query, int offset, int size, boolean includeDebug) throws IndexException { try {//from www .j a va2 s. co m //TODO: make age be a component in the ranking? BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder(); queryBuilder.add(parser.parse(query), Occur.MUST); queryBuilder.add(new TermQuery(new Term(IndexDocumentAdapter.FIELD_COLLECTION, collection)), Occur.FILTER); Query baseQuery = queryBuilder.build(); FunctionQuery boostQuery = new FunctionQuery( new ReciprocalFloatFunction(new DurationValueSource(new Date().getTime() / 1000, new LongFieldSource(IndexDocumentAdapter.FIELD_TIMESTAMP)), RECIP, 1F, 1F)); Query q = new CustomScoreQuery(baseQuery, boostQuery); QueryScorer queryScorer = new QueryScorer(q, IndexDocumentAdapter.FIELD_SEARCH); Fragmenter fragmenter = new SimpleSpanFragmenter(queryScorer); Highlighter highlighter = new Highlighter(queryScorer); highlighter.setTextFragmenter(fragmenter); GroupingSearch gsearch = new GroupingSearch(IndexDocumentAdapter.FIELD_URL_GROUP).setGroupDocsLimit(1) .setAllGroups(true).setIncludeMaxScore(true); TopGroups<?> groups = gsearch.search(searcher, q, offset, size); ArrayList<SearchResult> results = new ArrayList<>(size); for (int i = offset; i < offset + size && i < groups.groups.length; i++) { ScoreDoc scoreDoc = groups.groups[i].scoreDocs[0]; Document luceneDoc = searcher.doc(scoreDoc.doc); IndexDocumentAdapter doc = new IndexDocumentAdapter(luceneDoc); TokenStream tokenStream = TokenSources.getTokenStream(IndexDocumentAdapter.FIELD_SEARCH, reader.getTermVectors(scoreDoc.doc), luceneDoc.get(IndexDocumentAdapter.FIELD_SEARCH), analyzer, highlighter.getMaxDocCharsToAnalyze() - 1); String[] snippets = highlighter.getBestFragments(tokenStream, luceneDoc.get(IndexDocumentAdapter.FIELD_SEARCH), 3); String snippet = Arrays.asList(snippets).stream().collect(Collectors.joining("\n")); snippet = Jsoup.clean(snippet, Whitelist.simpleText()); String debugInfo = null; if (includeDebug) { Explanation explanation = searcher.explain(q, scoreDoc.doc); debugInfo = explanation.toString(); } results.add(new SearchResult(doc.getKey(), doc.getCollection(), doc.getTitle(), doc.getUrl(), doc.getDomain(), doc.getTimestampText(), snippet, debugInfo, scoreDoc.score)); } SearchResultWrapper wrapper = new SearchResultWrapper().setQuery(query).setOffset(offset) .setMaxResultsRequested(size) .setResultCount(groups.totalGroupCount != null ? groups.totalGroupCount : 0) .setResults(results); if (includeDebug) { wrapper.setDebugInfo(q.toString()); } return wrapper; } catch (IOException | ParseException | InvalidTokenOffsetsException e) { LOG.error(e.getLocalizedMessage()); throw new IndexException(e); } }
From source file:org.apache.solr.analytics.statistics.StatsCollectorSupplierFactory.java
License:Apache License
/** * Builds a value source for a given field, making sure that the field fits a given source type. * @param schema the schema//w ww . ja v a 2 s. c o m * @param expressionString The name of the field to build a Field Source from. * @param sourceType FIELD_TYPE for any type of field, NUMBER_TYPE for numeric fields, * DATE_TYPE for date fields and STRING_TYPE for string fields. * @return a value source */ private static ValueSource buildFieldSource(IndexSchema schema, String expressionString, int sourceType) { SchemaField sf; try { sf = schema.getField(expressionString); } catch (SolrException e) { throw new SolrException(ErrorCode.BAD_REQUEST, "The field " + expressionString + " does not exist.", e); } FieldType type = sf.getType(); if (type instanceof TrieIntField) { if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) { return null; } return new IntFieldSource(expressionString) { public String description() { return field; } }; } else if (type instanceof TrieLongField) { if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) { return null; } return new LongFieldSource(expressionString) { public String description() { return field; } }; } else if (type instanceof TrieFloatField) { if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) { return null; } return new FloatFieldSource(expressionString) { public String description() { return field; } }; } else if (type instanceof TrieDoubleField) { if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) { return null; } return new DoubleFieldSource(expressionString) { public String description() { return field; } }; } else if (type instanceof TrieDateField) { if (sourceType != DATE_TYPE && sourceType != FIELD_TYPE) { return null; } return new DateFieldSource(expressionString) { public String description() { return field; } }; } else if (type instanceof StrField) { if (sourceType != STRING_TYPE && sourceType != FIELD_TYPE) { return null; } return new BytesRefFieldSource(expressionString) { public String description() { return field; } }; } throw new SolrException(ErrorCode.BAD_REQUEST, type.toString() + " is not a supported field type in Solr Analytics."); }
From source file:org.apache.solr.schema.LongField.java
License:Apache License
@Override public ValueSource getValueSource(SchemaField field, QParser qparser) { field.checkFieldCacheSource(qparser); return new LongFieldSource(field.name); }
From source file:org.apache.solr.schema.LongPointField.java
License:Apache License
@Override public ValueSource getValueSource(SchemaField field, QParser qparser) { field.checkFieldCacheSource(); return new LongFieldSource(field.getName()); }
From source file:us.askplatyp.kb.lucene.lucene.LuceneSearcher.java
License:Open Source License
private Query addScoreBoostToQuery(Query query) { return new CustomScoreQuery(query, new FunctionQuery(new LongFieldSource("score"))); }