Search in sources :

Example 11 with SearchHitField

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

the class FessEsClient method getDocument.

public OptionalEntity<Map<String, Object>> getDocument(final String index, final String type, final SearchCondition<SearchRequestBuilder> condition) {
    return getDocument(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());
            docMap.put(fessConfig.getIndexFieldVersion(), hit.getVersion());
            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());
            docMap.put(fessConfig.getIndexFieldVersion(), hit.getVersion());
            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)

Example 12 with SearchHitField

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

the class QueryResponseList method init.

public void init(final OptionalEntity<SearchResponse> searchResponseOpt, final int start, final int pageSize) {
    searchResponseOpt.ifPresent(searchResponse -> {
        final FessConfig fessConfig = ComponentUtil.getFessConfig();
        final SearchHits searchHits = searchResponse.getHits();
        allRecordCount = searchHits.getTotalHits();
        queryTime = searchResponse.getTookInMillis();
        if (searchResponse.getTotalShards() != searchResponse.getSuccessfulShards()) {
            partialResults = true;
        }
        final QueryHelper queryHelper = ComponentUtil.getQueryHelper();
        final String hlPrefix = queryHelper.getHighlightPrefix();
        for (final SearchHit searchHit : searchHits.getHits()) {
            final Map<String, Object> docMap = parseSearchHit(fessConfig, hlPrefix, searchHit);
            if (fessConfig.isResultCollapsed()) {
                final Map<String, SearchHits> innerHits = searchHit.getInnerHits();
                if (innerHits != null) {
                    final SearchHits innerSearchHits = innerHits.get(fessConfig.getQueryCollapseInnerHitsName());
                    if (innerSearchHits != null) {
                        final long totalHits = innerSearchHits.getTotalHits();
                        if (totalHits > 1) {
                            docMap.put(fessConfig.getQueryCollapseInnerHitsName() + "_count", totalHits);
                            final SearchHitField bitsField = searchHit.getFields().get(fessConfig.getIndexFieldContentMinhashBits());
                            if (bitsField != null && !bitsField.getValues().isEmpty()) {
                                docMap.put(fessConfig.getQueryCollapseInnerHitsName() + "_hash", bitsField.getValues().get(0));
                            }
                            docMap.put(fessConfig.getQueryCollapseInnerHitsName(), StreamUtil.stream(innerSearchHits.getHits()).get(stream -> stream.map(v -> parseSearchHit(fessConfig, hlPrefix, v)).toArray(n -> new Map[n])));
                        }
                    }
                }
            }
            parent.add(docMap);
        }
        final Aggregations aggregations = searchResponse.getAggregations();
        if (aggregations != null) {
            facetResponse = new FacetResponse(aggregations);
        }
    });
    calculatePageInfo(start, pageSize);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) SearchHits(org.elasticsearch.search.SearchHits) Aggregations(org.elasticsearch.search.aggregations.Aggregations) OptionalEntity(org.dbflute.optional.OptionalEntity) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) List(java.util.List) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) StreamUtil(org.codelibs.core.stream.StreamUtil) Text(org.elasticsearch.common.text.Text) SearchHitField(org.elasticsearch.search.SearchHitField) Map(java.util.Map) QueryHelper(org.codelibs.fess.helper.QueryHelper) SearchResponse(org.elasticsearch.action.search.SearchResponse) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) ViewHelper(org.codelibs.fess.helper.ViewHelper) SearchHit(org.elasticsearch.search.SearchHit) Aggregations(org.elasticsearch.search.aggregations.Aggregations) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) QueryHelper(org.codelibs.fess.helper.QueryHelper) SearchHitField(org.elasticsearch.search.SearchHitField) SearchHits(org.elasticsearch.search.SearchHits)

Example 13 with SearchHitField

use of org.elasticsearch.search.SearchHitField in project zeppelin by apache.

the class TransportBasedClient method search.

