Class: RKD::Query::ElasticSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/r_k_d/query/elastic_search.rb

Constant Summary collapse

ELASTIC_SEARCH_ENDPOINT =
"https://api.rkd.triply.cc/datasets/rkd/RKD-Search-Graph/services/Elasticsearch/_search"

Class Method Summary collapse

Class Method Details

.find(id, dataset: "artists") ⇒ Object



54
55
56
57
58
59
60
# File 'lib/r_k_d/query/elastic_search.rb', line 54

def find(id, dataset: "artists")
  data = FileCache.cache("es-#{dataset}-find-#{id}") do
    HTTP.post_json ELASTIC_SEARCH_ENDPOINT, query(id:, dataset:)
  end

  JSON.parse(data)["hits"]["hits"].map { |hit| hit["_source"] }.first
end

.query(dataset:, name: nil, id: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/r_k_d/query/elastic_search.rb', line 7

def query(dataset:, name: nil, id: nil)
  base_query = {
    query: {
      bool: {
        filter: [
          {
            match: {"https://data rkd nl/search#datasetName": dataset}
          }
        ],
        must: [],
        should: []
      }
    }
  }

  if name
    base_query[:query][:bool][:should] << {
      simple_query_string: {
        query: name,
        fields: [
          "http://www w3 org/2000/01/rdf-schema#label"
        ]
      }
    }
    base_query[:query][:bool][:must] << {
      simple_query_string: {query: name}
    }
  end
  if id
    base_query[:query][:bool][:must] << {
      match: {
        _id: "https://data.rkd.nl/artists/#{id}"
      }
    }
  end

  base_query
end

.search(name, dataset: "artists") ⇒ Object



46
47
48
49
50
51
52
# File 'lib/r_k_d/query/elastic_search.rb', line 46

def search(name, dataset: "artists")
  data = FileCache.cache("es-#{dataset}-search-#{name}") do
    HTTP.post_json ELASTIC_SEARCH_ENDPOINT, query(name:, dataset:)
  end

  JSON.parse(data)["hits"]["hits"].map { |hit| hit["_source"] }
end