Java Code Examples for org.elasticsearch.search.sort.SortParseElement

The following examples show how to use org.elasticsearch.search.sort.SortParseElement. These examples are extracted from open source projects. 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: jframe   Source File: TestTransportClient.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testScrollSearch() {
    QueryBuilder qb = QueryBuilders.termQuery("multi", "test");

    // 100 hits per shard will be returned for each scroll
    SearchResponse scrollResp = client.prepareSearch("index1").addSort(SortParseElement.DOC_FIELD_NAME, SortOrder.ASC)
            .setScroll(new TimeValue(60000)).setQuery(qb).setSize(100).execute().actionGet();
    // Scroll until no hits are returned
    while (true) {

        for (SearchHit hit : scrollResp.getHits().getHits()) {
            // Handle the hit...
        }
        scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
        // Break condition: No hits are returned
        if (scrollResp.getHits().getHits().length == 0) {
            break;
        }
    }
}
 
Example 2
Source Project: Elasticsearch   Source File: SortSymbolVisitor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * generate a SortField from a Reference symbol.
 *
 * the implementation is similar to what {@link org.elasticsearch.search.sort.SortParseElement}
 * does.
 */
@Override
public SortField visitReference(final Reference symbol, final SortSymbolContext context) {
    // can't use the SortField(fieldName, type) constructor
    // because values are saved using docValues and therefore they're indexed in lucene as binary and not
    // with the reference valueType.
    // this is why we use a custom comparator source with the same logic as ES

    ColumnIdent columnIdent = symbol.info().ident().columnIdent();

    if (columnIdent.isColumn()) {
        if (SortParseElement.SCORE_FIELD_NAME.equals(columnIdent.name())) {
            return !context.reverseFlag ? SORT_SCORE_REVERSE : SortParseElement.SORT_SCORE;
        } else if (DocSysColumns.RAW.equals(columnIdent) || DocSysColumns.ID.equals(columnIdent)) {
            return customSortField(DocSysColumns.nameForLucene(columnIdent), symbol, context,
                    LUCENE_TYPE_MAP.get(symbol.valueType()), false);
        }
    }

    MultiValueMode sortMode = context.reverseFlag ? MultiValueMode.MAX : MultiValueMode.MIN;

    String indexName;
    IndexFieldData.XFieldComparatorSource fieldComparatorSource;
    MappedFieldType fieldType = context.context.mapperService().smartNameFieldType(columnIdent.fqn());
    if (fieldType == null){
        indexName = columnIdent.fqn();
        fieldComparatorSource = new NullFieldComparatorSource(LUCENE_TYPE_MAP.get(symbol.valueType()), context.reverseFlag, context.nullFirst);
    } else {
        indexName = fieldType.names().indexName();
        fieldComparatorSource = context.context.fieldData()
                .getForField(fieldType)
                .comparatorSource(SortOrder.missing(context.reverseFlag, context.nullFirst), sortMode, null);
    }
    return new SortField(
            indexName,
            fieldComparatorSource,
            context.reverseFlag
    );
}
 
