org.elasticsearch.action.termvectors.TermVectorsRequest Java Examples

The following examples show how to use org.elasticsearch.action.termvectors.TermVectorsRequest. 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: MoreLikeThisQueryBuilder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Convert this to a {@link TermVectorsRequest} for fetching the terms of the document.
 */
public TermVectorsRequest toTermVectorsRequest() {
    TermVectorsRequest termVectorsRequest = new TermVectorsRequest(index, type, id)
            .selectedFields(fields)
            .routing(routing)
            .version(version)
            .versionType(versionType)
            .perFieldAnalyzer(perFieldAnalyzer)
            .positions(false)  // ensures these following parameters are never set
            .offsets(false)
            .payloads(false)
            .fieldStatistics(false)
            .termStatistics(false)
            .dfs(false);
    // for artificial docs to make sure that the id has changed in the item too
    if (doc != null) {
        termVectorsRequest.doc(doc, true);
        this.id(termVectorsRequest.id());
    }
    return termVectorsRequest;
}
 
Example #2
Source File: RestTermVectorsAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
static public void readURIParameters(TermVectorsRequest termVectorsRequest, RestRequest request) {
    String fields = request.param("fields");
    addFieldStringsFromParameter(termVectorsRequest, fields);
    termVectorsRequest.offsets(request.paramAsBoolean("offsets", termVectorsRequest.offsets()));
    termVectorsRequest.positions(request.paramAsBoolean("positions", termVectorsRequest.positions()));
    termVectorsRequest.payloads(request.paramAsBoolean("payloads", termVectorsRequest.payloads()));
    termVectorsRequest.routing(request.param("routing"));
    termVectorsRequest.realtime(request.paramAsBoolean("realtime", null));
    termVectorsRequest.version(RestActions.parseVersion(request, termVectorsRequest.version()));
    termVectorsRequest.versionType(VersionType.fromString(request.param("version_type"), termVectorsRequest.versionType()));
    termVectorsRequest.parent(request.param("parent"));
    termVectorsRequest.preference(request.param("preference"));
    termVectorsRequest.termStatistics(request.paramAsBoolean("termStatistics", termVectorsRequest.termStatistics()));
    termVectorsRequest.termStatistics(request.paramAsBoolean("term_statistics", termVectorsRequest.termStatistics()));
    termVectorsRequest.fieldStatistics(request.paramAsBoolean("fieldStatistics", termVectorsRequest.fieldStatistics()));
    termVectorsRequest.fieldStatistics(request.paramAsBoolean("field_statistics", termVectorsRequest.fieldStatistics()));
    termVectorsRequest.dfs(request.paramAsBoolean("dfs", termVectorsRequest.dfs()));
}
 
Example #3
Source File: RestTermVectorsAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
static public void addFieldStringsFromParameter(TermVectorsRequest termVectorsRequest, String fields) {
    Set<String> selectedFields = termVectorsRequest.selectedFields();
    if (fields != null) {
        String[] paramFieldStrings = Strings.commaDelimitedListToStringArray(fields);
        for (String field : paramFieldStrings) {
            if (selectedFields == null) {
                selectedFields = new HashSet<>();
            }
            if (!selectedFields.contains(field)) {
                field = field.replaceAll("\\s", "");
                selectedFields.add(field);
            }
        }
    }
    if (selectedFields != null) {
        termVectorsRequest.selectedFields(selectedFields.toArray(new String[selectedFields.size()]));
    }
}
 
Example #4
Source File: ShardTermVectorsService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void handleFieldWildcards(TermVectorsRequest request) {
    Set<String> fieldNames = new HashSet<>();
    for (String pattern : request.selectedFields()) {
        fieldNames.addAll(indexShard.mapperService().simpleMatchToIndexNames(pattern));
    }
    request.selectedFields(fieldNames.toArray(Strings.EMPTY_ARRAY));
}
 
