SpringBoot Elastisearch RestHighLevelClient Java Search-API

时间:2018-02-10 18:05:01

标签: java spring spring-boot elasticsearch rest-client

我跟着这个Tutorial,一切正常。 除了我想添加一些额外的搜索功能。

我有以下文件是我的Elasticsearch 6.1索引:

{
    "author": "georges",
    "price": 99.1,
    "id": "06e68109-504a-44d6-bf2e-0debb12c984d",
    "title": "Java Always"
}

My Spring Boot应用程序在端口8080上运行。我知道如何使用postman:127.0.0.1:8080/books使用以下API来插入数据,以及如何通过127.0.0.1:8080/books/06e68109-504a-44d6-bf2e-0debb12c984d的书籍> ES Java High Level Rest API提供的GET请求:

    //This works perfectly thank you

@Repository
public class BookDao {
 private final String INDEX = "bookdata";
 private final String TYPE = "books";
 private RestHighLevelClient restHighLevelClient;

    ...

 public Map<String, Object> getBookById(String id) {
 GetRequest getRequest = new GetRequest(INDEX, TYPE, id);
 GetResponse getResponse = null;
 try {
      getResponse = restHighLevelClient.get(getRequest);
      } catch (java.io.IOException e) {
       e.getLocalizedMessage();
       }
       Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
                    return sourceAsMap;
       }

问题是:如何通过作者搜索本书?

我尝试了完全相同的实现,但它不起作用,因为GetRequest只能通过文档ID

获取文档
public Map<String, Object> getBookByAuthor(String author) throws IOException {

        GetRequest getRequest = new GetRequest(INDEX, TYPE, author);
        GetResponse getResponse = null;
        try {
            getResponse = restHighLevelClient.get(getRequest);
        } catch (java.io.IOException e) {
            e.getLocalizedMessage();
        }
        Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();

        return sourceAsMap;
    }

我的控制器:

import java.util.Map;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gvh.es.rest.es6rest.dao.BookDao;
import com.gvh.es.rest.es6rest.model.Book;

@RestController
@RequestMapping("/books")
public class BookController {

    private BookDao bookDao;

    public BookController(BookDao bookDao) {
        this.bookDao = bookDao;

    }



    @GetMapping("/{id}")
    public Map<String, Object> getBookById(@PathVariable String id){

      return bookDao.getBookById(id);
    }


    @PostMapping
    public Book insertBook(@RequestBody Book book) throws Exception {
      return bookDao.insertBook(book);
    }

    @PutMapping("/{id}")
    public Map<String, Object> updateBookById(@RequestBody Book book, @PathVariable String id) {
      return bookDao.updateBookById(id, book);
    }

    @DeleteMapping("/{id}")
    public void deleteBookById(@PathVariable String id) {
      bookDao.deleteBookById(id);
    }
}

1 个答案:

答案 0 :(得分:0)

我也遇到了问题,这是我解决问题的方法。它适用于我的代码。 更改用户名密码,我认为它可能会有效。

private RestHighLevelClient buildClient() {
    try {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("username", "password"));
        RestClientBuilder builder = RestClient.builder(
            new HttpHost("localhost", 9200, "http")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            }
        });
        restHighLevelClient = new RestHighLevelClient(builder);
    } catch (Exception e) {
        logger.error(e.getMessage());
    }
    return restHighLevelClient;
}