Search in sources :

Example 6 with SearchHitField

use of org.elasticsearch.search.SearchHitField in project elasticsearch by elastic.

the class ParentFieldSubFetchPhase method hitExecute.

@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    if (context.storedFieldsContext() != null && context.storedFieldsContext().fetchFields() == false) {
        return;
    }
    ParentFieldMapper parentFieldMapper = context.mapperService().documentMapper(hitContext.hit().getType()).parentFieldMapper();
    if (parentFieldMapper.active() == false) {
        return;
    }
    String parentId = getParentId(parentFieldMapper, hitContext.reader(), hitContext.docId());
    if (parentId == null) {
        // hit has no _parent field. Can happen for nested inner hits if parent hit is a p/c document.
        return;
    }
    Map<String, SearchHitField> fields = hitContext.hit().fieldsOrNull();
    if (fields == null) {
        fields = new HashMap<>();
        hitContext.hit().fields(fields);
    }
    fields.put(ParentFieldMapper.NAME, new SearchHitField(ParentFieldMapper.NAME, Collections.singletonList(parentId)));
}
Also used : ParentFieldMapper(org.elasticsearch.index.mapper.ParentFieldMapper) SearchHitField(org.elasticsearch.search.SearchHitField)

Example 7 with SearchHitField

use of org.elasticsearch.search.SearchHitField in project elasticsearch by elastic.

the class ScriptFieldsFetchSubPhase method hitExecute.

@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    if (context.hasScriptFields() == false) {
        return;
    }
    for (ScriptFieldsContext.ScriptField scriptField : context.scriptFields().fields()) {
        /* Because this is called once per document we end up creating new ScriptDocValues for every document which is important because
             * the values inside ScriptDocValues might be reused for different documents (Dates do this). */
        LeafSearchScript leafScript;
        try {
            leafScript = scriptField.script().getLeafSearchScript(hitContext.readerContext());
        } catch (IOException e1) {
            throw new IllegalStateException("Failed to load script", e1);
        }
        leafScript.setDocument(hitContext.docId());
        final Object value;
        try {
            value = leafScript.unwrap(leafScript.run());
        } catch (RuntimeException e) {
            if (scriptField.ignoreException()) {
                continue;
            }
            throw e;
        }
        if (hitContext.hit().fieldsOrNull() == null) {
            hitContext.hit().fields(new HashMap<>(2));
        }
        SearchHitField hitField = hitContext.hit().getFields().get(scriptField.name());
        if (hitField == null) {
            final List<Object> values;
            if (value instanceof Collection) {
                // TODO: use diamond operator once JI-9019884 is fixed
                values = new ArrayList<>((Collection<?>) value);
            } else {
                values = Collections.singletonList(value);
            }
            hitField = new SearchHitField(scriptField.name(), values);
            hitContext.hit().getFields().put(scriptField.name(), hitField);
        }
    }
}
Also used : LeafSearchScript(org.elasticsearch.script.LeafSearchScript) Collection(java.util.Collection) SearchHitField(org.elasticsearch.search.SearchHitField) IOException(java.io.IOException)

Example 8 with SearchHitField

use of org.elasticsearch.search.SearchHitField in project elasticsearch by elastic.

the class ExpandSearchPhaseTests method testCollapseSingleHit.

