<commit_before>/*
 * Copyright (c) 2018 Redlink GmbH.
 */
package com.rbmhtechnology.vind.solr.backend;

import com.google.common.collect.Sets;
import com.rbmhtechnology.vind.api.query.filter.Filter;
import com.rbmhtechnology.vind.model.DocumentFactory;
import com.rbmhtechnology.vind.model.FieldDescriptor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.search.facet.FacetRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

import static com.rbmhtechnology.vind.api.query.filter.Filter.*;
import static com.rbmhtechnology.vind.solr.backend.SolrUtils.Fieldname.TYPE;

/**
 * Created on 10.08.18.
 */
public class ChildrenFilterSerializer {

    private static Logger log = LoggerFactory.getLogger(SolrFilterSerializer.class);

    private final DocumentFactory parentFactory;
    private final boolean strict;

    private final String TYPE_FILTER = "%s:%s";
    private final String CHILD_QUERY_TEMPLATE = "{!parent which='%s:%s' v='%s AND %s'}";

    private String searchContext;
    private final DocumentFactory childFactory;
    private final boolean childrenSearch;

    public ChildrenFilterSerializer(DocumentFactory parentFactory, DocumentFactory childFactory, String searchContext, boolean strict, boolean childrenSearch){
        this.parentFactory = parentFactory;
        this.childFactory = childFactory;
        this.searchContext = searchContext;
        this.strict = strict;
        this.childrenSearch = childrenSearch;
    }

    public String serialize(Filter filter){
        final Filter normalizedFilter = normalize(filter);

        if (AndFilter.class.isAssignableFrom(normalizedFilter.getClass()))
            return serialize((AndFilter)normalizedFilter);
        else if (OrFilter.class.isAssignableFrom(normalizedFilter.getClass()))
            return serialize((OrFilter)normalizedFilter);
        else if (isHierarchicalFilter(normalizedFilter)) {
             final String parentFilter =  "(" + new SolrFilterSerializer(parentFactory, strict).serialize(normalizedFilter,searchContext) +")";
             return parentFilter;
        } else {
            final String childFilter = "(" + new SolrFilterSerializer(childFactory, strict).serialize(normalizedFilter,searchContext) +")";
            return  String.format(CHILD_QUERY_TEMPLATE,
                    TYPE,
                    parentFactory.getType(),
                    String.format(TYPE_FILTER, TYPE, childFactory.getType()),
                    childFilter);
        }

    }

    private String serialize(AndFilter filter){

        //Get the filters which apply to the parent<commit_msg>#46: removed unnecesarry parenthesys<commit_after>/*
 * Copyright (c) 2018 Redlink GmbH.
 */
package com.rbmhtechnology.vind.solr.backend;

import com.google.common.collect.Sets;
import com.rbmhtechnology.vind.api.query.filter.Filter;
import com.rbmhtechnology.vind.model.DocumentFactory;
import com.rbmhtechnology.vind.model.FieldDescriptor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.search.facet.FacetRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

import static com.rbmhtechnology.vind.api.query.filter.Filter.*;
import static com.rbmhtechnology.vind.solr.backend.SolrUtils.Fieldname.TYPE;

/**
 * Created on 10.08.18.
 */
public class ChildrenFilterSerializer {

    private static Logger log = LoggerFactory.getLogger(SolrFilterSerializer.class);

    private final DocumentFactory parentFactory;
    private final boolean strict;

    private final String TYPE_FILTER = "%s:%s";
    private final String CHILD_QUERY_TEMPLATE = "{!parent which='%s:%s' v='%s AND %s'}";

    private String searchContext;
    private final DocumentFactory childFactory;
    private final boolean childrenSearch;

    public ChildrenFilterSerializer(DocumentFactory parentFactory, DocumentFactory childFactory, String searchContext, boolean strict, boolean childrenSearch){
        this.parentFactory = parentFactory;
        this.childFactory = childFactory;
        this.searchContext = searchContext;
        this.strict = strict;
        this.childrenSearch = childrenSearch;
    }

    public String serialize(Filter filter){
        final Filter normalizedFilter = normalize(filter);

        if (AndFilter.class.isAssignableFrom(normalizedFilter.getClass()))
            return serialize((AndFilter)normalizedFilter);
        else if (OrFilter.class.isAssignableFrom(normalizedFilter.getClass()))
            return serialize((OrFilter)normalizedFilter);
        else if (isHierarchicalFilter(normalizedFilter)) {
             final String parentFilter =  new SolrFilterSerializer(parentFactory, strict).serialize(normalizedFilter,searchContext);
             return parentFilter;
        } else {
            final String childFilter = new SolrFilterSerializer(childFactory, strict).serialize(normalizedFilter,searchContext);
            return  String.format(CHILD_QUERY_TEMPLATE,
                    TYPE,
                    parentFactory.getType(),
                    String.format(TYPE_FILTER, TYPE, childFactory.getType()),
                    childFilter);
        }

    }

    private String serialize(AndFilter filter){

        //Get the filters which apply to the parent
{'fraction_non_alphanumeric': 0.09145783793671118, 'fraction_numerical': 0.005121638924455826, 'mean_word_length': 3.6182432432432434, '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': '2020771', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1433, 'n_words': 314}