【问题标题】:Search returns document after delete删除后搜索返回文档
【发布时间】:2016-11-05 02:21:09
【问题描述】:

我有一个奇怪的效果,即对索引的搜索会返回我之前刚刚删除的文档。 “get”可以正常工作。难道我做错了什么?搜索没有限制(client.prepareSearch("test").execute(...))

我正在使用 Elastic Search 5.0 运行“ESIntegTestCase”

@Test
public void testES() throws Exception {

    String index = "test";
    String type = "event";
    String doc = "{\"Key0\":\"Val0\"}";

    createIndex(index);

    Semaphore sem = new Semaphore(0);

    client().prepareIndex(index, type).setSource(doc).execute(handleOrError(postResp -> {
        client().prepareGet(postResp.getIndex(), postResp.getType(), postResp.getId()).execute(handleOrError(getResp -> {
            printGR(getResp);
            client().prepareSearch(postResp.getIndex()).execute(handleOrError(searchResponse -> {
                printSR(searchResponse);
                client().prepareDelete(postResp.getIndex(), postResp.getType(), postResp.getId()).execute(handleOrError(resp -> {
                    printDR(resp);
                    client().prepareGet(postResp.getIndex(), postResp.getType(), postResp.getId()).execute(handleOrError(getResp2 -> {
                        printGR(getResp2);
                        client().prepareSearch(postResp.getIndex()).execute(handleOrError(searchResponse2 -> {
                            printSR(searchResponse2);
                            sem.release();
                        }));
                    }));
                }));
            }));
        }));
    }));

    sem.acquire();
}

打印:

1) GetResponse: {"_index":"test","_type":"events","_id":"AVgv1NHPHZ0vJaA-eRhJ","_version":1,"found":true,"_source":{"Key0":"Val0"}}


2) SearchResponse:{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"test","_type":"events","_id":"AVgv1NHPHZ0vJaA-eRhJ","_score":1.0,"_source":{"Key0":"Val0"}}]}}


3) DeleteResponse: DeleteResponse[index=test,type=events,id=AVgv1NHPHZ0vJaA-eRhJ,version=2,result=deleted,shards="_shards"{"total":2,"successful":2,"failed":0}]


4) GetResponse: {"_index":"test","_type":"events","_id":"AVgv1NHPHZ0vJaA-eRhJ","found":false}


5) SearchResponse:{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"test","_type":"events","_id":"AVgv1NHPHZ0vJaA-eRhJ","_score":1.0,"_source":{"Key0":"Val0"}}]}}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您发现了搜索索引和执行获取请求之间的区别。获取请求也使用事务日志。如果您希望删除对搜索产生影响,则需要执行刷新。使用 elastic 5,现在可以选择在插入或删除后等待刷新。使用该功能应该可以满足您的需求。更多信息可以在这里找到: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-refresh.html

    【讨论】:

    • 谢谢!更改删除的刷新策略解决了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 2016-06-08
    • 1970-01-01
    相关资源
    最近更新 更多