public void testCollapseSingleHit() throws IOException {
    final int iters = randomIntBetween(5, 10);
    for (int i = 0; i < iters; i++) {
        SearchHits collapsedHits = new SearchHits(new SearchHit[] { new SearchHit(2, "ID", new Text("type"), Collections.emptyMap()), new SearchHit(3, "ID", new Text("type"), Collections.emptyMap()) }, 1, 1.0F);
        AtomicBoolean executedMultiSearch = new AtomicBoolean(false);
        QueryBuilder originalQuery = randomBoolean() ? null : QueryBuilders.termQuery("foo", "bar");
        MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
        String collapseValue = randomBoolean() ? null : "boom";
        mockSearchPhaseContext.getRequest().source(new SearchSourceBuilder().collapse(new CollapseBuilder("someField").setInnerHits(new InnerHitBuilder().setName("foobarbaz"))));
        mockSearchPhaseContext.getRequest().source().query(originalQuery);
        mockSearchPhaseContext.searchTransport = new SearchTransportService(Settings.builder().put("search.remote.connect", false).build(), null, null) {

            @Override
            void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
                assertTrue(executedMultiSearch.compareAndSet(false, true));
                assertEquals(1, request.requests().size());
                SearchRequest searchRequest = request.requests().get(0);
                assertTrue(searchRequest.source().query() instanceof BoolQueryBuilder);
                BoolQueryBuilder groupBuilder = (BoolQueryBuilder) searchRequest.source().query();
                if (collapseValue == null) {
                    assertThat(groupBuilder.mustNot(), Matchers.contains(QueryBuilders.existsQuery("someField")));
                } else {
                    assertThat(groupBuilder.filter(), Matchers.contains(QueryBuilders.matchQuery("someField", "boom")));
                }
                if (originalQuery != null) {
                    assertThat(groupBuilder.must(), Matchers.contains(QueryBuilders.termQuery("foo", "bar")));
                }
                assertArrayEquals(mockSearchPhaseContext.getRequest().indices(), searchRequest.indices());
                assertArrayEquals(mockSearchPhaseContext.getRequest().types(), searchRequest.types());
                InternalSearchResponse internalSearchResponse = new InternalSearchResponse(collapsedHits, null, null, null, false, null, 1);
                SearchResponse response = mockSearchPhaseContext.buildSearchResponse(internalSearchResponse, null);
                listener.onResponse(new MultiSearchResponse(new MultiSearchResponse.Item[] { new MultiSearchResponse.Item(response, null) }));
            }
        };
        SearchHits hits = new SearchHits(new SearchHit[] { new SearchHit(1, "ID", new Text("type"), Collections.singletonMap("someField", new SearchHitField("someField", Collections.singletonList(collapseValue)))) }, 1, 1.0F);
        InternalSearchResponse internalSearchResponse = new InternalSearchResponse(hits, null, null, null, false, null, 1);
        SearchResponse response = mockSearchPhaseContext.buildSearchResponse(internalSearchResponse, null);
        AtomicReference<SearchResponse> reference = new AtomicReference<>();
        ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, response, r -> new SearchPhase("test") {

            @Override
            public void run() throws IOException {
                reference.set(r);
            }
        });
        phase.run();
        mockSearchPhaseContext.assertNoFailure();
        assertNotNull(reference.get());
        SearchResponse theResponse = reference.get();
        assertSame(theResponse, response);
        assertEquals(1, theResponse.getHits().getHits()[0].getInnerHits().size());
        assertSame(theResponse.getHits().getHits()[0].getInnerHits().get("foobarbaz"), collapsedHits);
        assertTrue(executedMultiSearch.get());
        assertEquals(1, mockSearchPhaseContext.phasesExecuted.get());
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) CollapseBuilder(org.elasticsearch.search.collapse.CollapseBuilder) Text(org.elasticsearch.common.text.Text) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SearchHitField(org.elasticsearch.search.SearchHitField) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse)

Example 9 with SearchHitField

use of org.elasticsearch.search.SearchHitField in project sonarqube by SonarSource.

the class BulkIndexer method addDeletion.

public void addDeletion(SearchRequestBuilder searchRequest) {
    searchRequest.addSort("_doc", SortOrder.ASC).setScroll(TimeValue.timeValueMinutes(5)).setSize(100).setFetchSource(false);
    // this search is synchronous. An optimization would be to be non-blocking,
    // but it requires to tracking pending requests in close().
    // Same semaphore can't be reused because of potential deadlock (requires to acquire
    // two locks)
    SearchResponse searchResponse = searchRequest.get();
    while (true) {
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            SearchHitField routing = hit.field("_routing");
            DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete(hit.index(), hit.type(), hit.getId());
            if (routing != null) {
                deleteRequestBuilder.setRouting(routing.getValue());
            }
            add(deleteRequestBuilder.request());
        }
        String scrollId = searchResponse.getScrollId();
        searchResponse = client.prepareSearchScroll(scrollId).setScroll(TimeValue.timeValueMinutes(5)).get();
        if (hits.length == 0) {
            client.nativeClient().prepareClearScroll().addScrollId(scrollId).get();
            break;
        }
    }
}
Also used : DeleteRequestBuilder(org.elasticsearch.action.delete.DeleteRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) SearchHitField(org.elasticsearch.search.SearchHitField) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 10 with SearchHitField