Example 3
Source Project: Elasticsearch   Source File: PercolatorService.java    License: Apache License 2.0 5 votes vote down vote up
@Inject
public PercolatorService(Settings settings, IndexNameExpressionResolver indexNameExpressionResolver, IndicesService indicesService,
                         PageCacheRecycler pageCacheRecycler, BigArrays bigArrays,
                         HighlightPhase highlightPhase, ClusterService clusterService,
                         AggregationPhase aggregationPhase, ScriptService scriptService,
                         MappingUpdatedAction mappingUpdatedAction) {
    super(settings);
    this.indexNameExpressionResolver = indexNameExpressionResolver;
    this.parseFieldMatcher = new ParseFieldMatcher(settings);
    this.indicesService = indicesService;
    this.pageCacheRecycler = pageCacheRecycler;
    this.bigArrays = bigArrays;
    this.clusterService = clusterService;
    this.highlightPhase = highlightPhase;
    this.aggregationPhase = aggregationPhase;
    this.scriptService = scriptService;
    this.mappingUpdatedAction = mappingUpdatedAction;
    this.sortParseElement = new SortParseElement();

    final long maxReuseBytes = settings.getAsBytesSize("indices.memory.memory_index.size_per_thread", new ByteSizeValue(1, ByteSizeUnit.MB)).bytes();
    cache = new CloseableThreadLocal<MemoryIndex>() {
        @Override
        protected MemoryIndex initialValue() {
            // TODO: should we expose payloads as an option? should offsets be turned on always?
            return new ExtendedMemoryIndex(true, false, maxReuseBytes);
        }
    };
    single = new SingleDocumentPercolatorIndex(cache);
    multi = new MultiDocumentPercolatorIndex(cache);

    percolatorTypes = new IntObjectHashMap<>(6);
    percolatorTypes.put(countPercolator.id(), countPercolator);
    percolatorTypes.put(queryCountPercolator.id(), queryCountPercolator);
    percolatorTypes.put(matchPercolator.id(), matchPercolator);
    percolatorTypes.put(queryPercolator.id(), queryPercolator);
    percolatorTypes.put(scoringPercolator.id(), scoringPercolator);
    percolatorTypes.put(topMatchingPercolator.id(), topMatchingPercolator);
}
 
Example 4
Source Project: Elasticsearch   Source File: TopHitsParser.java    License: Apache License 2.0 5 votes vote down vote up
@Inject
public TopHitsParser(FetchPhase fetchPhase, SortParseElement sortParseElement, FetchSourceParseElement sourceParseElement,
        HighlighterParseElement highlighterParseElement, FieldDataFieldsParseElement fieldDataFieldsParseElement,
        ScriptFieldsParseElement scriptFieldsParseElement, FieldsParseElement fieldsParseElement) {
    this.fetchPhase = fetchPhase;
    this.sortParseElement = sortParseElement;
    this.sourceParseElement = sourceParseElement;
    this.highlighterParseElement = highlighterParseElement;
    this.fieldDataFieldsParseElement = fieldDataFieldsParseElement;
    this.scriptFieldsParseElement = scriptFieldsParseElement;
    this.fieldsParseElement = fieldsParseElement;
}
 
Example 5
Source Project: Elasticsearch   Source File: InnerHitsFetchSubPhase.java    License: Apache License 2.0 5 votes vote down vote up
@Inject
public InnerHitsFetchSubPhase(SortParseElement sortParseElement, FetchSourceParseElement sourceParseElement, HighlighterParseElement highlighterParseElement, FieldDataFieldsParseElement fieldDataFieldsParseElement, ScriptFieldsParseElement scriptFieldsParseElement) {
    this.sortParseElement = sortParseElement;
    this.sourceParseElement = sourceParseElement;
    this.highlighterParseElement = highlighterParseElement;
    this.fieldDataFieldsParseElement = fieldDataFieldsParseElement;
    this.scriptFieldsParseElement = scriptFieldsParseElement;
}
 
Example 6
Source Project: Elasticsearch   Source File: InnerHitsParseElement.java    License: Apache License 2.0 5 votes vote down vote up
public InnerHitsParseElement(SortParseElement sortParseElement, FetchSourceParseElement sourceParseElement, HighlighterParseElement highlighterParseElement, FieldDataFieldsParseElement fieldDataFieldsParseElement, ScriptFieldsParseElement scriptFieldsParseElement) {
    this.sortParseElement = sortParseElement;
    this.sourceParseElement = sourceParseElement;
    this.highlighterParseElement = highlighterParseElement;
    this.fieldDataFieldsParseElement = fieldDataFieldsParseElement;
    this.scriptFieldsParseElement = scriptFieldsParseElement;
}
 
