/**Copyright 2019 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.google.error_analyzer.backend;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.error_analyzer.data.constant.Keywords;
import com.google.error_analyzer.data.constant.LogFields;
import com.google.error_analyzer.data.constant.RegexStrings;
import com.google.error_analyzer.data.constant.StackTraceFormat;
import com.google.error_analyzer.data.Document;
import java.util.*;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RegexpQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

/** Build search request for boolean query.
* Match query uses Keywords class for query string
* Regex query uses RegexStrings class for query string
*/

public class BooleanQuery {
    private final static Integer requestSize = 10000; //limited by ElasticSearch settings
    private final static Integer minimumMatch = 1;
    private final StackTraceFormat stackTraceFormat = new StackTraceFormat();

    //create searchRequest to seach index file for errors
    public SearchRequest createSearchRequest(String fileName) {
        String matchQueryString = Keywords.getQueryString();
        String regexQueryString = RegexStrings.getQueryString();
        BoolQueryBuilder boolQuery = buildBoolQuery(matchQueryString, regexQueryString);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
            .size(requestSize)
            .query(boolQuery)
            .sort(LogFields.LOG_LINE_NUMBER);
        SearchRequest searchRequest = new SearchRequest(fileName);
        searchRequest.source(searchSourceBuilder);
        return searchRequest;
    }

    public ImmutableList < Document > filterBoolQuerySearchHits (SearchHits hits) {
        Builder < Document > documentList = ImmutableList.< Document > builder();
        for (SearchHit hit : hits) {
            String logText = (String) hit.getSourceAsMap().get(LogFields.LOG_TEXT);
            if (filterForError(logText)) {
                Document document = new Document(hit.getId(), hit.getSourceAsString());
                documentList.add(document);
            }
        }
        return documentList.build();
    }

    private Boolean filterForError(String logText) {
        if(stackTraceFormat.matchesFormat(logText)){
            return false;
        }
        return true;
    }
    
    public ImmutableList < Document > creatIndexRequestForErrors (SearchHits hits) {
        Builder < Document > documentList = ImmutableList.< Document > builder();
        for (SearchHit hit : hits) {
            Document document = new Document(hit.getId(), hit.getSourceAsString());
            documentList.add(document);
        }
        return documentList.build();
    }

    //Combine matchquery and regex query and return a bool query
    private BoolQueryBuilder buildBoolQuery(String matchQueryString, 
    String regexQueryString) {
        MatchQueryBuilder matchQuery = buildMatchQuery(matchQueryString);
        RegexpQueryBuilder regexQuery = buildRegexQuery(regexQueryString);
        BoolQueryBuilder boolQuery = new BoolQueryBuilder()
            .minimumShouldMatch(minimumMatch)
            .should(regexQuery)
            .should(matchQuery);
        return boolQuery;
    }

    private MatchQueryBuilder buildMatchQuery(String matchQueryString) {
        return new MatchQueryBuilder
            (LogFields.LOG_TEXT, matchQueryString);
    }

    private RegexpQueryBuilder buildRegexQuery(String regexQueryString) {
        return new RegexpQueryBuilder
            (LogFields.LOG_TEXT,regexQueryString);
    }
}
@@ -1818,70 +1818,50 @@
 ate 
-final StackTraceFormat stackTraceFormat = new StackTraceFormat
+Set%3CString%3E hash_Set = new HashSet%3CString%3E
 ();%0A
@@ -2693,32 +2693,33 @@
 ent %3E builder();
+ 
 %0A        for (Se
@@ -3131,17 +3131,17 @@
      if(
-s
+S
 tackTrac
@@ -3201,32 +3201,196 @@
 alse;%0A        %7D%0A
+        logText = logText.replaceAll(%22%5B%5Ea-zA-Z%5D%22, %22 %22);%0A        if(hash_Set.contains(logText)) %7B%0A            return false;%0A        %7D%0A        hash_Set.add(logText);%0A
         return t

Add hash set for errors