<commit_before>package io.jenkins.plugins.datastore.impl;

import io.jenkins.plugins.commons.JsonObjectMapper;
import io.jenkins.plugins.datastore.DatastoreException;
import io.jenkins.plugins.datastore.DatastoreService;
import io.jenkins.plugins.datastore.SearchOptions;
import io.jenkins.plugins.datastore.SortBy;
import io.jenkins.plugins.datastore.support.ElasticsearchTransformer;
import io.jenkins.plugins.models.*;
import org.apache.commons.io.FileUtils;
import org.apache.lucene.queryparser.xml.builders.FilteredQueryBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.nested.InternalNested;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ElasticsearchDatastoreService implements DatastoreService {

  private Logger logger = LoggerFactory.getLogger(ElasticsearchDatastoreService.class);

  @Inject
  private Client esClient;

  @Override
  public Plugins search(SearchOptions searchOptions) throws DatastoreException {
    try {
      final SearchRequestBuilder requestBuilder = esClient.prepareSearch("plugins")
        .setFrom((searchOptions.getPage() - 1) * searchOptions.getLimit())
        .setSize(searchOptions.getLimit());
      final BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
      if (searchOptions.getQuery() != null) {
        queryBuilder
          .should(QueryBuilders.matchQuery("title", searchOptions.getQuery()))
          .should(QueryBuilders.matchQuery("name", searchOptions.getQuery()))
          .must(QueryBuilders.matchQuery("excerpt", searchOptions.getQuery()));
      } else {
        queryBuilder.must(QueryBuilders.matchAllQuery());
      }
      if (!searchOptions.getAuthors().isEmpty() || !searchOptions.getCategories().isEmpty()
            || searchOptions.getCore() != null || !searchOptions.getLabels().isEmpty()) {
        final BoolQueryBuilder filter = QueryBuilders.boolQuery();
        if (!searchOptions.getCategories().isEmpty()) {
          filter.must(
            QueryBuilders.boolQuery().should(
              QueryBuilders.termsQuery("categories", searchOptions.getCategories())
            )
          );
        }
        if (!searchOptions.getLabels().isEmpty()) {
          filter.must(
            QueryBuilders.boolQuery().should(
              QueryBuilders.termsQuery("labels", searchOptions.getLabels())
            )
          );
        }
        if (!searchOptions.getAuthors().isEmpty()) {
          filter.must(
            QueryBuilders.boolQuery().should(
              QueryBuilders.nestedQuery("developers", QueryBuilders.matchQuery("developers.name", searchOptions.getAuthors()))
            )
          );
        }
        if (searchOptions.getCore() != null) {
          filter.must(QueryBuilders.termQuery("requiredCore", searchOptions.getCore()));
        }
        queryBuilder.filter(filter);
      }
      requestBuilder.setQuery(queryBuilder);<commit_msg>Fix labels bug<commit_after>package io.jenkins.plugins.datastore.impl;

import io.jenkins.plugins.commons.JsonObjectMapper;
import io.jenkins.plugins.datastore.DatastoreException;
import io.jenkins.plugins.datastore.DatastoreService;
import io.jenkins.plugins.datastore.SearchOptions;
import io.jenkins.plugins.datastore.SortBy;
import io.jenkins.plugins.datastore.support.ElasticsearchTransformer;
import io.jenkins.plugins.models.*;
import org.apache.commons.io.FileUtils;
import org.apache.lucene.queryparser.xml.builders.FilteredQueryBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.nested.InternalNested;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ElasticsearchDatastoreService implements DatastoreService {

  private Logger logger = LoggerFactory.getLogger(ElasticsearchDatastoreService.class);

  @Inject
  private Client esClient;

  @Override
  public Plugins search(SearchOptions searchOptions) throws DatastoreException {
    try {
      final SearchRequestBuilder requestBuilder = esClient.prepareSearch("plugins")
        .setFrom((searchOptions.getPage() - 1) * searchOptions.getLimit())
        .setSize(searchOptions.getLimit());
      final BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
      if (searchOptions.getQuery() != null) {
        queryBuilder
          .should(QueryBuilders.matchQuery("title", searchOptions.getQuery()))
          .should(QueryBuilders.matchQuery("name", searchOptions.getQuery()))
          .must(QueryBuilders.matchQuery("excerpt", searchOptions.getQuery()));
      } else {
        queryBuilder.must(QueryBuilders.matchAllQuery());
      }
      if (!searchOptions.getAuthors().isEmpty() || !searchOptions.getCategories().isEmpty()
            || searchOptions.getCore() != null || !searchOptions.getLabels().isEmpty()) {
        final BoolQueryBuilder filter = QueryBuilders.boolQuery();
        if (!searchOptions.getCategories().isEmpty() && !searchOptions.getLabels().isEmpty()) {
          filter.must(
            QueryBuilders.boolQuery().should(
              QueryBuilders.termsQuery("categories", searchOptions.getCategories())
            ).should(
              QueryBuilders.termsQuery("labels", searchOptions.getLabels())
            )
          );
        } else if (!searchOptions.getCategories().isEmpty()) {
          filter.must(
            QueryBuilders.boolQuery().should(
              QueryBuilders.termsQuery("categories", searchOptions.getCategories())
            )
          );
        } else if (!searchOptions.getLabels().isEmpty()) {
          filter.must(
            QueryBuilders.boolQuery().should(
              QueryBuilders.termsQuery("labels", searchOptions.getLabels())
            )
          );
        }
        if (!searchOptions.getAuthors().isEmpty()) {
          filter.must(
            QueryBuilders.boolQuery().should(
              QueryBuilders.nestedQuery("developers", QueryBuilders.matchQuery("developers.name", searchOptions.getAuthors()))
            )
          );
        }
        if (searchOptions.getCore() != null) {
          filter.must(QueryBuilders.termQuery("requiredCore", searchOptions.getCore()));
        }
        queryBuilder.filter(filter);
      }
      requestBuilder.setQuery(queryBuilder);
{'fraction_non_alphanumeric': 0.11438633102908569, 'fraction_numerical': 0.0007825746706664928, 'mean_word_length': 3.7333333333333334, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3144404', 'n_tokens_mistral': 2057, 'n_tokens_neox': 2054, 'n_words': 299}