【问题标题】:ElasticSearch pagination through pyes. Offset ignored通过 pyes 进行 ElasticSearch 分页。忽略偏移
【发布时间】:2012-01-28 16:20:17
【问题描述】:

我正在研究 pyes 用法示例 here

我正在用四个文档索引 test-index 并稍后使用不同的偏移量进行查询。 start 参数不会改变我的偏移量,无论它的值如何,我都会得到相同的结果。为什么会这样?

from pyes import *
conn = ES(["localhost:9200"])
try:
    conn.delete_index('test-index') 
except:
    pass

conn.create_index('test-index')

mapping = {u'name': {'boost': 1.0,
                 'index': 'analyzed',
                 'store': 'yes',
                 'type': u'string',
                 "term_vector" : "with_positions_offsets"},
       u'title': {'boost': 1.0,
                 'index': 'analyzed',
                 'store': 'yes',
                 'type': u'string',
                 "term_vector" : "with_positions_offsets"},
       u'pos': {'store': 'yes',
                 'type': u'integer'},
       u'uuid': {'boost': 1.0,
                'index': 'not_analyzed',
                'store': 'yes',
                'type': u'string'}}

conn.put_mapping("test-type", {'properties':mapping}, ["test-index"])

conn.index({"name":"Joe Tester", "uuid":"11111", "position":1}, "test-index", "test-type", 1)
conn.index({"name":"Bill Baloney", "uuid":"22222", "position":2}, "test-index", "test-type", 2)
conn.index({"name":"Joe Joseph", "uuid":"33333", "position":3}, "test-index", "test-type", 3)
conn.index({"name":"Last Joe", "uuid":"44444", "position":4}, "test-index", "test-type", 4)

conn.refresh(["test-index"])

q = TermQuery("name", "joe")
r0 = conn.search(q, indices = ["test-index"], start=0, size=1)
r1 = conn.search(q, indices = ["test-index"], start=1, size=1)
r2 = conn.search(q, indices = ["test-index"], start=2, size=1)

print('0: {0}'.format(r0['hits']['hits']))
print('1: {0}'.format(r1['hits']['hits']))
print('2: {0}'.format(r2['hits']['hits']))

输出:

$ python pagination.py 
0: [{u'_score': 0.19178301, u'_type': u'test-type', u'_id': u'4', u'_source': {u'position': 4, u'name': u'Last Joe', u'uuid': u'44444'}, u'_index': u'test-index'}]
1: [{u'_score': 0.19178301, u'_type': u'test-type', u'_id': u'4', u'_source': {u'position': 4, u'name': u'Last Joe', u'uuid': u'44444'}, u'_index': u'test-index'}]
2: [{u'_score': 0.19178301, u'_type': u'test-type', u'_id': u'4', u'_source': {u'position': 4, u'name': u'Last Joe', u'uuid': u'44444'}, u'_index': u'test-index'}]

我的pyes版本是0.16.0

【问题讨论】:

    标签: python pagination elasticsearch


    【解决方案1】:

    问题在于请求是如何发送到 ES 的,尽管我仍然不清楚它失败的原因。

    而不是像我最初那样将查询直接发送到 ES:

    r0 = conn.search(q, indexes = ["test-index"], start=0, size=1)
    r1 = conn.search(q, indexes = ["test-index"], start=1, size=1)
    r2 = conn.search(q, indexes = ["test-index"], start=2, size=1)
    

    我将查询包装在 pyes.query.Search 对象中:

    r0 = conn.search(Search(q, start=0, size=1), indexes = ["test-index"])
    r1 = conn.search(Search(q, start=1, size=1), indexes = ["test-index"])
    r2 = conn.search(Search(q, start=2, size=1), indexes = ["test-index"])
    

    成功了,请看下面的输出:

    0s: [{u'_score': 0.19178301, u'_type': u'test-type', u'_id': u'4', u'_source': {u'position': 4, u'name': u'Last Joe', u'uuid': u'44444'}, u'_index': u'test-index'}]
    1s: [{u'_score': 0.19178301, u'_type': u'test-type', u'_id': u'1', u'_source': {u'position': 1, u'name': u'Joe Tester', u'uuid': u'11111'}, u'_index': u'test-index'}]
    2s: [{u'_score': 0.19178301, u'_type': u'test-type', u'_id': u'3', u'_source': {u'position': 3, u'name': u'Joe Joseph', u'uuid': u'33333'}, u'_index': u'test-index'}]
    

    【讨论】:

    • 当弹性搜索解析搜索时,我遇到了问题并获得了无查询注册异常。您在哪里使用什么版本的 pyes?
    • “indexes”参数令人困惑,因为 pyes 测试用例中使用了“indices”。 'indices' 被忽略并搜索所有索引。如果它们具有相同的映射,或者您只有一个可以正常使用的索引,否则,您将看到一个错误。
    【解决方案2】:

    确保您使用的搜索类型为Query then Fetch

    【讨论】:

    • 是的,很容易混合QUERY_AND_FETCHQUERY_THEN_FETCH
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多