use of org.elasticsearch.action.termvectors.TermVectorsResponse in project elasticsearch by elastic.
the class TermVectorsServiceTests method testTook.
public void testTook() throws Exception {
XContentBuilder mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field").field("type", "text").field("term_vector", "with_positions_offsets_payloads").endObject().endObject().endObject().endObject();
createIndex("test", Settings.EMPTY, "type1", mapping);
ensureGreen();
client().prepareIndex("test", "type1", "0").setSource("field", "foo bar").setRefreshPolicy(IMMEDIATE).get();
IndicesService indicesService = getInstanceFromNode(IndicesService.class);
IndexService test = indicesService.indexService(resolveIndex("test"));
IndexShard shard = test.getShardOrNull(0);
assertThat(shard, notNullValue());
List<Long> longs = Stream.of(abs(randomLong()), abs(randomLong())).sorted().collect(toList());
TermVectorsRequest request = new TermVectorsRequest("test", "type1", "0");
TermVectorsResponse response = TermVectorsService.getTermVectors(shard, request, longs.iterator()::next);
assertThat(response, notNullValue());
assertThat(response.getTookInMillis(), equalTo(TimeUnit.NANOSECONDS.toMillis(longs.get(1) - longs.get(0))));
}
use of org.elasticsearch.action.termvectors.TermVectorsResponse in project elasticsearch by elastic.
the class TermVectorsService method getTermVectors.
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 = new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(request.type(), request.id()));
Engine.GetResult get = indexShard.get(new Engine.Get(request.realtime(), uidTerm).version(request.version()).versionType(request.versionType()));
Fields termVectorsByField = null;
AggregatedDfs dfs = null;
TermVectorsFilter termVectorsFilter = null;
/* handle potential wildcards in fields */
if (request.selectedFields() != null) {
handleFieldWildcards(indexShard, request);
}
final Engine.Searcher searcher = indexShard.acquireSearcher("term_vector");
try {
Fields topLevelFields = MultiFields.getFields(get.searcher() != null ? get.searcher().reader() : searcher.reader());
Versions.DocIdAndVersion docIdAndVersion = get.docIdAndVersion();
/* from an artificial document */
if (request.doc() != null) {
termVectorsByField = generateTermVectorsFromDoc(indexShard, request);
// if no document indexed in shard, take the queried document itself for stats
if (topLevelFields == null) {
topLevelFields = termVectorsByField;
}
termVectorsResponse.setArtificial(true);
termVectorsResponse.setExists(true);
} else /* or from an existing document */
if (docIdAndVersion != null) {
// fields with stored term vectors
termVectorsByField = docIdAndVersion.context.reader().getTermVectors(docIdAndVersion.docId);
Set<String> selectedFields = request.selectedFields();
// generate tvs for fields where analyzer is overridden
if (selectedFields == null && request.perFieldAnalyzer() != null) {
selectedFields = getFieldsToGenerate(request.perFieldAnalyzer(), termVectorsByField);
}
// fields without term vectors
if (selectedFields != null) {
termVectorsByField = addGeneratedTermVectors(indexShard, get, termVectorsByField, request, selectedFields);
}
termVectorsResponse.setDocVersion(docIdAndVersion.version);
termVectorsResponse.setExists(true);
} else /* no term vectors generated or found */
{
termVectorsResponse.setExists(false);
}
/* if there are term vectors, optional compute dfs and/or terms filtering */
if (termVectorsByField != null) {
if (request.filterSettings() != null) {
termVectorsFilter = new TermVectorsFilter(termVectorsByField, topLevelFields, request.selectedFields(), dfs);
termVectorsFilter.setSettings(request.filterSettings());
try {
termVectorsFilter.selectBestTerms();
} catch (IOException e) {
throw new ElasticsearchException("failed to select best terms", e);
}
}
// write term vectors
termVectorsResponse.setFields(termVectorsByField, request.selectedFields(), request.getFlags(), topLevelFields, dfs, termVectorsFilter);
}
termVectorsResponse.setTookInMillis(TimeUnit.NANOSECONDS.toMillis(nanoTimeSupplier.getAsLong() - startTime));
} catch (Exception ex) {
throw new ElasticsearchException("failed to execute term vector request", ex);
} finally {
searcher.close();
get.release();
}
return termVectorsResponse;
}
use of org.elasticsearch.action.termvectors.TermVectorsResponse in project elasticsearch by elastic.
the class MoreLikeThisQueryBuilder method getFieldsFor.
private static Fields[] getFieldsFor(MultiTermVectorsResponse responses) throws IOException {
List<Fields> likeFields = new ArrayList<>();
for (MultiTermVectorsItemResponse response : responses) {
if (response.isFailed()) {
continue;
}
TermVectorsResponse getResponse = response.getResponse();
if (!getResponse.isExists()) {
continue;
}
likeFields.add(getResponse.getFields());
}
return likeFields.toArray(Fields.EMPTY_ARRAY);
}
use of org.elasticsearch.action.termvectors.TermVectorsResponse in project elasticsearch by elastic.
the class MoreLikeThisQueryBuilderTests method executeMultiTermVectors.
@Override
protected MultiTermVectorsResponse executeMultiTermVectors(MultiTermVectorsRequest mtvRequest) {
try {
MultiTermVectorsItemResponse[] responses = new MultiTermVectorsItemResponse[mtvRequest.size()];
int i = 0;
for (TermVectorsRequest request : mtvRequest) {
TermVectorsResponse response = new TermVectorsResponse(request.index(), request.type(), request.id());
response.setExists(true);
Fields generatedFields;
if (request.doc() != null) {
generatedFields = generateFields(randomFields, request.doc().utf8ToString());
} else {
generatedFields = generateFields(request.selectedFields().toArray(new String[request.selectedFields().size()]), request.id());
}
EnumSet<TermVectorsRequest.Flag> flags = EnumSet.of(TermVectorsRequest.Flag.Positions, TermVectorsRequest.Flag.Offsets);
response.setFields(generatedFields, request.selectedFields(), flags, generatedFields);
responses[i++] = new MultiTermVectorsItemResponse(response, null);
}
return new MultiTermVectorsResponse(responses);
} catch (IOException ex) {
throw new ElasticsearchException("boom", ex);
}
}
use of org.elasticsearch.action.termvectors.TermVectorsResponse in project elasticsearch by elastic.
the class SimpleRoutingIT method testRequiredRoutingMappingVariousAPIs.
public void testRequiredRoutingMappingVariousAPIs() throws Exception {
client().admin().indices().prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("_routing").field("required", true).endObject().endObject().endObject()).execute().actionGet();
ensureGreen();
logger.info("--> indexing with id [1], and routing [0]");
client().prepareIndex(indexOrAlias(), "type1", "1").setRouting("0").setSource("field", "value1").get();
logger.info("--> indexing with id [2], and routing [0]");
client().prepareIndex(indexOrAlias(), "type1", "2").setRouting("0").setSource("field", "value2").setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
logger.info("--> verifying get with id [1] with routing [0], should succeed");
assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(true));
logger.info("--> verifying get with id [1], with no routing, should fail");
try {
client().prepareGet(indexOrAlias(), "type1", "1").get();
fail();
} catch (RoutingMissingException e) {
assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
}
logger.info("--> verifying explain with id [2], with routing [0], should succeed");
ExplainResponse explainResponse = client().prepareExplain(indexOrAlias(), "type1", "2").setQuery(QueryBuilders.matchAllQuery()).setRouting("0").get();
assertThat(explainResponse.isExists(), equalTo(true));
assertThat(explainResponse.isMatch(), equalTo(true));
logger.info("--> verifying explain with id [2], with no routing, should fail");
try {
client().prepareExplain(indexOrAlias(), "type1", "2").setQuery(QueryBuilders.matchAllQuery()).get();
fail();
} catch (RoutingMissingException e) {
assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[2]"));
}
logger.info("--> verifying term vector with id [1], with routing [0], should succeed");
TermVectorsResponse termVectorsResponse = client().prepareTermVectors(indexOrAlias(), "type1", "1").setRouting("0").get();
assertThat(termVectorsResponse.isExists(), equalTo(true));
assertThat(termVectorsResponse.getId(), equalTo("1"));
try {
client().prepareTermVectors(indexOrAlias(), "type1", "1").get();
fail();
} catch (RoutingMissingException e) {
assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
}
UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setRouting("0").setDoc(Requests.INDEX_CONTENT_TYPE, "field1", "value1").get();
assertThat(updateResponse.getId(), equalTo("1"));
assertThat(updateResponse.getVersion(), equalTo(2L));
try {
client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field1", "value1").get();
fail();
} catch (RoutingMissingException e) {
assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
}
logger.info("--> verifying mget with ids [1,2], with routing [0], should succeed");
MultiGetResponse multiGetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").routing("0")).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").routing("0")).get();
assertThat(multiGetResponse.getResponses().length, equalTo(2));
assertThat(multiGetResponse.getResponses()[0].isFailed(), equalTo(false));
assertThat(multiGetResponse.getResponses()[0].getResponse().getId(), equalTo("1"));
assertThat(multiGetResponse.getResponses()[1].isFailed(), equalTo(false));
assertThat(multiGetResponse.getResponses()[1].getResponse().getId(), equalTo("2"));
logger.info("--> verifying mget with ids [1,2], with no routing, should fail");
multiGetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1")).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2")).get();
assertThat(multiGetResponse.getResponses().length, equalTo(2));
assertThat(multiGetResponse.getResponses()[0].isFailed(), equalTo(true));
assertThat(multiGetResponse.getResponses()[0].getFailure().getId(), equalTo("1"));
assertThat(multiGetResponse.getResponses()[0].getFailure().getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
assertThat(multiGetResponse.getResponses()[1].isFailed(), equalTo(true));
assertThat(multiGetResponse.getResponses()[1].getFailure().getId(), equalTo("2"));
assertThat(multiGetResponse.getResponses()[1].getFailure().getMessage(), equalTo("routing is required for [test]/[type1]/[2]"));
MultiTermVectorsResponse multiTermVectorsResponse = client().prepareMultiTermVectors().add(new TermVectorsRequest(indexOrAlias(), "type1", "1").routing("0")).add(new TermVectorsRequest(indexOrAlias(), "type1", "2").routing("0")).get();
assertThat(multiTermVectorsResponse.getResponses().length, equalTo(2));
assertThat(multiTermVectorsResponse.getResponses()[0].getId(), equalTo("1"));
assertThat(multiTermVectorsResponse.getResponses()[0].isFailed(), equalTo(false));
assertThat(multiTermVectorsResponse.getResponses()[0].getResponse().getId(), equalTo("1"));
assertThat(multiTermVectorsResponse.getResponses()[0].getResponse().isExists(), equalTo(true));
assertThat(multiTermVectorsResponse.getResponses()[1].getId(), equalTo("2"));
assertThat(multiTermVectorsResponse.getResponses()[1].isFailed(), equalTo(false));
assertThat(multiTermVectorsResponse.getResponses()[1].getResponse().getId(), equalTo("2"));
assertThat(multiTermVectorsResponse.getResponses()[1].getResponse().isExists(), equalTo(true));
multiTermVectorsResponse = client().prepareMultiTermVectors().add(new TermVectorsRequest(indexOrAlias(), "type1", "1")).add(new TermVectorsRequest(indexOrAlias(), "type1", "2")).get();
assertThat(multiTermVectorsResponse.getResponses().length, equalTo(2));
assertThat(multiTermVectorsResponse.getResponses()[0].getId(), equalTo("1"));
assertThat(multiTermVectorsResponse.getResponses()[0].isFailed(), equalTo(true));
assertThat(multiTermVectorsResponse.getResponses()[0].getFailure().getCause().getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
assertThat(multiTermVectorsResponse.getResponses()[0].getResponse(), nullValue());
assertThat(multiTermVectorsResponse.getResponses()[1].getId(), equalTo("2"));
assertThat(multiTermVectorsResponse.getResponses()[1].isFailed(), equalTo(true));
assertThat(multiTermVectorsResponse.getResponses()[1].getResponse(), nullValue());
assertThat(multiTermVectorsResponse.getResponses()[1].getFailure().getCause().getMessage(), equalTo("routing is required for [test]/[type1]/[2]"));
}
Aggregations