<commit_before>package at.medunigraz.imi.bst.trec.search;

import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.json.JSONObject;

import at.medunigraz.imi.bst.trec.model.Result;
import at.medunigraz.imi.bst.trec.model.ResultList;
import at.medunigraz.imi.bst.trec.model.Topic;
import at.medunigraz.imi.bst.trec.utils.JsonUtils;

public class ElasticSearch implements SearchEngine {

	private static final Logger LOG = LogManager.getLogger();

	private Client client = ElasticClientFactory.getClient();
	
	public ResultList query(Topic topic, JSONObject jsonQuery) {
		QueryBuilder qb = QueryBuilders.wrapperQuery(jsonQuery.toString());
		
		return query(topic, qb);
	}

	@Override
	public ResultList query(Topic topic) {
		// SearchResponse response = client.prepareSearch("index1", "index2")
		// .setTypes("type1", "type2")
		// .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
		// .setQuery(QueryBuilders.termQuery("multi", "test")) // Query
		// .setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) //
		// Filter
		// .setFrom(0).setSize(60).setExplain(true)
		// .get();
		
		LOG.debug("Querying topic " + topic.getNumber() + "...");

		QueryBuilder qb = multiMatchQuery(topic.getDisease() + " " + topic.getVariant(), "title^2", "abstract", "keywords", "meshTags");

		return query(topic, qb);
	}
	
	private ResultList query(Topic topic, QueryBuilder qb) {
		SearchRequestBuilder searchRequestBuilder = client.prepareSearch().setQuery(qb).setSize(1000);
		LOG.trace(searchRequestBuilder.toString());
		
		SearchResponse response = searchRequestBuilder.get();
		LOG.trace(JsonUtils.prettify(response.toString()));

		SearchHit[] results = response.getHits().getHits();

		ResultList ret = new ResultList(topic);
		for (SearchHit hit : results) {
			Result result = new Result(hit.getId(), hit.getScore());
			ret.add(result);
		}

		return ret;
	}

}
<commit_msg>Adds methods for querying without a topic<commit_after>package at.medunigraz.imi.bst.trec.search;

import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.json.JSONObject;

import at.medunigraz.imi.bst.trec.model.Result;
import at.medunigraz.imi.bst.trec.model.ResultList;
import at.medunigraz.imi.bst.trec.model.Topic;
import at.medunigraz.imi.bst.trec.utils.JsonUtils;

public class ElasticSearch implements SearchEngine {

	private static final Logger LOG = LogManager.getLogger();

	private Client client = ElasticClientFactory.getClient();
	
	public ResultList query(JSONObject jsonQuery) {
		QueryBuilder qb = QueryBuilders.wrapperQuery(jsonQuery.toString());
		
		return query(new Topic(), qb);
	}
	
	@Deprecated
	public ResultList query(Topic topic, JSONObject jsonQuery) {
		QueryBuilder qb = QueryBuilders.wrapperQuery(jsonQuery.toString());
		
		return query(topic, qb);
	}

	@Override
	public ResultList query(Topic topic) {
		// SearchResponse response = client.prepareSearch("index1", "index2")
		// .setTypes("type1", "type2")
		// .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
		// .setQuery(QueryBuilders.termQuery("multi", "test")) // Query
		// .setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) //
		// Filter
		// .setFrom(0).setSize(60).setExplain(true)
		// .get();
		
		LOG.debug("Querying topic " + topic.getNumber() + "...");

		QueryBuilder qb = multiMatchQuery(topic.getDisease() + " " + topic.getVariant(), "title^2", "abstract", "keywords", "meshTags");

		return query(topic, qb);
	}
	
	public ResultList query(QueryBuilder qb) {
		return query(new Topic(), qb);
	}
	
	private ResultList query(Topic topic, QueryBuilder qb) {
		SearchRequestBuilder searchRequestBuilder = client.prepareSearch().setQuery(qb).setSize(1000);
		LOG.trace(searchRequestBuilder.toString());
		
		SearchResponse response = searchRequestBuilder.get();
		LOG.trace(JsonUtils.prettify(response.toString()));

		SearchHit[] results = response.getHits().getHits();

		ResultList ret = new ResultList(topic);
		for (SearchHit hit : results) {
			Result result = new Result(hit.getId(), hit.getScore());
			ret.add(result);
		}

		return ret;
	}

}

{'fraction_non_alphanumeric': 0.13583282114320835, 'fraction_numerical': 0.007375537799631223, 'mean_word_length': 5.117794486215539, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5831827', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1529, 'n_words': 274}