如何在 elasticsearc_dsl 中搜索 md5 字段
How to search on md5 fields in elasticsearc_dsl
我是 Elasticsearch 的新手,elasticsearch_dsl 我遇到了一个问题,我想对 md5 字段进行研究,但我不知道我是否做对了。
这是我存储的文件:
"data": {
"uniqueInfo": {
"md5_of_my_unique_info": "a3e2c73ab0aaze73881db1a889826ada",
}
md5_of_my_unique_info 是一个很有价值的散列,我想研究一下它是否存在于数据库中,所以我这样做了:
es_host = {"host": "localhost", "port": 9200}
es = Elasticsearch(hosts=[es_host])
q = Q('bool',
must[Q('match', data__uniqueInfo__md5_of_my_unique_info=md5_value_I_want_Input)],
)
s = Search().using(es).query(q)
response = s.execute
for hit in s:
print(hit.meta.id)
我已经在一堆数据 (15) 上对其进行了测试,它似乎可以工作,但我无法在测试中对更多数据进行测试,所以有人可以告诉我我做对了吗?如果不是,我该怎么做?
提前感谢任何可以帮助我的人
我同意评论中 JotaGe 的观点 - 如果 md5_of_my_unique_info
是 keyword
类型就没问题(请参阅 [0] 了解如何在 dsl 中设置映射)。请注意,如果您没有对映射做任何事情,您应该有一个由 elasticsearch 自动为您创建的 keyword
子字段。
使用 term
查询作为 filter
也会让你的性能稍微好一点,因为 elasticsearch 不必尝试计算分数,这对你的情况不重要,而且你不需要必须将您的查询包装在 bool
查询中。
总体而言,您的代码如下所示:
es_host = {"host": "localhost", "port": 9200}
es = Elasticsearch(hosts=[es_host])
s = Search().using(es)
s = s.filter('term', data__uniqueInfo__md5_of_my_unique_info__keyword=md5_value_I_want_Input)
response = s.execute
for hit in s:
print(hit.meta.id)
希望对您有所帮助!
0 - http://elasticsearch-dsl.readthedocs.io/en/latest/persistence.html#doctype
我的映射是这样的:
"md5_of_my_unique_info":{
"type": "text",
"fields":{
"keyword":{
"type": "keyword",
"ignore_above": 256
}
}
}
因为我需要自动生成我的映射(我有 json 大约 4000 行)
非常感谢您的回答:)
我是 Elasticsearch 的新手,elasticsearch_dsl 我遇到了一个问题,我想对 md5 字段进行研究,但我不知道我是否做对了。
这是我存储的文件:
"data": {
"uniqueInfo": {
"md5_of_my_unique_info": "a3e2c73ab0aaze73881db1a889826ada",
}
md5_of_my_unique_info 是一个很有价值的散列,我想研究一下它是否存在于数据库中,所以我这样做了:
es_host = {"host": "localhost", "port": 9200}
es = Elasticsearch(hosts=[es_host])
q = Q('bool',
must[Q('match', data__uniqueInfo__md5_of_my_unique_info=md5_value_I_want_Input)],
)
s = Search().using(es).query(q)
response = s.execute
for hit in s:
print(hit.meta.id)
我已经在一堆数据 (15) 上对其进行了测试,它似乎可以工作,但我无法在测试中对更多数据进行测试,所以有人可以告诉我我做对了吗?如果不是,我该怎么做?
提前感谢任何可以帮助我的人
我同意评论中 JotaGe 的观点 - 如果 md5_of_my_unique_info
是 keyword
类型就没问题(请参阅 [0] 了解如何在 dsl 中设置映射)。请注意,如果您没有对映射做任何事情,您应该有一个由 elasticsearch 自动为您创建的 keyword
子字段。
使用 term
查询作为 filter
也会让你的性能稍微好一点,因为 elasticsearch 不必尝试计算分数,这对你的情况不重要,而且你不需要必须将您的查询包装在 bool
查询中。
总体而言,您的代码如下所示:
es_host = {"host": "localhost", "port": 9200}
es = Elasticsearch(hosts=[es_host])
s = Search().using(es)
s = s.filter('term', data__uniqueInfo__md5_of_my_unique_info__keyword=md5_value_I_want_Input)
response = s.execute
for hit in s:
print(hit.meta.id)
希望对您有所帮助!
0 - http://elasticsearch-dsl.readthedocs.io/en/latest/persistence.html#doctype
我的映射是这样的:
"md5_of_my_unique_info":{
"type": "text",
"fields":{
"keyword":{
"type": "keyword",
"ignore_above": 256
}
}
}
因为我需要自动生成我的映射(我有 json 大约 4000 行)
非常感谢您的回答:)