metron-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From merrimanr <...@git.apache.org>
Subject [GitHub] metron pull request #679: METRON-1075: Add faceted search capabilities
Date Thu, 03 Aug 2017 14:07:39 GMT
Github user merrimanr commented on a diff in the pull request:

    https://github.com/apache/metron/pull/679#discussion_r131151368
  
    --- Diff: metron-platform/metron-elasticsearch/src/main/java/org/apache/metron/elasticsearch/dao/ElasticsearchDao.java
---
    @@ -179,4 +206,43 @@ public void init(Map<String, Object> globalConfig, AccessConfig
config) {
         return latestIndices.values().toArray(new String[latestIndices.size()]);
       }
     
    +  public void addFacetFields(SearchSourceBuilder searchSourceBuilder, List<String>
fields) {
    +    for(String field: fields) {
    +      searchSourceBuilder = searchSourceBuilder.aggregation(new TermsBuilder(getAggregationName(field)).field(field));
    +    }
    +  }
    +
    +  public Map<String, Map<String, Long>> getFacetCounts(List<String>
fields, Aggregations aggregations, Map<String, FieldType> commonColumnMetadata) {
    +    Map<String, Map<String, Long>> fieldCounts = new HashMap<>();
    +    for (String field: fields) {
    +      Map<String, Long> valueCounts = new HashMap<>();
    +      Aggregation aggregation = aggregations.get(getAggregationName(field));
    +      if (aggregation instanceof LongTerms) {
    +        LongTerms longTerms = (LongTerms) aggregation;
    +        FieldType type = commonColumnMetadata.get(field);
    +        if (FieldType.IP.equals(type)) {
    +          longTerms.getBuckets().stream().forEach(bucket -> valueCounts.put(IpFieldMapper.longToIp((Long)
bucket.getKey()), bucket.getDocCount()));
    +        } else if (FieldType.BOOLEAN.equals(type)) {
    +          longTerms.getBuckets().stream().forEach(bucket -> {
    +            String key = (Long) bucket.getKey() == 1 ? "true" : "false";
    +            valueCounts.put(key, bucket.getDocCount());
    +          });
    +        } else {
    +          longTerms.getBuckets().stream().forEach(bucket -> valueCounts.put(bucket.getKeyAsString(),
bucket.getDocCount()));
    +        }
    +      } else if (aggregation instanceof DoubleTerms) {
    +        DoubleTerms doubleTerms = (DoubleTerms) aggregation;
    +        doubleTerms.getBuckets().stream().forEach(bucket -> valueCounts.put(bucket.getKeyAsString(),
bucket.getDocCount()));
    +      } else if (aggregation instanceof StringTerms) {
    +        StringTerms stringTerms = (StringTerms) aggregation;
    +        stringTerms.getBuckets().stream().forEach(bucket -> valueCounts.put(bucket.getKeyAsString(),
bucket.getDocCount()));
    +      }
    +      fieldCounts.put(field, valueCounts);
    --- End diff --
    
    The ES java api can be pretty awkward to use.  This is a good example of that.  The Aggregation
object is abstract and has 3 subclasses related to term aggregations:  LongTerms, DoubleTerms
and StringTerms.  All types fall into one of these (as far as I could tell) which is one reason
we needed a function to get the fields types.  For example, an aggregation for a field of
type "ip" is represented as a LongTerms object and the value returned is also a long.  To
get it to display correct we need to convert it to a string representation of the ip address.
 Same thing for booleans.  There are returned as LongTerms with a value of 1 or 0.
    
    Hopefully I covered all the different types in the integration tests.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message