<commit_before>/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portal.search.elastic;

import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.kernel.search.DocumentImpl;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.Hits;
import com.liferay.portal.kernel.search.HitsImpl;
import com.liferay.portal.kernel.search.IndexSearcher;
import com.liferay.portal.kernel.search.Query;
import com.liferay.portal.kernel.search.QueryConfig;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.SearchException;
import com.liferay.portal.kernel.search.Sort;
import com.liferay.portal.kernel.search.facet.Facet;
import com.liferay.portal.kernel.search.facet.collector.FacetCollector;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.search.elastic.facet.ElasticsearchFacetFieldCollector;

import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.facet.Facets;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class ElasticsearchIndexSearcher implements IndexSearcher {

    @Override
    public Hits search(SearchContext searchContext, Query query) {
        Client client = getClient();

        SearchRequestBuilder searchRequestBuilder = client.prepareSearch(
                String.valueOf(searchContext.getCompanyId()));

        QueryBuilder queryBuilder = QueryBuilders.queryString(query.toString());

        searchRequestBuilder.setQuery(queryBuilder);

        searchRequestBuilder.setTypes("LiferayDocuments");

        SearchRequest searchRequest = searchRequestBuilder.request();

        ActionFuture<SearchResponse> future = client.search(searchRequest);

        SearchResponse searchResponse = future.actionGet();

        updateFacetCollectors(searchContext, searchResponse);

        Hits hits = processSearchHits(
                searchResponse.getHits(), query.getQueryConfig());

        hits.setQuery(query);

        TimeValue timeValue = searchResponse.getTook();

        hits.setSearchTime((float) timeValue.getSecondsFrac());
        return hits;
    }

    @Override
    public Hits search(String searchEngineId, long companyId, Query query, Sort[] sort, int start, int end) throws SearchException {
        Client client = getClient();

        SearchRequestBuilder searchRequestBuilder = client.prepareSearch(
                String.valueOf(companyId));

        QueryBuilder queryBuilder = QueryBuilders.queryString(query.toString());

        searchRequestBuilder.setQuery(queryBuilder);

        searchRequestBuilder.setTypes("LiferayDocuments");

        SearchRequest searchRequest = searchRequestBuilder.request();

        ActionFuture<SearchResponse> future = client.search(searchRequest);

        SearchResponse searchResponse = future.actionGet();


        Hits hits = processSearchHits(
                searchResponse.getHits(), query.getQueryConfig());

        hits.setQuery(query);

        TimeValue timeValue = searchResponse.getTook();

        hits.setSearchTime((float) timeValue.getSecondsFrac());
        return hits;
    }

    @Override
    public String spellCheckKeywords(SearchContext searchContext) {
        return StringPool.BLANK;
    }

    @Override
    public Map<String, List<String>> spellCheckKeywords(
            SearchContext searchContext, int max) {

        return Collections.emptyMap();
    }

    @Override
    public String[] suggestKeywordQueries(
            SearchContext searchContext, int max) {

        return new String[0];
    }



    protected Document processSearchHit(SearchHit hit) {
        Document document = new DocumentImpl();

        Map<String, SearchHitField> searchHitFields = hit.getFields();

        for (Map.Entry<String, SearchHitField> entry :
                searchHitFields.entrySet()) {

            SearchHitField searchHitField = entry.getValue();

            Collection<Object> fieldValues = searchHitField.getValues();

            Field field = new Field(
                    entry.getKey(),
                    ArrayUtil.toStringArray(
                            fieldValues.toArray(new Object[fieldValues.size()]))
            );

            document.add(field);
        }

        return document;<commit_msg>Change document creation in search<commit_after>/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portal.search.elastic;

import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.kernel.search.DocumentImpl;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.Hits;
import com.liferay.portal.kernel.search.HitsImpl;
import com.liferay.portal.kernel.search.IndexSearcher;
import com.liferay.portal.kernel.search.Query;
import com.liferay.portal.kernel.search.QueryConfig;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.SearchException;
import com.liferay.portal.kernel.search.Sort;
import com.liferay.portal.kernel.search.facet.Facet;
import com.liferay.portal.kernel.search.facet.collector.FacetCollector;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.search.elastic.facet.ElasticsearchFacetFieldCollector;

import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.facet.Facets;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class ElasticsearchIndexSearcher implements IndexSearcher {

    @Override
    public Hits search(SearchContext searchContext, Query query) {
        Client client = getClient();

        SearchRequestBuilder searchRequestBuilder = client.prepareSearch(
                String.valueOf(searchContext.getCompanyId()));

        QueryBuilder queryBuilder = QueryBuilders.queryString(query.toString());

        searchRequestBuilder.setQuery(queryBuilder);

        searchRequestBuilder.setTypes("LiferayDocuments");

        SearchRequest searchRequest = searchRequestBuilder.request();

        ActionFuture<SearchResponse> future = client.search(searchRequest);

        SearchResponse searchResponse = future.actionGet();

        updateFacetCollectors(searchContext, searchResponse);

        Hits hits = processSearchHits(
                searchResponse.getHits(), query.getQueryConfig());

        hits.setQuery(query);

        TimeValue timeValue = searchResponse.getTook();

        hits.setSearchTime((float) timeValue.getSecondsFrac());
        return hits;
    }

    @Override
    public Hits search(String searchEngineId, long companyId, Query query, Sort[] sort, int start, int end) throws SearchException {
        Client client = getClient();

        SearchRequestBuilder searchRequestBuilder = client.prepareSearch(
                String.valueOf(companyId));

        QueryBuilder queryBuilder = QueryBuilders.queryString(query.toString());

        searchRequestBuilder.setQuery(queryBuilder);

        searchRequestBuilder.setTypes("LiferayDocuments");

        SearchRequest searchRequest = searchRequestBuilder.request();

        ActionFuture<SearchResponse> future = client.search(searchRequest);

        SearchResponse searchResponse = future.actionGet();


        Hits hits = processSearchHits(
                searchResponse.getHits(), query.getQueryConfig());

        hits.setQuery(query);

        TimeValue timeValue = searchResponse.getTook();

        hits.setSearchTime((float) timeValue.getSecondsFrac());
        return hits;
    }

    @Override
    public String spellCheckKeywords(SearchContext searchContext) {
        return StringPool.BLANK;
    }

    @Override
    public Map<String, List<String>> spellCheckKeywords(
            SearchContext searchContext, int max) {

        return Collections.emptyMap();
    }

    @Override
    public String[] suggestKeywordQueries(
            SearchContext searchContext, int max) {

        return new String[0];
    }



    protected Document processSearchHit(SearchHit hit) {
        Document document = new DocumentImpl();

       Map<String, Object> source = hit.getSource();
  
       for (String fieldName :
        	source.keySet()) {

        	String val =(String)source.get(fieldName);
        	Field field = new Field(
        			fieldName,
                    new String[]{val}
            );

            document.add(field);
            
        }

        return document;
{'fraction_non_alphanumeric': 0.09086918349429324, 'fraction_numerical': 0.0012291483757682178, 'mean_word_length': 3.7011968633924885, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '539318', 'n_tokens_mistral': 2946, 'n_tokens_neox': 2989, 'n_words': 670}