Ejemplo n.º 1
0
    def citations(self):
        """Return citation export for single record."""

        out = []
        row = []

        # Get citations
        record_citations = LiteratureSearch().query_from_iq(
            'refersto:' + str(self.record['control_number'])).params(_source=[
                'control_number', 'citation_count', 'titles', 'earliest_date'
            ]).execute().hits

        for citation in record_citations:

            citation_from_es = LiteratureSearch().get_source(citation.meta.id)

            row.append(
                render_template_to_string("inspirehep_theme/citations.html",
                                          record=citation_from_es))
            try:
                citation_count = citation.citation_count
            except AttributeError:
                citation_count = 0
            row.append(citation_count)
            out.append(row)
            row = []

        return out
Ejemplo n.º 2
0
    def serialize(self, pid, record, links_factory=None):
        search_by_institution = LiteratureSearch().query(
            'match', authors__affiliations__recid=get_id(record)
        ).params(
            _source=[
                'control_number',
            ],
        )

        literature_recids = [
            get_id(el.to_dict()) for el in search_by_institution.scan()]

        search_by_recids = LiteratureSearch().filter(
            'terms', control_number=literature_recids
        ).params(
            _source=[
                'authors.recid',
                'collaborations.value',
                'control_number',
                'earliest_date',
                'facet_inspire_doc_type',
                'inspire_categories',
                'titles.title',
            ],
        )

        return json.dumps(build_citesummary(search_by_recids))
Ejemplo n.º 3
0
def get_and_format_citations(record):
    """.. deprecated:: 2018-08-23"""
    result = []

    citations = LiteratureSearch().query(
        'match', references__recid=record['control_number'],
    ).params(
        _source=[
            'citation_count',
            'control_number',
            'earliest_date',
            'titles',
        ]
    ).execute().hits

    for citation in citations:
        citation_from_es = LiteratureSearch().get_source(citation.meta.id)
        row = []

        row.append(
            render_template_to_string(
                'inspirehep_theme/citations.html',
                record=citation_from_es,
            )
        )

        try:
            citation_count = citation.citation_count
        except AttributeError:
            citation_count = 0
        row.append(citation_count)

        result.append(row)

    return result
def test_google_style_or_google_style():
    query = IQ('sungtae cho or 1301.7261', LiteratureSearch())

    expected = {}
    result = query.to_dict()

    assert expected == result
def test_type_code_colon():
    query = IQ('tc: l', LiteratureSearch())

    expected = {'multi_match': {'query': 'l', 'fields': ['collection']}}
    result = query.to_dict()

    assert expected == result
def test_find_type_code():
    query = IQ('find tc book', LiteratureSearch())

    expected = {'multi_match': {'query': 'book', 'fields': ['collection']}}
    result = query.to_dict()

    assert expected == result
def test_creating_deleted_record_and_undeleting_created_record_in_es(app):
    search = LiteratureSearch()
    json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'titles': [
            {
                'title': 'foo'
            },
        ],
        'deleted': True,
        '_collections': ['Literature']
    }

    # When a record is created in the DB with deleted flag True, it is not created in ES.

    record = InspireRecord.create(json)
    record.commit()
    db.session.commit()
    with pytest.raises(NotFoundError):
        search.get_source(record.id)

    # When a record is undeleted, it is created in ES.
    record['deleted'] = False
    record.commit()
    db.session.commit()
    search.get_source(record.id)
    record._delete(force=True)
def test_author_and_not_type_code():
    query = IQ('a nilles,h and not tc I', LiteratureSearch())

    expected = {}
    result = query.to_dict()

    assert expected == result
def test_author():
    query = IQ('a kondrashuk', LiteratureSearch())

    expected = {}
    result = query.to_dict()

    assert expected == result
def test_google_style_and_not_collaboration():
    query = IQ("raffaele d'agnolo and not cn cms", LiteratureSearch())

    expected = {}
    result = query.to_dict()

    assert expected == result
def test_exactauthor_colon():
    query = IQ('ea:matt visser', LiteratureSearch())

    expected = {}
    result = query.to_dict()

    assert expected == result
