java - 无法让 findAll 在 Elasticsearch 中工作

标签 java elasticsearch

我有以下代码在 Elasticsearch 中创建文档:

    public static final String INDEX = "profile";

    private RestHighLevelClient client;

    ...

    public DocWriteResponse.Result createProfileDocument(ProfileDocument document)
            throws Exception
    {
        UUID uuid = UUID.randomUUID();
        document.setId(uuid.toString());

        IndexRequest indexRequest = new IndexRequest(INDEX);
        indexRequest.id(document.getId());
        indexRequest.source(document, XContentType.JSON);
        indexRequest.opType("create");

        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);

        return indexResponse.getResult();
    }

我想对所有条目执行简单搜索。我有以下代码:

    public List<ProfileDocument> findAll()
            throws Exception
    {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());

        SearchRequest searchRequest = buildSearchRequest(INDEX);
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        return getSearchResult(searchResponse);
    }

    private List<ProfileDocument> getSearchResult(SearchResponse response)
    {
        SearchHit[] searchHit = response.getHits().getHits();

        List<ProfileDocument> profileDocuments = new ArrayList<>();

        for (SearchHit hit : searchHit)
        {
            profileDocuments.add(objectMapper.convertValue(hit.getSourceAsMap(), ProfileDocument.class));
        }

        return profileDocuments;
    }

    private SearchRequest buildSearchRequest(String index)
    {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(index);

        return searchRequest;
    }


这是ProfileDocument的代码:

public class ProfileDocument
{

    private String id;

    private String name;


    public ProfileDocument()
    {
    }

    public ProfileDocument(String id, String name)
    {
        this.id = id;
        this.name = name;
    }

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    @Override
    public String toString()
    {
        return id + ":" + name;
    }

}

当我执行时:

import ...

@SpringBootTest(classes = Application.class)
@ActiveProfiles(profiles = "test")
public class ProfileServiceTest
{

    @Inject
    private ProfileService profileService;


    @Test
    public void testCRUD()
            throws Exception
    {
        ProfileDocument document = new ProfileDocument("foo", "bar");

        DocWriteResponse.Result result = profileService.createProfileDocument(document);

        assertEquals(DocWriteResponse.Result.CREATED,
                     result,
                     "Failed to store the test document!");

        // This actually prints CREATED
        System.out.println();
        System.out.println();
        System.out.println(result.name());
        System.out.println();
        System.out.println();

        List<ProfileDocument> profileDocuments = profileService.findAll();

        assertNotNull(profileDocuments, "Failed to find any results!");
        // It fails here:
        assertFalse(profileDocuments.isEmpty(), "Failed to find any results!");

        for (ProfileDocument profileDocument : profileDocuments)
        {
            System.out.println(profileDocument.toString());
        }

        ProfileDocument byId = profileService.findById("foo");

        System.out.println(byId.toString());

        assertNotNull(byId, "Failed to find the document by an ID!");

        List<ProfileDocument> byNames = profileService.findProfileByName("bar");

        assertFalse(byNames.isEmpty(), "Failed to find the document by a name!");
    }

}

我不明白为什么它说它创建了文档,但是 findAll 部分不起作用,它返回一个空对象,如下所示:

00:48:33.344 26-09-2019 | INFO  | main                 | c.e.aws.elasticsearch.demo.ProfileServiceTest      | Starting ProfileServiceTest on carlspring with PID 6598 (started by carlspring in /java/opensource/examples/spring-boot-java-highlevel-rest-client-elasticsearch)
00:48:33.345 26-09-2019 | INFO  | main                 | c.e.aws.elasticsearch.demo.ProfileServiceTest      | The following profiles are active: test
00:48:35.485 26-09-2019 | INFO  | main                 | c.e.aws.elasticsearch.demo.ProfileServiceTest      | Started ProfileServiceTest in 3.09 seconds (JVM running for 4.065)
[2019-09-26T00:48:40,919][INFO ][o.e.c.m.MetaDataCreateIndexService] [carlspring] [profile] creating index, cause [auto(bulk api)], templates [], shards [1]/[1], mappings []
[2019-09-26T00:48:41,461][INFO ][o.e.c.m.MetaDataMappingService] [carlspring] [profile/f3Egu6jWR9iJyYkVS-uVxw] create_mapping [_doc]


CREATED


null:null
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 10.396 s <<< FAILURE! - in com.example.aws.elasticsearch.demo.ProfileServiceTest
[ERROR] testCRUD  Time elapsed: 6.873 s  <<< ERROR!
java.lang.NullPointerException
    at com.example.aws.elasticsearch.demo.ProfileServiceTest.testCRUD(ProfileServiceTest.java:54)

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   ProfileServiceTest.testCRUD:54 NullPointer
[INFO] 
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

我使用的是 Elasticsearch 7.3.1。

任何提示和帮助将不胜感激!

最佳答案

这是因为在您创建文档和搜索文档之间索引尚未刷新。

您需要修改 createProfileDocument() 中的代码,如下所示:

    IndexRequest indexRequest = new IndexRequest(INDEX);
    indexRequest.id(document.getId());
    indexRequest.source(document, XContentType.JSON);
    indexRequest.opType("create");

    // add this line
    indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);

    // assert: at this point the document will be searchable

关于java - 无法让 findAll 在 Elasticsearch 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58089144/

相关文章:

elasticsearch - 类似于 ElasticSearch 中的 "Materialized view"

java - Mockito 监视被单元测试的对象

java - 调用数组并返回不同数组的方法会给出无法解析 B 到变量错误

java - 创建用于收集数据的搜索插件

string - 在 Elasticsearch 中如何映射由字符分隔的文本?

Elasticsearch 错误无法连接到主机 - 没有到主机的路由

java - java中的继承 : object state and behavior

java - 使用 Java 将字符串数组写入文件 - 单独的行

elasticsearch - 指定从 Logstash 到 Elasticsearch 的字段类型索引

full-text-search - Elasticsearch:是否可以编写区分大小写的查询?