Java Code Examples for org.elasticsearch.search.internal.SearchContext#current()
The following examples show how to use
org.elasticsearch.search.internal.SearchContext#current() .
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: InnerHitsQueryParserHelper.java From Elasticsearch with Apache License 2.0 | 6 votes |
public InnerHitsSubSearchContext parse(QueryParseContext parserContext) throws IOException, QueryParsingException { String fieldName = null; XContentParser.Token token; String innerHitName = null; SubSearchContext subSearchContext = new SubSearchContext(SearchContext.current()); try { XContentParser parser = parserContext.parser(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { fieldName = parser.currentName(); } else if (token.isValue()) { if ("name".equals(fieldName)) { innerHitName = parser.textOrNull(); } else { parseCommonInnerHitOptions(parser, token, fieldName, subSearchContext, sortParseElement, sourceParseElement, highlighterParseElement, scriptFieldsParseElement, fieldDataFieldsParseElement); } } else { parseCommonInnerHitOptions(parser, token, fieldName, subSearchContext, sortParseElement, sourceParseElement, highlighterParseElement, scriptFieldsParseElement, fieldDataFieldsParseElement); } } } catch (Exception e) { throw new QueryParsingException(parserContext, "Failed to parse [_inner_hits]", e); } return new InnerHitsSubSearchContext(innerHitName, subSearchContext); }
Example 2
Source File: QueryParseContext.java From Elasticsearch with Apache License 2.0 | 5 votes |
public void addInnerHits(String name, InnerHitsContext.BaseInnerHits context) { SearchContext sc = SearchContext.current(); if (sc == null) { throw new QueryParsingException(this, "inner_hits unsupported"); } InnerHitsContext innerHitsContext = sc.innerHits(); innerHitsContext.addInnerHitDefinition(name, context); }
Example 3
Source File: QueryParseContext.java From Elasticsearch with Apache License 2.0 | 5 votes |
public long nowInMillis() { SearchContext current = SearchContext.current(); if (current != null) { return current.nowInMillis(); } return System.currentTimeMillis(); }
Example 4
Source File: ParentQuery.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Weight doCreateWeight(IndexSearcher searcher, boolean needsScores) throws IOException { SearchContext sc = SearchContext.current(); ChildWeight childWeight; boolean releaseCollectorResource = true; ParentOrdAndScoreCollector collector = null; IndexParentChildFieldData globalIfd = parentChildIndexFieldData.loadGlobal((DirectoryReader)searcher.getIndexReader()); if (globalIfd == null) { // No docs of the specified type don't exist on this shard return new BooleanQuery.Builder().build().createWeight(searcher, needsScores); } try { collector = new ParentOrdAndScoreCollector(sc, globalIfd, parentType); searcher.search(parentQuery, collector); if (collector.parentCount() == 0) { return new BooleanQuery.Builder().build().createWeight(searcher, needsScores); } childWeight = new ChildWeight(this, parentQuery.createWeight(searcher, needsScores), childrenFilter, collector, globalIfd); releaseCollectorResource = false; } finally { if (releaseCollectorResource) { // either if we run into an exception or if we return early Releasables.close(collector); } } sc.addReleasable(collector, Lifetime.COLLECTION); return childWeight; }
Example 5
Source File: ChildrenConstantScoreQuery.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Weight doCreateWeight(IndexSearcher searcher, boolean needsScores) throws IOException { SearchContext sc = SearchContext.current(); IndexParentChildFieldData globalIfd = parentChildIndexFieldData.loadGlobal((DirectoryReader)searcher.getIndexReader()); final long valueCount; List<LeafReaderContext> leaves = searcher.getIndexReader().leaves(); if (globalIfd == null || leaves.isEmpty()) { return new BooleanQuery.Builder().build().createWeight(searcher, needsScores); } else { AtomicParentChildFieldData afd = globalIfd.load(leaves.get(0)); SortedDocValues globalValues = afd.getOrdinalsValues(parentType); valueCount = globalValues.getValueCount(); } if (valueCount == 0) { return new BooleanQuery.Builder().build().createWeight(searcher, needsScores); } ParentOrdCollector collector = new ParentOrdCollector(globalIfd, valueCount, parentType); searcher.search(childQuery, collector); final long remaining = collector.foundParents(); if (remaining == 0) { return new BooleanQuery.Builder().build().createWeight(searcher, needsScores); } Filter shortCircuitFilter = null; if (remaining <= shortCircuitParentDocSet) { shortCircuitFilter = ParentIdsFilter.createShortCircuitFilter( nonNestedDocsFilter, sc, parentType, collector.values, collector.parentOrds, remaining ); } return new ParentWeight(this, parentFilter, globalIfd, shortCircuitFilter, collector, remaining); }
Example 6
Source File: TTLFieldMapper.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Object valueForSearch(Object value) { long now; SearchContext searchContext = SearchContext.current(); if (searchContext != null) { now = searchContext.nowInMillis(); } else { now = System.currentTimeMillis(); } long val = value(value); return val - now; }
Example 7
Source File: DateFieldMapper.java From Elasticsearch with Apache License 2.0 | 5 votes |
private static Callable<Long> now() { return new Callable<Long>() { @Override public Long call() { final SearchContext context = SearchContext.current(); return context != null ? context.nowInMillis() : System.currentTimeMillis(); } }; }
Example 8
Source File: FieldValueFactorFunctionParser.java From Elasticsearch with Apache License 2.0 | 4 votes |
@Override public ScoreFunction parse(QueryParseContext parseContext, XContentParser parser) throws IOException, QueryParsingException { String currentFieldName = null; String field = null; float boostFactor = 1; FieldValueFactorFunction.Modifier modifier = FieldValueFactorFunction.Modifier.NONE; Double missing = null; XContentParser.Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if ("field".equals(currentFieldName)) { field = parser.text(); } else if ("factor".equals(currentFieldName)) { boostFactor = parser.floatValue(); } else if ("modifier".equals(currentFieldName)) { modifier = FieldValueFactorFunction.Modifier.valueOf(parser.text().toUpperCase(Locale.ROOT)); } else if ("missing".equals(currentFieldName)) { missing = parser.doubleValue(); } else { throw new QueryParsingException(parseContext, NAMES[0] + " query does not support [" + currentFieldName + "]"); } } else if("factor".equals(currentFieldName) && (token == XContentParser.Token.START_ARRAY || token == XContentParser.Token.START_OBJECT)) { throw new QueryParsingException(parseContext, "[" + NAMES[0] + "] field 'factor' does not support lists or objects"); } } if (field == null) { throw new QueryParsingException(parseContext, "[" + NAMES[0] + "] required field 'field' missing"); } SearchContext searchContext = SearchContext.current(); MappedFieldType fieldType = searchContext.mapperService().smartNameFieldType(field); IndexNumericFieldData fieldData = null; if (fieldType == null) { if(missing == null) { throw new ElasticsearchException("Unable to find a field mapper for field [" + field + "]. No 'missing' value defined."); } } else { fieldData = searchContext.fieldData().getForField(fieldType); } return new FieldValueFactorFunction(field, boostFactor, modifier, missing, fieldData); }
Example 9
Source File: ChildrenQuery.java From Elasticsearch with Apache License 2.0 | 4 votes |
@Override public Weight doCreateWeight(IndexSearcher searcher, boolean needsScores) throws IOException { SearchContext sc = SearchContext.current(); IndexParentChildFieldData globalIfd = ifd.loadGlobal((DirectoryReader)searcher.getIndexReader()); if (globalIfd == null) { // No docs of the specified type exist on this shard return new BooleanQuery.Builder().build().createWeight(searcher, needsScores); } boolean abort = true; long numFoundParents; ParentCollector collector = null; try { if (minChildren == 0 && maxChildren == 0 && scoreType != ScoreType.NONE) { switch (scoreType) { case MIN: collector = new MinCollector(globalIfd, sc, parentType); break; case MAX: collector = new MaxCollector(globalIfd, sc, parentType); break; case SUM: collector = new SumCollector(globalIfd, sc, parentType); break; } } if (collector == null) { switch (scoreType) { case MIN: collector = new MinCountCollector(globalIfd, sc, parentType); break; case MAX: collector = new MaxCountCollector(globalIfd, sc, parentType); break; case SUM: case AVG: collector = new SumCountAndAvgCollector(globalIfd, sc, parentType); break; case NONE: collector = new CountCollector(globalIfd, sc, parentType); break; default: throw new RuntimeException("Are we missing a score type here? -- " + scoreType); } } searcher.search(childQuery, collector); numFoundParents = collector.foundParents(); if (numFoundParents == 0) { return new BooleanQuery.Builder().build().createWeight(searcher, needsScores); } abort = false; } finally { if (abort) { Releasables.close(collector); } } sc.addReleasable(collector, Lifetime.COLLECTION); final Filter parentFilter; if (numFoundParents <= shortCircuitParentDocSet) { parentFilter = ParentIdsFilter.createShortCircuitFilter(nonNestedDocsFilter, sc, parentType, collector.values, collector.parentIdxs, numFoundParents); } else { parentFilter = this.parentFilter; } return new ParentWeight(this, childQuery.createWeight(searcher, needsScores), parentFilter, numFoundParents, collector, minChildren, maxChildren); }