def test_eprint_colon_without_arxiv():
    query = IQ('eprint:TODO', LiteratureSearch())

    expected = {}
    result = query.to_dict()

    assert expected == result
def test_citedby_colon_recid_colon():
    query = IQ('citedby:recid:902780', LiteratureSearch())

    expected = {}
    result = query.to_dict()

    assert expected == result
def test_citedby_colon():
    query = IQ('citedby:foobar', LiteratureSearch())

    expected = {}
    result = query.to_dict()

    assert expected == result
def test_author_colon_bai_with_double_quotes_and_collection_colon():
    query = IQ('author:"E.Witten.1" AND collection:citeable',
               LiteratureSearch())

    expected = {
        'bool': {
            'must': [{
                'bool': {
                    'should': [{
                        'match': {
                            u'authors.name_variations': 'E.Witten.1'
                        }
                    }, {
                        'term': {
                            u'authors.ids.value': 'E.Witten.1'
                        }
                    }]
                }
            }, {
                'multi_match': {
                    'fields': ['collections.primary'],
                    'query': 'citeable'
                }
            }],
            'should': [{
                'match': {
                    u'authors.full_name': 'E.Witten.1'
                }
            }]
        }
    }

    result = query.to_dict()

    assert expected == result
def test_author_colon_bai():
    query = IQ('author:Y.Nomura.1', LiteratureSearch())

    expected = {
        'bool': {
            'must': [{
                'bool': {
                    'should': [{
                        'match': {
                            u'authors.name_variations': 'Y.Nomura.1'
                        }
                    }, {
                        'term': {
                            u'authors.ids.value': 'Y.Nomura.1'
                        }
                    }]
                }
            }],
            'should': [{
                'match': {
                    u'authors.full_name': 'Y.Nomura.1'
                }
            }]
        }
    }
    result = query.to_dict()

    assert expected == result
def test_author_colon_with_double_quotes():
    query = IQ('author:"tachikawa, yuji"', LiteratureSearch())

    expected = {
        "bool": {
            "must": [{
                "bool": {
                    "should": [{
                        "match": {
                            "authors.name_variations": "tachikawa, yuji"
                        }
                    }, {
                        "term": {
                            "authors.ids.value": "tachikawa, yuji"
                        }
                    }]
                }
            }],
            "should": [{
                "match": {
                    "authors.full_name": "tachikawa, yuji"
                }
            }]
        }
    }
    result = query.to_dict()

    assert expected == result
def test_find_exactauthor_not_affiliation_uppercase():
    query = IQ('FIND EA RINALDI, MASSIMILIANO NOT AFF SINCROTRONE TRIESTE',
               LiteratureSearch())

    expected = {
        "bool": {
            "must_not": [{
                "multi_match": {
                    "query": "SINCROTRONE TRIESTE",
                    "fields":
                    ["authors.affiliations.value", "corporate_author"]
                }
            }],
            "must": [{
                "multi_match": {
                    "query":
                    "RINALDI, MASSIMILIANO",
                    "fields": [
                        "exactauthor.raw", "authors.full_name",
                        "authors.alternative_names", "authors.ids.value"
                    ]
                }
            }]
        }
    }
    result = query.to_dict()

    assert expected == result
def test_find_author():
    query = IQ('find a polchinski', LiteratureSearch())

    expected = {
        "bool": {
            "must": [{
                "bool": {
                    "should": [{
                        "match": {
                            "authors.name_variations": "polchinski"
                        }
                    }, {
                        "term": {
                            "authors.ids.value": "polchinski"
                        }
                    }]
                }
            }],
            "should": [{
                "match": {
                    "authors.full_name": "polchinski"
                }
            }]
        }
    }
    result = query.to_dict()

    assert expected == result
def test_exactauthor_colon_and_collection_colon():
    query = IQ('ea: matt visser AND collection:citeable', LiteratureSearch())

    expected = {}
    result = query.to_dict()

    assert expected == result
