标签: spring-data-elasticsearch

从 Spring Data Elastic Search 4.0.0 的 SearchHits 类获取 InnerHits 结果

我目前正在从 spring data elastic search 3.2.X 迁移到 4.0.0。

我一直依赖于通过原始 SearchResponse 类获取内部命中结果,方法是利用现已弃用的低级函数org.springframework.data.elasticsearch.core.ElasticsearchOperations.query(SearchQuery query, ResultsExtractor resultsExtractor);

https://docs.spring.io/spring-data/elasticsearch/docs/4.0.0.RELEASE/reference/html/#elasticsearch-migration-guide-3.2-4.0

基本上我所做的是创建自定义 resultExtractor ,它将公开低级别的 SearchResponse ,以便我可以提取内部命中结果。

现在,在 4.0.0 版本中,该功能已不再存在,ElasticSearchOperations 和 ElasticSearchRestTemplate 都使用名为 SearchHits 的新结果类,据我所知,该类不存储内部点击信息。

除了直接使用restHighLevelClient(这当然是不可取的,因为我会丢失对象映射和其他东西)之外,我能想到的解决方法是扩展ElasticSearchRestTemplate并基本上创建新的搜索实现(查询查询、Class clazz、IndexCooperatives索引),其中将返回 SearchHits 和原始搜索响应(类似于我的自定义 ResultExtractor 用来执行的操作)。

之后,由于所有返回页面实例的 api 已被弃用,我现在必须依赖SearchHitSupport.searchPageFor(SearchHits searchHits, @Nullable Pageable pageable)来获取通常的分页功能。

我觉得这不是非常简单和干净的解决方案,而且感觉更像是一种解决方法。在 4.0.0 版本中是否有更直接的方法来获取 innerHitsResult 并将结果内容映射到页面?

java spring spring-data-elasticsearch

4
推荐指数
1
解决办法
6084
查看次数

Spring Data Elasticsearch 批量索引/删除 - 数百万条记录

我正在使用 Spring Data Elasticsearch 4.2.5,我们有一项工作对特定的数据库表执行 ETL(提取、转换和加载数据)。我在作业运行时使用 Elasticsearch 为这些数据建立索引。数据将达到数百万条甚至更多。目前,我正在对每次迭代进行索引。我读到,在每次迭代中使用 elasticsearch 索引可能需要一些时间。我想使用像bulk-index这样的东西,但为此我需要将indexQuery对象添加到List中。添加数百万条记录到列表并进行批量索引可能会带来内存问题。

我需要应用类似的删除过程。当根据一些常见的ID删除记录时,我需要删除相关的弹性文档,这也将是数百万甚至更多。

无论如何,是否可以针对此要求非常快速地进行索引/删除?非常感谢任何帮助,如果我的理解不正确,请纠正我。

索引

for (Map.Entry<Integer, ObjectDetails> key : objectDetailsHashMap.entrySet()) {
    indexDocument(elasticsearchOperations, key, oPath);
    // other code to insert data in db table...
 }

