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 + } + 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)
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
On Mon, Oct 19, 2020 at 03:16:54PM +0800, Cao Xueliang wrote:
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
"Setting size to 0 avoids executing the fetch phase of the search making the request more efficient." https://www.elastic.co/guide/en/elasticsearch/reference/current/returning-on...
Thanks, Zhang Yuhang
}
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
On Mon, Oct 19, 2020 at 03:23:34PM +0800, Zhang Yuhang wrote:
On Mon, Oct 19, 2020 at 03:16:54PM +0800, Cao Xueliang wrote:
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
"Setting size to 0 avoids executing the fetch phase of the search making the request more efficient." https://www.elastic.co/guide/en/elasticsearch/reference/current/returning-on...
Got it.
Thanks, Zhang Yuhang
}
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
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*')
with this (index: 'jobs*'), seems will get nil if use ESQuery like this: es = ESQuery.new query_result = es.query_by_id('crystal.89912')
Thanks, Weitao
2.23.0