<commit_before>package banter

import com.google.inject.Inject
import groovy.util.logging.Slf4j
import org.elasticsearch.client.Client
import org.elasticsearch.search.SearchHits
import org.elasticsearch.search.sort.SortBuilders

import static banter.SearchConstants.INDEX
import static banter.SearchConstants.TYPE
import static org.elasticsearch.search.sort.SortOrder.ASC

@Slf4j
class Searcher {

    @Inject
    Client client

    SearchHits search(String channel, String query) {
        // TODO: better querying
        log.info("Running search")
        def searchResponse = client.prepareSearch(INDEX)
                .setTypes(TYPE)
//                .setPostFilter(FilterBuilders.termFilter("channel", message.parameters[0]))
                .addSort(SortBuilders.fieldSort("timestamp").order(ASC))
                .setFrom(0).setSize(100).setExplain(true)
                .addFields("timestamp", "channel", "nickname", "username", "realname", "text")
                .execute().actionGet()
        log.info("Search result: {} total hits", searchResponse.hits.totalHits)
        for (hit in searchResponse.hits.hits) {
            log.info("Hit: {}, {}", hit.sourceAsMap(), hit.fields.collect {"${it.key}:${it.value.value}}"})
        }
        return searchResponse.hits
    }

}
<commit_msg>searcher: make channel and query parameters work<commit_after>package banter

import com.google.inject.Inject
import groovy.util.logging.Slf4j
import org.apache.lucene.queryparser.xml.FilterBuilder
import org.elasticsearch.client.Client
import org.elasticsearch.index.query.FilterBuilders
import org.elasticsearch.index.query.QueryBuilders
import org.elasticsearch.search.SearchHits
import org.elasticsearch.search.sort.SortBuilders

import static banter.SearchConstants.INDEX
import static banter.SearchConstants.TYPE
import static org.elasticsearch.search.sort.SortOrder.ASC

@Slf4j
class Searcher {

    @Inject
    Client client

    SearchHits search(String channel, String queryTerm) {
        // TODO: better querying
        log.info("Running search")
        def query = queryTerm ? QueryBuilders.termQuery("text", queryTerm) : QueryBuilders.matchAllQuery()
        if (channel) {
            query = QueryBuilders.filteredQuery(query, FilterBuilders.inFilter("channel", channel))
        }
        def searchRequest = client.prepareSearch(INDEX)
                        .setTypes(TYPE)
                        .addSort(SortBuilders.fieldSort("timestamp").order(ASC))
                        .setFrom(0).setSize(100).setExplain(true)
                        .addFields("timestamp", "channel", "nickname", "username", "realname", "text")
                        .setQuery(query)
        def searchResponse = searchRequest.get()
        log.info("Search result: {} total hits", searchResponse.hits.totalHits)
        for (hit in searchResponse.hits.hits) {
            log.info("Hit: {}, {}", hit.sourceAsMap(), hit.fields.collect {"${it.key}:${it.value.value}}"})
        }
        return searchResponse.hits
    }

}

{'fraction_non_alphanumeric': 0.11694747274529237, 'fraction_numerical': 0.004294681202510736, 'mean_word_length': 3.010596026490066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, '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': '7574480', 'n_tokens_mistral': 850, 'n_tokens_neox': 841, 'n_words': 159}