Example #5
Source File: ShardTermVectorsService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private Fields addGeneratedTermVectors(Engine.GetResult get, Fields termVectorsByField, TermVectorsRequest request, Set<String> selectedFields) throws IOException {
    /* only keep valid fields */
    Set<String> validFields = new HashSet<>();
    for (String field : selectedFields) {
        MappedFieldType fieldType = indexShard.mapperService().smartNameFieldType(field);
        if (!isValidField(fieldType)) {
            continue;
        }
        // already retrieved, only if the analyzer hasn't been overridden at the field
        if (fieldType.storeTermVectors() &&
                (request.perFieldAnalyzer() == null || !request.perFieldAnalyzer().containsKey(field))) {
            continue;
        }
        validFields.add(field);
    }

    if (validFields.isEmpty()) {
        return termVectorsByField;
    }

    /* generate term vectors from fetched document fields */
    GetResult getResult = indexShard.getService().get(
            get, request.id(), request.type(), validFields.toArray(Strings.EMPTY_ARRAY), null, false);
    Fields generatedTermVectors = generateTermVectors(getResult.getFields().values(), request.offsets(), request.perFieldAnalyzer(), validFields);

    /* merge with existing Fields */
    if (termVectorsByField == null) {
        return generatedTermVectors;
    } else {
        return mergeFields(termVectorsByField, generatedTermVectors);
    }
}
 
Example #6
Source File: RestTermVectorsAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws Exception {
    TermVectorsRequest termVectorsRequest = new TermVectorsRequest(request.param("index"), request.param("type"), request.param("id"));
    if (RestActions.hasBodyContent(request)) {
        try (XContentParser parser = XContentFactory.xContent(RestActions.guessBodyContentType(request)).createParser(RestActions.getRestContent(request))){
            TermVectorsRequest.parseRequest(termVectorsRequest, parser);
        }
    }
    readURIParameters(termVectorsRequest, request);

    client.termVectors(termVectorsRequest, new RestToXContentListener<TermVectorsResponse>(channel));
}
 
Example #7
Source File: RestMultiTermVectorsAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws Exception {
    MultiTermVectorsRequest multiTermVectorsRequest = new MultiTermVectorsRequest();
    TermVectorsRequest template = new TermVectorsRequest();
    template.index(request.param("index"));
    template.type(request.param("type"));
    RestTermVectorsAction.readURIParameters(template, request);
    multiTermVectorsRequest.ids(Strings.commaDelimitedListToStringArray(request.param("ids")));
    multiTermVectorsRequest.add(template, RestActions.getRestContent(request));

    client.multiTermVectors(multiTermVectorsRequest, new RestToXContentListener<MultiTermVectorsResponse>(channel));
}
 
Example #8
Source File: FessEsClient.java    From fess with Apache License 2.0 4 votes vote down vote up
@Override
public void termVectors(final TermVectorsRequest request, final ActionListener<TermVectorsResponse> listener) {
    client.termVectors(request, listener);
}
 
Example #9
Source File: FessEsClient.java    From fess with Apache License 2.0 4 votes vote down vote up
@Override
public ActionFuture<TermVectorsResponse> termVectors(final TermVectorsRequest request) {
    return client.termVectors(request);
}
 
Example #10
Source File: GenTermValuesHandler.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
@Override
public void onResponse(final SearchResponse response) {
    if (mTVListener != null) {
        try {
            mTVListener.await();
        } catch (final InterruptedException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Interrupted.", e);
            }
        }
    }

    if (interrupted) {
        return;
    }

    final SearchHits searchHits = response.getHits();
    final SearchHit[] hits = searchHits.getHits();
    if (hits.length == 0) {
        scrollSearchGate.countDown();
        shutdown();
    } else {
        final Map<String, DocInfo> idMap = new HashMap<>(hits.length);
        final MultiTermVectorsRequestBuilder requestBuilder = client
                .prepareMultiTermVectors();
        for (final SearchHit hit : hits) {
            final String id = hit.getId();
            final SearchHitField searchHitField = hit.field(idField);
            if (searchHitField != null) {
                idMap.put(id,
                        new DocInfo((String) searchHitField.getValue(),
                                hit.getSource()));
            }
            final TermVectorsRequest termVectorRequest = new TermVectorsRequest(
                    sourceIndex, sourceType, id);
            termVectorRequest.selectedFields(sourceFields);
            requestBuilder.add(termVectorRequest);
        }
        mTVListener = new MultiTermVectorsListener(numOfThreads,
                requestHandlers, eventParams, idMap, executor, logger);
        requestBuilder.execute(mTVListener);

        client.prepareSearchScroll(response.getScrollId())
                .setScroll(new TimeValue(keepAlive.longValue()))
                .execute(this);
    }
}
 