use of org.elasticsearch.search.SearchHitField in project fess by codelibs.

the class FessEsClient method getDocumentList.

public List<Map<String, Object>> getDocumentList(final String index, final String type, final SearchCondition<SearchRequestBuilder> condition) {
    return getDocumentList(index, type, condition, (response, hit) -> {
        final FessConfig fessConfig = ComponentUtil.getFessConfig();
        final Map<String, Object> source = hit.getSource();
        if (source != null) {
            final Map<String, Object> docMap = new HashMap<>(source);
            docMap.put(fessConfig.getIndexFieldId(), hit.getId());
            return docMap;
        }
        final Map<String, SearchHitField> fields = hit.getFields();
        if (fields != null) {
            final Map<String, Object> docMap = fields.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> (Object) e.getValue().getValues()));
            docMap.put(fessConfig.getIndexFieldId(), hit.getId());
            return docMap;
        }
        return null;
    });
}
Also used : Arrays(java.util.Arrays) ExplainRequest(org.elasticsearch.action.explain.ExplainRequest) SearchHits(org.elasticsearch.search.SearchHits) Curl(org.codelibs.elasticsearch.runner.net.Curl) InetAddress(java.net.InetAddress) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) QueryContext(org.codelibs.fess.entity.QueryContext) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequestBuilder(org.elasticsearch.action.delete.DeleteRequestBuilder) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) Map(java.util.Map) TermVectorsRequest(org.elasticsearch.action.termvectors.TermVectorsRequest) IllegalBehaviorStateException(org.dbflute.exception.IllegalBehaviorStateException) RefreshPolicy(org.elasticsearch.action.support.WriteRequest.RefreshPolicy) ActionRequest(org.elasticsearch.action.ActionRequest) MultiTermVectorsRequest(org.elasticsearch.action.termvectors.MultiTermVectorsRequest) ResourceUtil(org.codelibs.core.io.ResourceUtil) ExplainResponse(org.elasticsearch.action.explain.ExplainResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) MultiSearchRequestBuilder(org.elasticsearch.action.search.MultiSearchRequestBuilder) StandardCharsets(java.nio.charset.StandardCharsets) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) DocMap(org.codelibs.fess.util.DocMap) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) FileUtil(org.codelibs.core.io.FileUtil) ComponentUtil(org.codelibs.fess.util.ComponentUtil) TermVectorsResponse(org.elasticsearch.action.termvectors.TermVectorsResponse) DocumentHelper(org.codelibs.fess.helper.DocumentHelper) TermVectorsRequestBuilder(org.elasticsearch.action.termvectors.TermVectorsRequestBuilder) ClearScrollRequestBuilder(org.elasticsearch.action.search.ClearScrollRequestBuilder) AdminClient(org.elasticsearch.client.AdminClient) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) ExplainRequestBuilder(org.elasticsearch.action.explain.ExplainRequestBuilder) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) FessSystemException(org.codelibs.fess.exception.FessSystemException) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) IndicesAliasesResponse(org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) TransportClient(org.elasticsearch.client.transport.TransportClient) SimpleDateFormat(java.text.SimpleDateFormat) ResultOffsetExceededException(org.codelibs.fess.exception.ResultOffsetExceededException) SearchRequest(org.elasticsearch.action.search.SearchRequest) ResourceNotFoundRuntimeException(org.codelibs.core.exception.ResourceNotFoundRuntimeException) Order(org.elasticsearch.search.aggregations.bucket.terms.Terms.Order) ArrayList(java.util.ArrayList) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) OpType(org.elasticsearch.action.DocWriteRequest.OpType) SearchRequestType(org.codelibs.fess.entity.SearchRequestParams.SearchRequestType) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) FacetInfo(org.codelibs.fess.entity.FacetInfo) ActionRequestBuilder(org.elasticsearch.action.ActionRequestBuilder) FlushResponse(org.elasticsearch.action.admin.indices.flush.FlushResponse) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) CollapseBuilder(org.elasticsearch.search.collapse.CollapseBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BaseEncoding(com.google.common.io.BaseEncoding) ActionResponse(org.elasticsearch.action.ActionResponse) Client(org.elasticsearch.client.Client) StringUtil(org.codelibs.core.lang.StringUtil) MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder) IOException(java.io.IOException) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) UnknownHostException(java.net.UnknownHostException) File(java.io.File) IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) ElasticsearchClusterRunner(org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner) GetRequestBuilder(org.elasticsearch.action.get.GetRequestBuilder) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) ActionListener(org.elasticsearch.action.ActionListener) Builder(org.elasticsearch.common.settings.Settings.Builder) ElasticsearchException(org.elasticsearch.ElasticsearchException) Constants(org.codelibs.fess.Constants) GetResponse(org.elasticsearch.action.get.GetResponse) FieldStatsRequest(org.elasticsearch.action.fieldstats.FieldStatsRequest) Date(java.util.Date) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) LoggerFactory(org.slf4j.LoggerFactory) FieldCapabilitiesResponse(org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) GeoInfo(org.codelibs.fess.entity.GeoInfo) PreDestroy(javax.annotation.PreDestroy) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Settings(org.elasticsearch.common.settings.Settings) PingResponse(org.codelibs.fess.entity.PingResponse) QueryHelper(org.codelibs.fess.helper.QueryHelper) SearchResponse(org.elasticsearch.action.search.SearchResponse) CurlResponse(org.codelibs.elasticsearch.runner.net.CurlResponse) FieldCapabilitiesRequest(org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest) ThreadPool(org.elasticsearch.threadpool.ThreadPool) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) FieldStatsResponse(org.elasticsearch.action.fieldstats.FieldStatsResponse) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Failure(org.elasticsearch.action.bulk.BulkItemResponse.Failure) SearchHit(org.elasticsearch.search.SearchHit) GetRequest(org.elasticsearch.action.get.GetRequest) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) MultiTermVectorsRequestBuilder(org.elasticsearch.action.termvectors.MultiTermVectorsRequestBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) Collectors(java.util.stream.Collectors) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) ElasticsearchClusterRunner.newConfigs(org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner.newConfigs) List(java.util.List) FieldCapabilitiesRequestBuilder(org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) TransportAddress(org.elasticsearch.common.transport.TransportAddress) PostConstruct(javax.annotation.PostConstruct) Pattern(java.util.regex.Pattern) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) BeanUtil(org.codelibs.core.beans.util.BeanUtil) FieldStatsRequestBuilder(org.elasticsearch.action.fieldstats.FieldStatsRequestBuilder) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) HashMap(java.util.HashMap) ActionFuture(org.elasticsearch.action.ActionFuture) ClearScrollResponse(org.elasticsearch.action.search.ClearScrollResponse) SearchHitField(org.elasticsearch.search.SearchHitField) TimeValue(org.elasticsearch.common.unit.TimeValue) GetIndexResponse(org.elasticsearch.action.admin.indices.get.GetIndexResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) Result(org.elasticsearch.action.DocWriteResponse.Result) SearchQueryException(org.codelibs.fess.exception.SearchQueryException) UserMessages(org.lastaflute.core.message.UserMessages) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) Logger(org.slf4j.Logger) OptionalEntity(org.dbflute.optional.OptionalEntity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) Configs(org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner.Configs) Action(org.elasticsearch.action.Action) IndicesAliasesRequestBuilder(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) InvalidQueryException(org.codelibs.fess.exception.InvalidQueryException) Collections(java.util.Collections) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) SearchScrollRequestBuilder(org.elasticsearch.action.search.SearchScrollRequestBuilder) HashMap(java.util.HashMap) SearchHitField(org.elasticsearch.search.SearchHitField) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Aggregations

SearchHitField (org.elasticsearch.search.SearchHitField)23 SearchHit (org.elasticsearch.search.SearchHit)16 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 SearchHits (org.elasticsearch.search.SearchHits)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)8 Map (java.util.Map)7 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)7 IOException (java.io.IOException)6 List (java.util.List)6 Text (org.elasticsearch.common.text.Text)6 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)5 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)4 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)4 Script (org.elasticsearch.script.Script)4 CollapseBuilder (org.elasticsearch.search.collapse.CollapseBuilder)4 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)4 QueryHelper (org.codelibs.fess.helper.QueryHelper)3 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)3 OptionalEntity (org.dbflute.optional.OptionalEntity)3