Example 7
Source Project: Elasticsearch   Source File: InnerHitsQueryParserHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Inject
public InnerHitsQueryParserHelper(SortParseElement sortParseElement, FetchSourceParseElement sourceParseElement, HighlighterParseElement highlighterParseElement, ScriptFieldsParseElement scriptFieldsParseElement, FieldDataFieldsParseElement fieldDataFieldsParseElement) {
    this.sortParseElement = sortParseElement;
    this.sourceParseElement = sourceParseElement;
    this.highlighterParseElement = highlighterParseElement;
    this.scriptFieldsParseElement = scriptFieldsParseElement;
    this.fieldDataFieldsParseElement = fieldDataFieldsParseElement;
}
 
Example 8
@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
	super.initialize(context);
	logger = context.getLogger();
	client = esResource.getClient();
	esIndex = esResource.getIndex();

	try {
		XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("match").startObject()
				.field("DocumentLanguage", language).endObject().endObject();

		// retrieve all ids
		totalIdList = new ArrayList<String>();
		SearchResponse scrollResp = client.prepareSearch(esIndex)
				.addSort(SortParseElement.DOC_FIELD_NAME, SortOrder.ASC).setScroll(new TimeValue(60000))
				.setQuery(builder).setSize(10000).execute().actionGet();
		while (true) {
			for (SearchHit hit : scrollResp.getHits().getHits()) {
				totalIdList.add(hit.getId());
			}
			scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000))
					.execute().actionGet();
			// Break condition: No hits are returned
			if (scrollResp.getHits().getHits().length == 0) {
				break;
			}
		}

		totalRecords = totalIdList.size();
		logger.log(Level.INFO, "Found " + totalRecords + " for language " + language + " in index");

	} catch (IOException e) {
		e.printStackTrace();
		System.exit(1);
	}

	// System.exit(1);

}
 
Example 9
Source Project: Elasticsearch   Source File: InnerHitsQueryParserHelper.java    License: Apache License 2.0 4 votes vote down vote up
public static void parseCommonInnerHitOptions(XContentParser parser, XContentParser.Token token, String fieldName, SubSearchContext subSearchContext,
                                              SortParseElement sortParseElement, FetchSourceParseElement sourceParseElement, HighlighterParseElement highlighterParseElement,
                                              ScriptFieldsParseElement scriptFieldsParseElement, FieldDataFieldsParseElement fieldDataFieldsParseElement) throws Exception {
    if ("sort".equals(fieldName)) {
        sortParseElement.parse(parser, subSearchContext);
    } else if ("_source".equals(fieldName)) {
        sourceParseElement.parse(parser, subSearchContext);
    } else if (token == XContentParser.Token.START_OBJECT) {
        switch (fieldName) {
            case "highlight":
                highlighterParseElement.parse(parser, subSearchContext);
                break;
            case "scriptFields":
            case "script_fields":
                scriptFieldsParseElement.parse(parser, subSearchContext);
                break;
            default:
                throw new IllegalArgumentException("Unknown key for a " + token + " for nested query: [" + fieldName + "].");
        }
    } else if (token == XContentParser.Token.START_ARRAY) {
        switch (fieldName) {
            case "fielddataFields":
            case "fielddata_fields":
                fieldDataFieldsParseElement.parse(parser, subSearchContext);
                break;
            case "fields":
                boolean added = false;
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    String name = parser.text();
                    added = true;
                    subSearchContext.fieldNames().add(name);
                }
                if (!added) {
                    subSearchContext.emptyFieldNames();
                }
                break;
            default:
                throw new IllegalArgumentException("Unknown key for a " + token + " for nested query: [" + fieldName + "].");
        }
    } else if (token.isValue()) {
        switch (fieldName) {
            case "from":
                subSearchContext.from(parser.intValue());
                break;
            case "size":
                subSearchContext.size(parser.intValue());
                break;
            case "track_scores":
            case "trackScores":
                subSearchContext.trackScores(parser.booleanValue());
                break;
            case "version":
                subSearchContext.version(parser.booleanValue());
                break;
            case "explain":
                subSearchContext.explain(parser.booleanValue());
                break;
            case "fields":
                subSearchContext.fieldNames().add(parser.text());
                break;
            default:
                throw new IllegalArgumentException("Unknown key for a " + token + " for nested query: [" + fieldName + "].");
        }
    }
}