List of usage examples for org.apache.lucene.queries.function.valuesource ReciprocalFloatFunction ReciprocalFloatFunction
public ReciprocalFloatFunction(ValueSource source, float m, float a, float b)
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 av a 2 s .c om*/ //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.search.function.distance.MultiPointDistanceValueSourceParser.java
License:Apache License
@Override public ValueSource parse(FunctionQParser fp) throws SyntaxError { String fieldName = fp.parseId(); SchemaField field = fp.getReq().getSchema().getField(fieldName); FieldType type = field.getType();/*w w w.jav a 2 s . co m*/ if (!(type instanceof MultiPointDocValuesField)) throw new SyntaxError("This function only supports fields of type " + MultiPointDocValuesField.class.getName() + ", not " + type.getClass().getName()); MultiPointDocValuesField mpdvFieldType = (MultiPointDocValuesField) type; double[] parsedLatLong = null; try { parsedLatLong = ParseUtils.parseLatitudeLongitude(fp.parseArg()); } catch (InvalidShapeException e) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e); } double y = parsedLatLong[0]; double x = parsedLatLong[1]; SpatialContext ctx = mpdvFieldType.getCtx(); Point point = ctx.makePoint(x, y); String score = fp.getLocalParams().get("score", "distance"); ValueSource valueSource = new MultiPointDistanceValueSource(fieldName, point, ctx); if ("distance".equals(score)) { return valueSource; } else if ("recipDistance".equals(score)) { int shift = fp.getLocalParams().getInt("shift", 100); int maxScore = fp.getLocalParams().getInt("maxScore", 10); return new ReciprocalFloatFunction(valueSource, maxScore, shift, shift); } else { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'score' local-param must be one of 'distance', or 'recipDistance'"); } }