Example #11
Source File: ClientWithStats.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
@Deprecated
public void termVector(TermVectorsRequest request, ActionListener<TermVectorsResponse> listener) {
	wrapped.termVector(request, listener);
}
 
Example #12
Source File: ClientWithStats.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
@Deprecated
public ActionFuture<TermVectorsResponse> termVector(TermVectorsRequest request) {
	return wrapped.termVector(request);
}
 
Example #13
Source File: ClientWithStats.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void termVectors(TermVectorsRequest request, ActionListener<TermVectorsResponse> listener) {
	wrapped.termVectors(request, listener);
}
 
Example #14
Source File: ClientWithStats.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public ActionFuture<TermVectorsResponse> termVectors(TermVectorsRequest request) {
	return wrapped.termVectors(request);
}
 
Example #15
Source File: ShardTermVectorsService.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private AggregatedDfs getAggregatedDfs(Fields termVectorsFields, TermVectorsRequest request) throws IOException {
    DfsOnlyRequest dfsOnlyRequest = new DfsOnlyRequest(termVectorsFields, new String[]{request.index()},
            new String[]{request.type()}, request.selectedFields());
    DfsOnlyResponse response = dfsAction.execute(dfsOnlyRequest).actionGet();
    return response.getDfs();
}
 
Example #16
Source File: ShardTermVectorsService.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private boolean useDfs(TermVectorsRequest request) {
    return request.dfs() && (request.fieldStatistics() || request.termStatistics());
}
 
Example #17
Source File: MoreLikeThisQueryBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Parses and returns the given item.
 */
public static Item parse(XContentParser parser, ParseFieldMatcher parseFieldMatcher, Item item) throws IOException {
    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (currentFieldName != null) {
            if (parseFieldMatcher.match(currentFieldName, Field.INDEX)) {
                item.index = parser.text();
            } else if (parseFieldMatcher.match(currentFieldName, Field.TYPE)) {
                item.type = parser.text();
            } else if (parseFieldMatcher.match(currentFieldName, Field.ID)) {
                item.id = parser.text();
            } else if (parseFieldMatcher.match(currentFieldName, Field.DOC)) {
                item.doc(jsonBuilder().copyCurrentStructure(parser));
            } else if (parseFieldMatcher.match(currentFieldName, Field.FIELDS)) {
                if (token == XContentParser.Token.START_ARRAY) {
                    List<String> fields = new ArrayList<>();
                    while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                        fields.add(parser.text());
                    }
                    item.fields(fields.toArray(new String[fields.size()]));
                } else {
                    throw new ElasticsearchParseException(
                            "failed to parse More Like This item. field [fields] must be an array");
                }
            } else if (parseFieldMatcher.match(currentFieldName, Field.PER_FIELD_ANALYZER)) {
                item.perFieldAnalyzer(TermVectorsRequest.readPerFieldAnalyzer(parser.map()));
            } else if ("_routing".equals(currentFieldName) || "routing".equals(currentFieldName)) {
                item.routing = parser.text();
            } else if ("_version".equals(currentFieldName) || "version".equals(currentFieldName)) {
                item.version = parser.longValue();
            } else if ("_version_type".equals(currentFieldName) || "_versionType".equals(currentFieldName)
                    || "version_type".equals(currentFieldName) || "versionType".equals(currentFieldName)) {
                item.versionType = VersionType.fromString(parser.text());
            } else {
                throw new ElasticsearchParseException(
                        "failed to parse More Like This item. unknown field [{}]", currentFieldName);
            }
        }
    }
    if (item.id != null && item.doc != null) {
        throw new ElasticsearchParseException(
                "failed to parse More Like This item. either [id] or [doc] can be specified, but not both!");
    }
    return item;
}