Search in sources :

Example 1 with MultiTermVectorsResponse

use of org.elasticsearch.action.termvectors.MultiTermVectorsResponse in project elasticsearch by elastic.

the class MoreLikeThisQueryBuilder method handleItems.

private Query handleItems(QueryShardContext context, MoreLikeThisQuery mltQuery, Item[] likeItems, Item[] unlikeItems, boolean include, List<String> moreLikeFields, boolean useDefaultField) throws IOException {
    // set default index, type and fields if not specified
    for (Item item : likeItems) {
        setDefaultIndexTypeFields(context, item, moreLikeFields, useDefaultField);
    }
    for (Item item : unlikeItems) {
        setDefaultIndexTypeFields(context, item, moreLikeFields, useDefaultField);
    }
    // fetching the items with multi-termvectors API
    MultiTermVectorsResponse likeItemsResponse = fetchResponse(context.getClient(), likeItems);
    // getting the Fields for liked items
    mltQuery.setLikeText(getFieldsFor(likeItemsResponse));
    // getting the Fields for unliked items
    if (unlikeItems.length > 0) {
        MultiTermVectorsResponse unlikeItemsResponse = fetchResponse(context.getClient(), unlikeItems);
        org.apache.lucene.index.Fields[] unlikeFields = getFieldsFor(unlikeItemsResponse);
        if (unlikeFields.length > 0) {
            mltQuery.setUnlikeText(unlikeFields);
        }
    }
    BooleanQuery.Builder boolQuery = new BooleanQuery.Builder();
    boolQuery.add(mltQuery, BooleanClause.Occur.SHOULD);
    // exclude the items from the search
    if (!include) {
        handleExclude(boolQuery, likeItems);
    }
    return boolQuery.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Fields(org.apache.lucene.index.Fields) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse)

Example 2 with MultiTermVectorsResponse

use of org.elasticsearch.action.termvectors.MultiTermVectorsResponse in project elasticsearch by elastic.

the class RestMultiTermVectorsAction method prepareRequest.

@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));
}
Also used : MultiTermVectorsRequest(org.elasticsearch.action.termvectors.MultiTermVectorsRequest) TermVectorsRequest(org.elasticsearch.action.termvectors.TermVectorsRequest) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) MultiTermVectorsRequest(org.elasticsearch.action.termvectors.MultiTermVectorsRequest) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestToXContentListener(org.elasticsearch.rest.action.RestToXContentListener) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse) Strings(org.elasticsearch.common.Strings) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) TermVectorsRequest(org.elasticsearch.action.termvectors.TermVectorsRequest) MultiTermVectorsRequest(org.elasticsearch.action.termvectors.MultiTermVectorsRequest) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse)

Example 3 with MultiTermVectorsResponse

use of org.elasticsearch.action.termvectors.MultiTermVectorsResponse 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);
}
Also used : MultiTermVectorsItemResponse(org.elasticsearch.action.termvectors.MultiTermVectorsItemResponse) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse) TermVectorsResponse(org.elasticsearch.action.termvectors.TermVectorsResponse) Fields(org.apache.lucene.index.Fields) ArrayList(java.util.ArrayList)

Example 4 with MultiTermVectorsResponse

use of org.elasticsearch.action.termvectors.MultiTermVectorsResponse 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);
    }
}
Also used : TermVectorsRequest(org.elasticsearch.action.termvectors.TermVectorsRequest) MultiTermVectorsRequest(org.elasticsearch.action.termvectors.MultiTermVectorsRequest) MultiTermVectorsItemResponse(org.elasticsearch.action.termvectors.MultiTermVectorsItemResponse) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse) TermVectorsResponse(org.elasticsearch.action.termvectors.TermVectorsResponse) Fields(org.apache.lucene.index.Fields) MultiFields(org.apache.lucene.index.MultiFields) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse)

Example 5 with MultiTermVectorsResponse

use of org.elasticsearch.action.termvectors.MultiTermVectorsResponse 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]"));
}
Also used : TermVectorsRequest(org.elasticsearch.action.termvectors.TermVectorsRequest) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse) TermVectorsResponse(org.elasticsearch.action.termvectors.TermVectorsResponse) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) Alias(org.elasticsearch.action.admin.indices.alias.Alias) ExplainResponse(org.elasticsearch.action.explain.ExplainResponse) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest)

Aggregations

MultiTermVectorsResponse (org.elasticsearch.action.termvectors.MultiTermVectorsResponse)5 Fields (org.apache.lucene.index.Fields)3 TermVectorsRequest (org.elasticsearch.action.termvectors.TermVectorsRequest)3 TermVectorsResponse (org.elasticsearch.action.termvectors.TermVectorsResponse)3 IOException (java.io.IOException)2 MultiTermVectorsItemResponse (org.elasticsearch.action.termvectors.MultiTermVectorsItemResponse)2 MultiTermVectorsRequest (org.elasticsearch.action.termvectors.MultiTermVectorsRequest)2 ArrayList (java.util.ArrayList)1 MultiFields (org.apache.lucene.index.MultiFields)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 RoutingMissingException (org.elasticsearch.action.RoutingMissingException)1 Alias (org.elasticsearch.action.admin.indices.alias.Alias)1 ExplainResponse (org.elasticsearch.action.explain.ExplainResponse)1 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)1 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)1 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)1 NodeClient (org.elasticsearch.client.node.NodeClient)1 Strings (org.elasticsearch.common.Strings)1 Settings (org.elasticsearch.common.settings.Settings)1