如何在Java中向Elasticsearch索引文档中插入唯一数据

huangapple go评论76阅读模式
英文:

how to insert unique data in Elasticsearch index document in java

问题

这是我的先前问题 - https://stackoverflow.com/q/63918143/13504695

索引映射如下

{
"test" : {
"mappings" : {
"properties" : {
"name" : {
"type" : "keyword"
},
"info" : {
"type" : "nested"
},
"joining" : {
"type" : "date"
}
}
}
}

在将数据上传到索引之前,如何检查字段的数据是否已经存在。

注意:索引中没有维护id字段。需要在每个文档中检查名称是否已经存在,如果已经存在,则不要将文档插入到索引中。

提前感谢您的帮助。

英文:

this is my previous question - https://stackoverflow.com/q/63918143/13504695

index mapping is as follows

{
 "test" : {
   "mappings" : {
     "properties" : {
        "name" : {
          "type" : "keyword"
        },
        "info" : {
          "type" : "nested"
        },
        "joining" : {
           "type" : "date"
        }
    }
}

how can i check the data of field is already present or not before uploading a data to the index

Note :- I dont have id field maintained in index. need to check name in each document if it is already present then dont insert document into index

thanks in advance

答案1

得分: 1

因为你的映射中没有 id 字段,所以你需要在 name 字段上进行搜索,你可以使用下面的代码来进行搜索:

public List<SearchResult> search(String searchTerm) throws IOException {
    SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    MatchQueryBuilder multiMatchQueryBuilder = new 
        MatchQueryBuilder(searchTerm, "firstName");
    searchSourceBuilder.query(matchQueryBuilder);
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse = esclient.search(searchRequest, RequestOptions.DEFAULT);
    return getSearchResults(searchResponse);
}

注意,由于你使用的是 keyword 字段而不是 match,你可以使用 termquerybuilder

而且它使用了一个 utility 方法来解析 ES 的 searchResponse,其代码如下:

private List<yourpojo> getSearchResults(SearchResponse searchResponse) {
    RestStatus status = searchResponse.status();
    TimeValue took = searchResponse.getTook();
    Boolean terminatedEarly = searchResponse.isTerminatedEarly();
    boolean timedOut = searchResponse.isTimedOut();

    // 开始获取与搜索结果匹配的文档。
    // https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search
    // .html#java-rest-high-search-response-search-hits
    SearchHits hits = searchResponse.getHits();
    SearchHit[] searchHits = hits.getHits();
    List<sr> sr = new ArrayList<>();
    for (SearchHit hit : searchHits) {
        // 对 SearchHit 进行操作
        String index = hit.getIndex();
        String id = hit.getId();
        float score = hit.getScore();

        //String sourceAsString = hit.getSourceAsString();
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        String firstName = (String) sourceAsMap.get("firstName");

        sr.add(userSearchResultBuilder.build());
    }
}
英文:

As you don't have a id field in your mapping, you have to search on name field and you can use below code to search on it.

public List&lt;SearchResult&gt; search(String searchTerm) throws IOException {
		SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		MatchQueryBuilder multiMatchQueryBuilder = new 
        MatchQueryBuilder(searchTerm, &quot;firstName&quot;);
		searchSourceBuilder.query(matchQueryBuilder);
		searchRequest.source(searchSourceBuilder);
		SearchResponse searchResponse = esclient.search(searchRequest, RequestOptions.DEFAULT);
		return getSearchResults(searchResponse);

	}

Note, as you have keyword field instead of match you can use termquerybuilder

And it uses the utility method to parse the searchResponse of ES, code of which is below:

private List&lt;yourpojo&gt; getSearchResults(SearchResponse searchResponse) {
		RestStatus status = searchResponse.status();
		TimeValue took = searchResponse.getTook();
		Boolean terminatedEarly = searchResponse.isTerminatedEarly();
		boolean timedOut = searchResponse.isTimedOut();

		// Start fetching the documents matching the search results.
		//https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search
		// .html#java-rest-high-search-response-search-hits
		SearchHits hits = searchResponse.getHits();
		SearchHit[] searchHits = hits.getHits();
		List&lt;sr&gt; sr = new ArrayList&lt;&gt;();
		for (SearchHit hit : searchHits) {
			// do something with the SearchHit
			String index = hit.getIndex();
			String id = hit.getId();
			float score = hit.getScore();

			//String sourceAsString = hit.getSourceAsString();
			Map&lt;String, Object&gt; sourceAsMap = hit.getSourceAsMap();
			String firstName = (String) sourceAsMap.get(&quot;firstName&quot;);
		    
			sr.add(userSearchResultBuilder.build());
		}

huangapple
  • 本文由 发表于 2020年9月16日 21:54:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63921602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定