@Override
public ActionResponse search(String[] indices, String[] types, String query, int size) {
    final SearchRequestBuilder reqBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE);
    reqBuilder.setIndices();
    if (indices != null) {
        reqBuilder.setIndices(indices);
    }
    if (types != null) {
        reqBuilder.setTypes(types);
    }
    if (!StringUtils.isEmpty(query)) {
        // So, try to parse as a JSON => if there is an error, consider the query a Lucene one
        try {
            @SuppressWarnings("rawtypes") final Map source = gson.fromJson(query, Map.class);
            reqBuilder.setExtraSource(source);
        } catch (final JsonSyntaxException e) {
            // This is not a JSON (or maybe not well formatted...)
            reqBuilder.setQuery(QueryBuilders.queryStringQuery(query).analyzeWildcard(true));
        }
    }
    reqBuilder.setSize(size);
    final SearchResponse searchResp = reqBuilder.get();
    final ActionResponse actionResp = new ActionResponse().succeeded(true).totalHits(searchResp.getHits().getTotalHits());
    if (searchResp.getAggregations() != null) {
        setAggregations(searchResp.getAggregations(), actionResp);
    } else {
        for (final SearchHit hit : searchResp.getHits()) {
            // Fields can be found either in _source, or in fields (it depends on the query)
            // => specific for elasticsearch's version < 5
            //
            String src = hit.getSourceAsString();
            if (src == null) {
                final Map<String, Object> hitFields = new HashMap<>();
                for (final SearchHitField hitField : hit.getFields().values()) {
                    hitFields.put(hitField.getName(), hitField.getValues());
                }
                src = gson.toJson(hitFields);
            }
            actionResp.addHit(new HitWrapper(hit.getIndex(), hit.getType(), hit.getId(), src));
        }
    }
    return actionResp;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) JsonSyntaxException(com.google.gson.JsonSyntaxException) HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) SearchHitField(org.elasticsearch.search.SearchHitField) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with SearchHitField

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

the class DocValueFieldsFetchSubPhase method hitExecute.

@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    if (context.collapse() != null) {
        // retrieve the `doc_value` associated with the collapse field
        String name = context.collapse().getFieldType().name();
        if (context.docValueFieldsContext() == null) {
            context.docValueFieldsContext(new DocValueFieldsContext(Collections.singletonList(name)));
        } else if (context.docValueFieldsContext().fields().contains(name) == false) {
            context.docValueFieldsContext().fields().add(name);
        }
    }
    if (context.docValueFieldsContext() == null) {
        return;
    }
    for (String field : context.docValueFieldsContext().fields()) {
        if (hitContext.hit().fieldsOrNull() == null) {
            hitContext.hit().fields(new HashMap<>(2));
        }
        SearchHitField hitField = hitContext.hit().getFields().get(field);
        if (hitField == null) {
            hitField = new SearchHitField(field, new ArrayList<>(2));
            hitContext.hit().getFields().put(field, hitField);
        }
        MappedFieldType fieldType = context.mapperService().fullName(field);
        if (fieldType != null) {
            /* Because this is called once per document we end up creating a new ScriptDocValues for every document which is important
                 * because the values inside ScriptDocValues might be reused for different documents (Dates do this). */
            AtomicFieldData data = context.fieldData().getForField(fieldType).load(hitContext.readerContext());
            ScriptDocValues<?> values = data.getScriptValues();
            values.setNextDocId(hitContext.docId());
            hitField.getValues().addAll(values);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) AtomicFieldData(org.elasticsearch.index.fielddata.AtomicFieldData) SearchHitField(org.elasticsearch.search.SearchHitField)

Example 15 with SearchHitField

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

the class FetchPhase method getSearchFields.

private Map<String, SearchHitField> getSearchFields(SearchContext context, int nestedSubDocId, Set<String> fieldNames, List<String> fieldNamePatterns, LeafReaderContext subReaderContext) {
    Map<String, SearchHitField> searchFields = null;
    if (context.hasStoredFields() && !context.storedFieldsContext().fieldNames().isEmpty()) {
        FieldsVisitor nestedFieldsVisitor = new CustomFieldsVisitor(fieldNames == null ? Collections.emptySet() : fieldNames, fieldNamePatterns == null ? Collections.emptyList() : fieldNamePatterns, false);
        if (nestedFieldsVisitor != null) {
            loadStoredFields(context, subReaderContext, nestedFieldsVisitor, nestedSubDocId);
            nestedFieldsVisitor.postProcess(context.mapperService());
            if (!nestedFieldsVisitor.fields().isEmpty()) {
                searchFields = new HashMap<>(nestedFieldsVisitor.fields().size());
                for (Map.Entry<String, List<Object>> entry : nestedFieldsVisitor.fields().entrySet()) {
                    searchFields.put(entry.getKey(), new SearchHitField(entry.getKey(), entry.getValue()));
                }
            }
        }
    }
    return searchFields;
}
Also used : FieldsVisitor(org.elasticsearch.index.fieldvisitor.FieldsVisitor) CustomFieldsVisitor(org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor) CustomFieldsVisitor(org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor) SearchHitField(org.elasticsearch.search.SearchHitField) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

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