private void indexDocument(ElasticsearchOperations elasticsearchOperations,
                              Map.Entry<Integer, ObjectDetails> key, String oPath) {
    String docId = "" + key.getValue().getCatalogId() + key.getValue().getObjectId();

    byte[] nameBytes = key.getValue().getName();
    byte[] physicalNameBytes = key.getValue().getPhysicalName();
    byte[] definitionBytes =  key.getValue().getDefinition();
    byte[] commentBytes = key.getValue().getComment();

    IndexQuery indexQuery = new IndexQueryBuilder()
            .withId(docId)
            .withObject(new MetadataSearch(
                    key.getValue().getObjectId(),
                    key.getValue().getCatalogId(),
                    key.getValue().getParentId(),
                    key.getValue().getTypeCode(),
                    key.getValue().getStartVersion(), …
Run Code Online (Sandbox Code Playgroud)

java elasticsearch spring-data-elasticsearch

4
推荐指数
1
解决办法
3119
查看次数

如何使用Spring Data ElasticSearchTemplate保存对象

如何使用Spring Data保存实体ElasticSearchTemplate?在文档中找不到它。

spring elasticsearch spring-data spring-data-elasticsearch

3
推荐指数
1
解决办法
7177
查看次数

Spring中带有@Query的ElasticSearch

我已经使用ElasticSearch的_plugin / head接口成功创建了查询。该查询旨在返回特定位置的特定设备的最新时间戳。该查询如下所示:

{  
   "query":{  
      "bool":{  
         "must":[  
            {  
               "term":{  
                  "deviceevent.location.id":"1"
               }
            },
            {  
               "term":{  
                  "deviceevent.deviceId":"AHE1LDD01"
               }
            }
         ]
      }
   },
   "from":0,
   "size":1,
   "sort":{  
      "timestamp":{  
         "order":"desc"
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

上面的查询按预期工作。现在使用Spring-Boot和Spring-Data-ElasticSearch,我定义了自己的ElasticSearchRepository外观,如下所示:

package com.repository.elasticsearch;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import com.domain.DeviceEvent;

public interface DeviceEventRepository extends ElasticsearchRepository<DeviceEvent, String>
{
    @Query("{\"bool\":{\"must\":[{\"term\":{\"deviceevent.location.id\": \"?0\"}},{\"term\":{\"deviceevent.deviceId\": \"?1\"}}]}},\"from\": 0,\"size\": 1,\"sort\":{\"timestamp\":{\"order\":\"desc\"}}")
    DeviceEvent findLatestCheckInAtLocation(Long locationId, String deviceId);
}
Run Code Online (Sandbox Code Playgroud)

上面的代码之所以中断,主要是因为我希望它返回一个DeviceEvent,但实际上它返回的是带有count = 10(默认页面大小)的设备事件。似乎时间戳也没有按降序对结果进行排序。好像查询的sizeorder部分没有被拾取。

我在这里做错了什么?

elasticsearch elasticsearch-plugin spring-boot spring-data-elasticsearch

3
推荐指数
1
解决办法
3365
查看次数

Spring Data Elastic Search的聚合支持

弹性搜索已弃用Facets并建议使用Aggregations(http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html).

Spring Data Elastic Search目前是否支持此功能?

如果是,是否有可用的样品?

spring-data-elasticsearch

3
推荐指数
2
解决办法
8337
查看次数

是否可以使用Spring的注释为Elasticsearch中的映射定义Completion Suggester?

我目前有以下POJO.

@Document(indexName="ws",type="vid")
public class Vid {
    @Id 
    private String id;

    @Field(type=FieldType.String, index=FieldIndex.not_analyzed)
    private List<String> tags;
}
Run Code Online (Sandbox Code Playgroud)

表示此POJO的JSON如下所示.

{ 
    "id" : "someId",
    "tags" : [ "one", "two", "three" ]
}
Run Code Online (Sandbox Code Playgroud)

我想要的是定义tags字段的映射,以便我可以在自动完成搜索框中使用这些值.这得到了Elasticsearch的Completion Suggester的支持.https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html上的文档似乎向我建议我必须按如下方式设置映射.

{
    "vid": {
        "properties": {
            "id": {
                "type": "string"
            },
            "tags": {
                "type": "completion",
                "index_analyzer": "simple",
                "search_analyzer": "simple",
                "payloads": true
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这意味着我必须修改我的POJO和JSON表示.

{
    "id": "someId",
    "tags": {
        "input": [ "one", "two", "three" ]
    }
}
Run Code Online (Sandbox Code Playgroud)

我在Completions Suggesters这里找到了另一个好的页面http://blog.qbox.io/quick-and-dirty-autocomplete-with-elasticsearch-completion-suggest.但是,该页面似乎表明了冗余tags …

java spring elasticsearch spring-data-elasticsearch elasticsearch-mapping

3
推荐指数
1
解决办法
2859
查看次数

Spring Elasticsearch HashMap [String,String]映射值不能not_analyzed

其实我的问题很简单:我希望我的hashmap值not_analyzed!

现在我有一个对象包含一个hashmap [string,string],看起来像:

class SomeObject{
    String id;
    @Field(type=FieldType.Object, index=FieldIndex.not_analyzed)
    Map<String, String> parameters;
}
Run Code Online (Sandbox Code Playgroud)

然后Spring数据elasticsearch在开始时生成这样的映射:

{
    "id": {
      "type": "string"
    },
    "parameters": {
      "type": "object"
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在我向es添加一些对象之后,它会添加更多这样的属性:

{
    "id": {
      "type": "string"
    },
    "parameters": {
        "properties": {
             "shiduan": {
                "type": "string"
             },
             "??": {
                "type": "string"
             }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,由于参数的值被分析,所以不能用es搜索,我的意思是不能搜索中文值,我试过我此时可以搜索英文.

然后,在阅读本帖后/sf/answers/2243105931/,我手动更新了映射:

{
    "id": {
      "type": "string"
    },
    "parameters": {
        "properties": {
             "shiduan": {
                "type": "string",
                "index": "not_analyzed"
             },
             "??": {
                "type": "string",
                "index": "not_analyzed"
             }
        }
    } …
Run Code Online (Sandbox Code Playgroud)

java spring hashmap elasticsearch spring-data-elasticsearch

3
推荐指数
1
解决办法
1714
查看次数

兼容版本的Spring启动,弹性搜索和弹簧数据弹性搜索

我一直在使用spring boot,弹性搜索和spring数据弹性搜索开发一个简单的java编程.

我可以用以下版本成功进行弹性搜索1)spring boot 1.3.5 2)spring-data-elasticsearch 1.3.4.RELEASE 3)elasticsearch 1.3.2

但是当我升级到以下版本时,我的一个服务类用@service注释注释显示错误为

"类型org.elasticsearch.search.suggest.SuggestBuilder $ SuggestionBuilder无法解析.它是从所需的.class文件间接引用的".

     1) spring boot 1.5.1.RELEASE
     2) spring-data-elasticsearch 2.1.0.RELEASE
     3) elasticsearch 5.0.1
     4) org.elasticsearch.client.transport 5.1.1
Run Code Online (Sandbox Code Playgroud)

是不兼容的版本的问题?如果有,请帮助我什么是spring boot,elasticsearch和spring data elasticsearch的兼容版本.

elasticsearch spring-boot spring-data-elasticsearch

3
推荐指数
1
解决办法
5771
查看次数

将弹性搜索结果转换为POJO

我有一个使用spring-data-elasticsearch库的项目.我有我的系统返回结果,但我想知道如何以我的域POJO类的形式获得我的结果.

我没有看到太多关于如何实现这一目标的文档,但我不知道我应该使用Google搜索的正确问题.

目前,我的代码看起来像这样,在我的测试中,它检索正确的结果,但不是作为POJO.

    QueryBuilder matchQuery = QueryBuilders.queryStringQuery(searchTerm).defaultOperator(QueryStringQueryBuilder.Operator.AND);

    Client client = elasticsearchTemplate.getClient();

    SearchRequestBuilder request = client
            .prepareSearch("mediaitem")
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            .setQuery(matchQuery)
            .setFrom(0)
            .setSize(100)
            .addFields("title", "description", "department");
    System.out.println("SEARCH QUERY: " + request.toString());

    SearchResponse response = request.execute().actionGet();
    SearchHits searchHits = response.getHits();
    SearchHit[] hits = searchHits.getHits();
Run Code Online (Sandbox Code Playgroud)

任何帮助是极大的赞赏.

elasticsearch spring-boot spring-data-elasticsearch

3
推荐指数
1
解决办法
2778
查看次数

使用spring-data-elasticsearch从索引中获取所有文档

我正在尝试使用Spring Boot连接到我的外部ElasticSearch服务器。

如果我在命令行中进行卷曲,则可以得到预期的结果。

curl "http://ipAddr:9200/indexName/TYPE/_search?pretty=true"
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试通过Spring Boot访问它时出现此错误。

<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Mon Sep 11 12:39:15 IST 2017</div><div>There was an unexpected error (type=Internal Server Error, status=500).</div><div>Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]-&gt;org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl[&quot;facets&quot;])</div></body></html>
Run Code Online (Sandbox Code Playgroud)

不知道为什么NullPointerException和什么aggregartion.impl

这是我的春季申请:

控制器:

@RestController
public class PojoController {

    @Autowired
    PojoService pojoService;

    @RequestMapping(value = "/", method=RequestMethod.GET)
    public @ResponseBody String index() {
        return new String("Welcome:)"); …
Run Code Online (Sandbox Code Playgroud)

spring elasticsearch spring-data-elasticsearch

3
推荐指数
1
解决办法
2405
查看次数