new: Improve search

pull/3/head
Raphaël Vinot 2018-07-06 15:28:53 +02:00
parent ec89c67858
commit 98ab8ddbc3
3 changed files with 22 additions and 6 deletions

View File

@ -177,6 +177,7 @@ class ClusterValue():
self.searchable.append(self.uuid) self.searchable.append(self.uuid)
if self.meta and self.meta.synonyms: if self.meta and self.meta.synonyms:
self.searchable += self.meta.synonyms self.searchable += self.meta.synonyms
self.searchable = list(set(self.searchable))
def __init_meta(self, m): def __init_meta(self, m):
if not m: if not m:
@ -219,10 +220,14 @@ class Cluster(collections.Mapping):
raise PyMISPGalaxiesError("Duplicate value ({}) in cluster: {}".format(new_cluster_value.value, self.name)) raise PyMISPGalaxiesError("Duplicate value ({}) in cluster: {}".format(new_cluster_value.value, self.name))
self.cluster_values[new_cluster_value.value] = new_cluster_value self.cluster_values[new_cluster_value.value] = new_cluster_value
def search(self, query): def search(self, query, return_tags=False):
matching = [] matching = []
for v in self.values(): for v in self.values():
if [s for s in v.searchable if query.lower() in s.lower()]: if [s for s in v.searchable if query.lower() in s.lower()]:
if return_tags:
matching.append('misp-galaxy:{}="{}"'.format(self.type, v.value))
pass
else:
matching.append(v) matching.append(v)
return matching return matching
@ -291,10 +296,10 @@ class Clusters(collections.Mapping):
except Exception: except Exception:
raise UnableToRevertMachinetag('The machinetag {} could not be found.'.format(machinetag)) raise UnableToRevertMachinetag('The machinetag {} could not be found.'.format(machinetag))
def search(self, query): def search(self, query, return_tags=False):
to_return = [] to_return = []
for cluster in self.values(): for cluster in self.values():
values = cluster.search(query) values = cluster.search(query, return_tags)
if not values: if not values:
continue continue
to_return.append((cluster, values)) to_return.append((cluster, values))

@ -1 +1 @@
Subproject commit 308774755c8d5b01bfd30ea0ab868c83fdfdd715 Subproject commit e5939e3248591c0e454a8a25a9cbf21f6318541d

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import unittest import unittest
@ -7,6 +7,7 @@ from glob import glob
import os import os
import json import json
from collections import Counter from collections import Counter
import warnings
class TestPyMISPGalaxies(unittest.TestCase): class TestPyMISPGalaxies(unittest.TestCase):
@ -16,6 +17,16 @@ class TestPyMISPGalaxies(unittest.TestCase):
self.clusters = Clusters(skip_duplicates=True) self.clusters = Clusters(skip_duplicates=True)
self.maxDiff = None self.maxDiff = None
def test_searchable(self):
for cluster in self.clusters.values():
all_searchable = []
for c_values in cluster.values():
all_searchable += c_values.searchable
count = Counter(all_searchable)
for k, v in count.items():
if v != 1:
warnings.warn(f'Duplicate on {cluster.type}: {k}')
def test_duplicates(self): def test_duplicates(self):
has_duplicates = False has_duplicates = False
for name, c in self.clusters.items(): for name, c in self.clusters.items():