Skip to content
This repository was archived by the owner on Aug 20, 2025. It is now read-only.

Conversation

@merrimanr
Copy link
Contributor

@merrimanr merrimanr commented Aug 17, 2017

Contributor Comments

This PR adds a group by feature to the search REST endpoint. This PR is fairly complex so I don't think it should be merged before a thorough discussion and review.

The following is an intentionally simple example of how group by works. This PR exposes a new REST endpoint ("/api/v1/search/group") that allows a user to group results based on a field or list of fields. For example, grouping results by a field called "fieldA" would be done by POSTing a GroupRequest object that looks like:

{
  "groups": [
    {
      "field": "fieldA",
      "order": {
        "groupOrderType": "term or count",
        "sortOrder": "desc or asc"
      }
    }
  ],
  "indices": [
    "index to search"
  ],
  "query": "lucene query",
  "scoreField": "score"
}

Grouping by multiple fields would just be a matter of adding another group object:

{
  "groups": [
    {
      "field": "fieldA",
      "order": {
        "groupOrderType": "term or count",
        "sortOrder": "desc or asc"
      }
    },
    {
      "field": "fieldB"
    }
  ],
  "indices": [
    "index to search"
  ],
  "query": "lucene query"
}

The "order" property defaults to count descending so it is optional. The groups would then be nested based on the order they appear in the list:

{
  "groupedBy": "fieldA",
  "groupResults": [
    {
      "key": "fieldA value1",
      "total": 100,
      "groupedBy": "fieldB",
      "groups": [
        {
          "key": "fieldB value1",
          "total": 200
        },
        {
          "key": "fieldB value2",
          "total": 150
        }
      ]
    } 
  ]
}

The "scoreField" property is also optional and can be used to calculate the sum of this field within each group. This has been verified and tested in full dev. For example, this search query:

{
  "groups": [
    {
      "field": "ip_src_addr"
    },
    {
      "field": "ip_src_port",
      "order": {
        "groupOrderType": "term",
        "sortOrder": "asc"
      }
    }
  ],
  "indices": [
    "bro"
  ],
  "query": "*"
}

should return search results properly grouped by those fields. The result should like something like:

{
  "groupedBy": "ip_src_addr",
  "groupResults": [
    {
      "key": "192.168.138.158",
      "total": 5140,
      "groupedBy": "ip_src_port",
      "groups": [
        {
          "key": "49184",
          "total": 121
        },
        {
          "key": "49185",
          "total": 113
        },
        {
          "key": "49186",
          "total": 121
        },
        ...
      ]
    },
    {
      "key": "192.168.66.1",
      "total": 2160,
      "groupedBy": "ip_src_port",
      "groups": [
        {
          "key": "5353",
          "total": 670
        },
        {
          "key": "50451",
          "total": 1490
        }
      ]
    }
  ]
}

There are a couple of outstanding issues that we need to work through before this gets merged:

  • I also did a fair amount of refactoring in an attempt to make this class easier to read. If that is not the case and the refactoring made it worse, let me know.

  • It has been requested in the past and I think we definitely need more detailed documentation around this particular endpoint. I am planning on adding some extensive documentation to this PR once we settle on an API design approach. The alerts UI isn't quite ready for this feature so there's no rush in getting this merged.

Pull Request Checklist

Thank you for submitting a contribution to Apache Metron.
Please refer to our Development Guidelines for the complete guide to follow for contributions.
Please refer also to our Build Verification Guidelines for complete smoke testing guides.

In order to streamline the review of the contribution we ask you follow these guidelines and ask you to double check the following:

For all changes:

  • Is there a JIRA ticket associated with this PR? If not one needs to be created at Metron Jira.
  • Does your PR title start with METRON-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
  • Has your PR been rebased against the latest commit within the target branch (typically master)?

For code changes:

  • Have you included steps to reproduce the behavior or problem that is being changed or addressed?

  • Have you included steps or a guide to how the change may be verified and tested manually?

  • Have you ensured that the full suite of tests and checks have been executed in the root metron folder via:

    mvn -q clean integration-test install && build_utils/verify_licenses.sh 
    
  • Have you written or updated unit tests and or integration tests to verify your changes?

  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?

  • Have you verified the basic functionality of the build by building and running locally with Vagrant full-dev environment or the equivalent?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered by building and verifying the site-book? If not then run the following commands and the verify changes via site-book/target/site/index.html:

    cd site-book
    mvn site
    

Note:

Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.
It is also recommended that travis-ci is set up for your personal repository such that your branches are built there before submitting a pull request.

@iraghumitra
Copy link
Contributor

@merrimanr Does the group by support pagination?

@merrimanr
Copy link
Contributor Author

No, the "from" parameter is not supported.

@merrimanr
Copy link
Contributor Author

The latest commit moves this group by function to it's own REST endpoint. Combining groups and search results became too awkward once I started looking into adding sorting and size constraints to each group. I like this better. The original description has been updated to match.

# Conflicts:
#	metron-platform/metron-elasticsearch/src/main/java/org/apache/metron/elasticsearch/dao/ElasticsearchDao.java
#	metron-platform/metron-indexing/src/test/java/org/apache/metron/indexing/dao/SearchIntegrationTest.java
@merrimanr
Copy link
Contributor Author

The latest commit adds the ability to sum a field within each group with the "scoreField".


private TermsBuilder getGroupsTermBuilder(GroupRequest groupRequest, int index) {
List<Group> groups = groupRequest.getGroups();
Group group = groups.get(index);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the groups field is empty, this will end up throwing an exception:

{
  "timestamp": "2017-09-08 10:44:34",
  "status": 500,
  "error": "Internal Server Error",
  "exception": "java.lang.IndexOutOfBoundsException",
  "message": "Index: 0, Size: 0",
  "path": "/api/v1/search/group"
}

It seems like we should either return no results (after all, if there's nothing requested in the group by, then no results seems at least vaguely reasonable) or throw a more informative exception

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a better exception


public enum GroupOrderType {

@JsonProperty("count")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a place we can document what the options for this are? Right now it doesn't look like it's exposed to people using the endpoint

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some documentation to the README.

String hbaseProviderImpl = environment.getProperty(MetronRestConstants.INDEX_HBASE_TABLE_PROVIDER_IMPL, String.class, null);
String indexDaoImpl = environment.getProperty(MetronRestConstants.INDEX_DAO_IMPL, String.class, null);
int searchMaxResults = environment.getProperty(MetronRestConstants.SEARCH_MAX_RESULTS, Integer.class, -1);
int searchMaxGroups = environment.getProperty(MetronRestConstants.SEARCH_MAX_GROUPS, Integer.class, 1000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we say SEARCH_MAX_RESULTS grabs a default of -1, but SEARCH_MAX_GROUPS grabs a default of 1000? I'd be inclined to make them consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the SEARCH_MAX_RESULTS default to 1000.

@justinleet
Copy link
Contributor

This is really good, thanks for the contribution! Are we intending (as a follow-on activity), to enable something like top hits aggregation in here so we can get the documents in the buckets back, or are we just expecting follow on queries to be the norm?

At least for using the API, it seems like it would be nice to at least have something rudimentary, but again, that's not a this PR thing.

@merrimanr
Copy link
Contributor Author

The latest commit should address the feedback so far. I don't feel like the top hits aggregation is necessary at this point but it could be in the future. I would prefer to wait and keep it simple until we have a need for it.

@justinleet
Copy link
Contributor

Updates look good.

I'm +1. Thanks again for contributing this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants