/** * Convert this to a {@link TermVectorsRequest} for fetching the terms of the document. */ 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); // for artificial docs to make sure that the id has changed in the item too if (doc != null) { termVectorsRequest.doc(doc, true, xContentType); this.id = termVectorsRequest.id(); } return termVectorsRequest; }
/** * Constructs a new term vector request for a document that will be fetch * from the provided index. Use {@link #type(String)} and * {@link #id(String)} to specify the document to load. */ public TermVectorsRequest(TermVectorsRequest other) { super(other.index()); this.id = other.id(); this.type = other.type(); if (other.doc != null) { this.doc = new BytesArray(other.doc().toBytesRef(), true); this.xContentType = other.xContentType; } this.flagsEnum = other.getFlags().clone(); this.preference = other.preference(); this.routing = other.routing(); this.parent = other.parent(); if (other.selectedFields != null) { this.selectedFields = new HashSet<>(other.selectedFields); } if (other.perFieldAnalyzer != null) { this.perFieldAnalyzer = new HashMap<>(other.perFieldAnalyzer); } this.realtime = other.realtime(); this.version = other.version(); this.versionType = VersionType.fromValue(other.versionType().getValue()); this.filterSettings = other.filterSettings(); }
public static TermVectorsRequest readTermVectorsRequest(StreamInput in) throws IOException { TermVectorsRequest termVectorsRequest = new TermVectorsRequest(); termVectorsRequest.readFrom(in); return termVectorsRequest; }
public static 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", termVectorsRequest.realtime())); 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.offsets(parser.booleanValue()); } else if (POSITIONS.match(currentFieldName, parser.getDeprecationHandler())) { termVectorsRequest.positions(parser.booleanValue()); } else if (PAYLOADS.match(currentFieldName, parser.getDeprecationHandler())) { termVectorsRequest.payloads(parser.booleanValue()); } else if (currentFieldName.equals("term_statistics") || currentFieldName.equals("termStatistics")) { termVectorsRequest.termStatistics(parser.booleanValue()); } else if (currentFieldName.equals("field_statistics") || currentFieldName.equals("fieldStatistics")) { termVectorsRequest.fieldStatistics(parser.booleanValue()); } else if (DFS.match(currentFieldName, parser.getDeprecationHandler())) { throw new IllegalArgumentException("distributed frequencies is not supported anymore for term vectors"); } else if (currentFieldName.equals("per_field_analyzer") || currentFieldName.equals("perFieldAnalyzer")) { termVectorsRequest.perFieldAnalyzer(readPerFieldAnalyzer(parser.map())); } else if (FILTER.match(currentFieldName, parser.getDeprecationHandler())) { termVectorsRequest.filterSettings(readFilterSettings(parser)); } else if (INDEX.match(currentFieldName, parser.getDeprecationHandler())) { "either [id] or [doc] can be specified, but not both!"); termVectorsRequest.doc(jsonBuilder().copyCurrentStructure(parser)); } else if (ROUTING.match(currentFieldName, parser.getDeprecationHandler())) { termVectorsRequest.routing = parser.text(); termVectorsRequest.selectedFields(fields.toArray(fieldsAsArray));
static TermVectorsResponse getTermVectors(IndexShard indexShard, TermVectorsRequest request, LongSupplier nanoTimeSupplier) { final long startTime = nanoTimeSupplier.getAsLong(); final TermVectorsResponse termVectorsResponse = new TermVectorsResponse(indexShard.shardId().getIndex().getName(), request.type(), request.id()); final Term uidTerm = indexShard.mapperService().createUidTerm(request.type(), request.id()); if (uidTerm == null) { termVectorsResponse.setExists(false); return termVectorsResponse; Engine.GetResult get = indexShard.get(new Engine.Get(request.realtime(), false, request.type(), request.id(), uidTerm) .version(request.version()).versionType(request.versionType())); if (request.selectedFields() != null) { handleFieldWildcards(indexShard, request); if (request.doc() != null) { termVectorsByField = generateTermVectorsFromDoc(indexShard, request); Set<String> selectedFields = request.selectedFields(); if (selectedFields == null && request.perFieldAnalyzer() != null) { selectedFields = getFieldsToGenerate(request.perFieldAnalyzer(), termVectorsByField); if (request.filterSettings() != null) { termVectorsFilter = new TermVectorsFilter(termVectorsByField, topLevelFields, request.selectedFields(), dfs); termVectorsFilter.setSettings(request.filterSettings()); try { termVectorsFilter.selectBestTerms(); termVectorsResponse.setFields(termVectorsByField, request.selectedFields(),
@Override protected void resolveRequest(ClusterState state, InternalRequest request) { // update the routing (request#index here is possibly an alias or a parent) request.request().routing(state.metaData() .resolveIndexRouting(request.request().parent(), request.request().routing(), request.request().index())); // Fail fast on the node that received the request. if (request.request().routing() == null && state.getMetaData().routingRequired(request.concreteIndex(), request.request().type())) { throw new RoutingMissingException(request.concreteIndex(), request.request().type(), request.request().id()); } }
private static Fields generateTermVectorsFromDoc(IndexShard indexShard, TermVectorsRequest request) throws IOException { // parse the document, at the moment we do update the mapping, just like percolate ParsedDocument parsedDocument = parseDocument(indexShard, indexShard.shardId().getIndexName(), request.type(), request.doc(), request.xContentType()); // select the right fields and generate term vectors ParseContext.Document doc = parsedDocument.rootDoc(); Set<String> seenFields = new HashSet<>(); Collection<DocumentField> documentFields = new HashSet<>(); for (IndexableField field : doc.getFields()) { MappedFieldType fieldType = indexShard.mapperService().fullName(field.name()); if (!isValidField(fieldType)) { continue; } if (request.selectedFields() != null && !request.selectedFields().contains(field.name())) { continue; } if (seenFields.contains(field.name())) { continue; } else { seenFields.add(field.name()); } String[] values = doc.getValues(field.name()); documentFields.add(new DocumentField(field.name(), Arrays.asList((Object[]) values))); } return generateTermVectors(indexShard, XContentHelper.convertToMap(parsedDocument.source(), true, request.xContentType()).v2(), documentFields, request.offsets(), request.perFieldAnalyzer(), seenFields); }
@Override protected ShardIterator shards(ClusterState state, InternalRequest request) { if (request.request().doc() != null && request.request().routing() == null) { // artificial document without routing specified, ignore its "id" and use either random shard or according to preference GroupShardsIterator<ShardIterator> groupShardsIter = clusterService.operationRouting().searchShards(state, new String[] { request.concreteIndex() }, null, request.request().preference()); return groupShardsIter.iterator().next(); } return clusterService.operationRouting().getShards(state, request.concreteIndex(), request.request().id(), request.request().routing(), request.request().preference()); }
public TermVectorsRequest(MultiGetRequest.Item item) { super(item.index()); this.id = item.id(); this.type = item.type(); this.selectedFields(item.storedFields()); this.routing(item.routing()); this.parent(item.parent()); }
@Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { 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"))); request.withContentOrSourceParamParserOrNull(p -> multiTermVectorsRequest.add(template, p)); return channel -> client.multiTermVectors(multiTermVectorsRequest, new RestToXContentListener<MultiTermVectorsResponse>(channel)); }
(request.perFieldAnalyzer() == null || !request.perFieldAnalyzer().containsKey(field))) { continue; getFields[getFields.length - 1] = SourceFieldMapper.NAME; GetResult getResult = indexShard.getService().get( get, request.id(), request.type(), getFields, null); Fields generatedTermVectors = generateTermVectors(indexShard, getResult.sourceAsMap(), getResult.getFields().values(), request.offsets(), request.perFieldAnalyzer(), validFields);
throw new IllegalArgumentException("docs array element should include an object"); TermVectorsRequest termVectorsRequest = new TermVectorsRequest(template); TermVectorsRequest.parseRequest(termVectorsRequest, parser); add(termVectorsRequest); TermVectorsRequest.parseRequest(template, parser); } else { throw new ElasticsearchParseException("no parameter named [{}] and type OBJECT", currentFieldName); TermVectorsRequest curRequest = new TermVectorsRequest(template); curRequest.id(id); requests.add(curRequest);
@Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { TermVectorsRequest termVectorsRequest = new TermVectorsRequest(request.param("index"), request.param("type"), request.param("id")); if (request.hasContentOrSourceParam()) { try (XContentParser parser = request.contentOrSourceParamParser()) { TermVectorsRequest.parseRequest(termVectorsRequest, parser); } } readURIParameters(termVectorsRequest, request); return channel -> client.termVectors(termVectorsRequest, new RestToXContentListener<>(channel)); }
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(); } }
/** * Constructs a new term vector request builder for a document that will be fetch * from the provided index. Use {@code index}, {@code type} and * {@code id} to specify the document to load. */ public TermVectorsRequestBuilder(ElasticsearchClient client, TermVectorsAction action, String index, String type, String id) { super(client, action, new TermVectorsRequest(index, type, id)); }
/** * Sets whether to return only term vectors for special selected fields. Returns the term * vectors for all fields if selectedFields == null */ public TermVectorsRequestBuilder setSelectedFields(String... fields) { request.selectedFields(fields); return this; }
/** * Sets the artificial document from which to generate term vectors. */ public TermVectorsRequestBuilder setDoc(XContentBuilder xContent) { request.doc(xContent); return this; }
@Override public void onFailure(Exception e) { // create failures for all relevant requests for (int i = 0; i < shardRequest.locations.size(); i++) { TermVectorsRequest termVectorsRequest = shardRequest.requests.get(i); responses.set(shardRequest.locations.get(i), new MultiTermVectorsItemResponse(null, new MultiTermVectorsResponse.Failure(shardRequest.index(), termVectorsRequest.type(), termVectorsRequest.id(), e))); } if (counter.decrementAndGet() == 0) { finishHim(); } }
private boolean useDfs(TermVectorsRequest request) { return request.dfs() && (request.fieldStatistics() || request.termStatistics()); }