On Mon, Oct 19, 2020 at 02:48:24PM +0800, Zhang Yuhang wrote:
Signed-off-by: Zhang Yuhang zhangyuhang25@huawei.com
lib/es_query.rb | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-)
diff --git a/lib/es_query.rb b/lib/es_query.rb index aaaadc2..9a4e45a 100644 --- a/lib/es_query.rb +++ b/lib/es_query.rb @@ -9,12 +9,20 @@ require_relative 'constants.rb' class ESQuery HOST = (ENV.key?('ES_HOST') ? ENV['ES_HOST'] : ES_HOST) PORT = (ENV.key?('ES_PORT') ? ENV['ES_PORT'] : ES_PORT).to_i
- def initialize(host = HOST, port = PORT, index: 'jobs')
def initialize(host = HOST, port = PORT, index: 'jobs*') @index = index @client = Elasticsearch::Client.new url: "http://#%7Bhost%7D:#%7Bport%7D" raise 'Connect Elasticsearch error!' unless @client.ping end
def es_query(query)
@client.search index: @index, body: query
end
def es_count(query)
@client.count(index: @index, body: query)['count']
end
# Example @items: { key1 => value1, key2 => [value2, value3, ..], ...} # means to query: key1 == value1 && (key2 in [value2, value3, ..]) def multi_field_query(items, size: 10_000)
@@ -26,7 +34,23 @@ class ESQuery } }, size: size }
- @client.search index: 'jobs*', body: query
- es_query(query)
- end
- def all_values(field, size: 1000)
- query = {
aggs: {
all_field: {
terms: { field: field, size: size }
}
},
size: 0
What's the function of this line?
Thanks, Xueliang
}
es_result = es_query(query)['aggregations']['all_field']['buckets']
es_result.sort_by! { |h| h['doc_count'] }
es_result.reverse!.map! { |x| x['key'] }
es_result end
def query_by_id(id)
-- 2.23.0