Beispiel #1
0
    def get(self):
        """HTTP Get that enables boolean query processing and search."""
        query = request.args.get('query')

        if query is None:
            abort(400)

        q_parser = parser.QueryParser()
        q_builder = builder.QueryBuilder()

        pf_query = q_parser.parse_query(query)
        elastic_query = q_builder.build(pf_query)

        # response = Faculty.search().query(elastic_query).execute()
        response = Keywords.search().query(elastic_query).execute()
        faculty_with_keywords = set()
        for keywords in response:
            faculty_with_keywords.add(keywords.faculty_id)
        schema = FacultySchema()
        results = [
            schema.dump(Faculty.safe_get(faculty_id))
            for faculty_id in faculty_with_keywords
        ]

        return {"data": results}
Beispiel #2
0
    def get(self):
        """HTTP Get that enables boolean query processing and search."""
        query = request.args.get('query')
        dept = request.args.get('department')

        if query is None:
            abort(400)

        # Take dept string and turn it into an easy to compare set.
        try:
            if dept is not None:
                dept_filter = set([x.strip() for x in dept.split(',')])
            else:
                dept_filter = set()
        except:
            abort(400)

        q_parser = parser.QueryParser()
        q_builder = builder.QueryBuilder()

        try:
            pf_query = q_parser.parse_query(query)
        except parser.QueryException:
            abort(400)

        keywords_elastic_query = q_builder.build(pf_query)        
        response = Keywords.search().query(keywords_elastic_query).execute()    
        faculty_with_keywords = SearchAPI.get_faculty_with_keywords(response)
        
        SearchAPI.add_name_search_results(faculty_with_keywords, pf_query)
        
        return {
            "data": SearchAPI.create_results(faculty_with_keywords, dept_filter)
        }
Beispiel #3
0
    def get(self):
        """HTTP Get that enables boolean query processing and search."""
        query = request.args.get('query')

        if query is None:
            abort(400)

        q_parser = parser.QueryParser()
        q_builder = builder.QueryBuilder()

        pf_query = q_parser.parse_query(query)
        elastic_query = q_builder.build(pf_query)

        response = Faculty.search().query(elastic_query).execute()
        schema = FacultySchema()
        results = [schema.dump(faculty) for faculty in response]

        return {"data": results}
Beispiel #4
0
    def get(self):
        query = request.args.get('query')
        approach = request.args.get('approach')
        approach = int(approach)

        if query is None or approach is None:
            abort(400)

        q_parser = parser.QueryParser()
        q_builder = builder.QueryBuilder()

        try:
            pf_query = q_parser.parse_query(query)
        except parser.QueryException:
            abort(400)

        keywords_elastic_query = q_builder.build(pf_query)
        response = Keywords.search().query(keywords_elastic_query).execute()
        faculty_with_keywords = SearchAPI.get_faculty_with_keywords(response)

        empty_profs = []
        for faculty_id, keywords in faculty_with_keywords.items():
            filtered_keywords = []
            for keyword_obj in keywords:
                if keyword_obj.approach_id == approach:
                    filtered_keywords.append(keyword_obj)
            faculty_with_keywords[faculty_id] = filtered_keywords

            if len(filtered_keywords) == 0:
                empty_profs.append(faculty_id)

        for faculty_id in empty_profs:
            del faculty_with_keywords[faculty_id]

        results = SearchAPI.create_results(faculty_with_keywords,
                                           dept_filter=[])
        str_io = BytesIO()
        str_io.write(json.dumps(results, indent=4).encode())
        str_io.seek(0)

        return send_file(str_io,
                         as_attachment=True,
                         attachment_filename="batch_results.txt")
Beispiel #5
0
class TestSearchResults(object):
    query_builder = builder.QueryBuilder()
    query_parser = parser.QueryParser()

    @classmethod
    def setup_class(cls):
        Keywords.search().delete()
        Faculty.search().delete()
        sleep(3)

        will = Faculty(meta={"id": 379},
                       name="William.Allison",
                       full_name="Allison, William.",
                       faculty_id=379,
                       email="*****@*****.**")
        will.save()
        will_keywords = Keywords(
            faculty_id=379,
            datasource="test",
            approach_id=0,
            keywords=["zebrafish", "evolutionary processes", "marine"])
        will_keywords.save()

        vince = Faculty(meta={"id": 356},
                        name="Vincent.Bouchard",
                        full_name="Bouchard, Vincent",
                        faculty_id=356,
                        email="*****@*****.**")
        vince.save()
        vince_keywords = Keywords(
            faculty_id=356,
            datasource="test",
            approach_id=0,
            keywords=["string theory", "number theory", "mathematics"])
        vince_keywords.save()

        sleep(3)

    def test_simple_search(self):
        """Test the results of queries on actual data."""
        query = "zebrafish"
        elastic_query = self.build_query(query)
        results = Keywords.search().query(elastic_query).execute()
        assert len(results) == 1

        query = "zebrafish OR mathematics"
        elastic_query = self.build_query(query)
        results = Keywords.search().query(elastic_query).execute()
        assert len(results) == 2

        query = "zebrafish AND mathematics"
        elastic_query = self.build_query(query)
        results = Keywords.search().query(elastic_query).execute()
        assert len(results) == 0

    def test_add_name_to_search_results(self):
        faculty_with_keywords = {}
        query = "zebrafish"
        pf_query = self.query_parser.parse_query(query)
        SearchAPI.add_name_search_results(faculty_with_keywords, pf_query)
        assert len(faculty_with_keywords) == 0

        faculty_with_keywords = {}
        query = "\"Allison, William\""
        pf_query = self.query_parser.parse_query(query)
        SearchAPI.add_name_search_results(faculty_with_keywords, pf_query)
        assert len(faculty_with_keywords) == 1
        assert 379 in faculty_with_keywords

        faculty_with_keywords = {}
        query = "\"Allison, William\" OR \"Bouchard, Vincent\""
        pf_query = self.query_parser.parse_query(query)
        SearchAPI.add_name_search_results(faculty_with_keywords, pf_query)
        assert len(faculty_with_keywords) == 2
        assert 379 in faculty_with_keywords
        assert 356 in faculty_with_keywords

        faculty_with_keywords = {}
        query = "\"Allison, William\" AND \"Bouchard, Vincent\""
        pf_query = self.query_parser.parse_query(query)
        SearchAPI.add_name_search_results(faculty_with_keywords, pf_query)
        assert len(faculty_with_keywords) == 0

    def build_query(self, query, search_field="keywords"):
        """Utility function to parse and build an elasticsearch query."""
        pf_query = self.query_parser.parse_query(query)
        return self.query_builder.build(pf_query, search_field=search_field)

    @classmethod
    def teardown_class(cls):
        Faculty.get(id=379).delete()
        Faculty.get(id=356).delete()

        Keywords.search().query('match', faculty_id=379).delete()
        Keywords.search().query('match', faculty_id=356).delete()