def test_find_author_uppercase():
    query = IQ('FIND A W F CHANG', LiteratureSearch())

    expected = {
        "bool": {
            "must": [{
                "bool": {
                    "should": [{
                        "match": {
                            "authors.name_variations": "W F CHANG"
                        }
                    }, {
                        "term": {
                            "authors.ids.value": "W F CHANG"
                        }
                    }]
                }
            }],
            "should": [{
                "match": {
                    "authors.full_name": "W F CHANG"
                }
            }]
        }
    }
    result = query.to_dict()

    assert expected == result
Ejemplo n.º 22
0
def test_that_db_changes_are_mirrored_in_es(app):
    search = LiteratureSearch()
    json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'titles': [
            {'title': 'foo'},
        ],
    }

    # When a record is created in the DB, it is also created in ES.

    record = InspireRecord.create(json)
    es_record = search.get_source(record.id)

    assert get_title(es_record) == 'foo'

    # When a record is updated in the DB, is is also updated in ES.

    record['titles'][0]['title'] = 'bar'
    record.commit()
    es_record = search.get_source(record.id)

    assert get_title(es_record) == 'bar'

    # When a record is deleted in the DB, it is also deleted in ES.

    record._delete(force=True)

    with pytest.raises(NotFoundError):
        es_record = search.get_source(record.id)
def test_field_code_colon():
    query = IQ('fc: a', LiteratureSearch())

    expected = {'multi_match': {'query': 'a', 'fields': ['field_code']}}
    result = query.to_dict()

    assert expected == result
def test_author_or_author():
    query = IQ('a fileviez perez,p or p. f. perez', LiteratureSearch())

    expected = {}
    result = query.to_dict()

    assert expected == result
Ejemplo n.º 25
0
def suggest():
    """Power typeahead.js search bar suggestions."""
    field = request.values.get('field')
    query = request.values.get('query')

    search = LiteratureSearch()
    search = search.suggest('suggestions', query, completion={"field": field})
    suggestions = search.execute_suggest()

    if field == "authors.name_suggest":
        bai_name_map = {}
        for suggestion in suggestions['suggestions'][0]['options']:
            bai = suggestion['_source']['bai']
            if bai in bai_name_map:
                bai_name_map[bai].append(suggestion['text'])
            else:
                bai_name_map[bai] = [suggestion['text']]

        result = []
        for key, value in six.iteritems(bai_name_map):
            result.append({
                'name': max(value, key=len),
                'value': key,
                'template': 'author'
            })

        return jsonify({'results': result})

    return jsonify({
        'results': [{
            'value': s['text']
        } for s in suggestions['suggestions'][0]['options']]
    })
def test_fulltext_colon():
    query = IQ('fulltext:TODO', LiteratureSearch())

    expected = {}
    result = query.to_dict()

    assert expected == result
def test_journal_colon():
    query = IQ('journal:TODO', LiteratureSearch())

    expected = {}
    result = query.to_dict()

    assert expected == result
def test_topcite_colon():
    query = IQ('topcite:200+', LiteratureSearch())

    expected = {}
    result = query.to_dict()

    assert expected == result
def test_deleting_record_triggers_delete_in_es(app):
    search = LiteratureSearch()
    json = {
        '$schema': 'http://localhost:5000/schemas/records/hep.json',
        'document_type': [
            'article',
        ],
        'titles': [
            {
                'title': 'foo'
            },
        ],
        '_collections': ['Literature']
    }

    # When a record is created in the DB, it is also created in ES.

    record = InspireRecord.create(json)
    record.commit()
    db.session.commit()
    search.get_source(record.id)

    # When a record is updated with deleted flag true, it is deleted in ES
    record['deleted'] = True
    record.commit()
    db.session.commit()
    with pytest.raises(NotFoundError):
        search.get_source(record.id)
def test_author_bai():
    query = IQ('find a r.j.hill.1', LiteratureSearch())

    expected = {
        "bool": {
            "must": [{
                "bool": {
                    "should": [{
                        "match": {
                            "authors.name_variations": "r.j.hill.1"
                        }
                    }, {
                        "term": {
                            "authors.ids.value": "r.j.hill.1"
                        }
                    }]
                }
            }],
            "should": [{
                "match": {
                    "authors.full_name": "r.j.hill.1"
                }
            }]
        }
    }
    result = query.to_dict()

    assert expected == result