当前位置: 首页 > news >正文

做一下网站需要什么时候开始湖北seo关键词排名优化软件

做一下网站需要什么时候开始,湖北seo关键词排名优化软件,临沂做网站推广的公司,武汉做公司网站的公司Spring Data访问Elasticsearch----其他Elasticsearch操作支持 一、索引设置二、索引映射三、Filter Builder四、为大结果集使用滚动Scroll五、排序选项六、运行时字段6.1 索引映射中的运行时字段定义6.2 在查询上设置的运行时字段定义 七、Point In Time (PIT) API八、搜索模板…

Spring Data访问Elasticsearch----其他Elasticsearch操作支持

  • 一、索引设置
  • 二、索引映射
  • 三、Filter Builder
  • 四、为大结果集使用滚动Scroll
  • 五、排序选项
  • 六、运行时字段
    • 6.1 索引映射中的运行时字段定义
    • 6.2 在查询上设置的运行时字段定义
  • 七、Point In Time (PIT) API
  • 八、搜索模板(Template)支持
  • 九、嵌套排序Nested sort

本文介绍了对Elasticsearch操作的额外支持,这些操作无法通过存储库接口直接访问。建议将这些操作添加为自定义实现,如“ 自定义存储库实现”中所述。

一、索引设置

当使用Spring Data创建Elasticsearch索引时,可以使用@Setting注解定义不同的索引设置。以下参数可用:

  • useServerConfiguration 不发送任何设置参数,因此由Elasticsearch服务器配置决定。
  • settingPath指的是一个JSON文件,该文件定义了必须在类路径中解析的设置
  • shards要使用的分片数,默认为1
  • replicas复制副本的数量,默认为1
  • refreshIntervall,默认为“1s”
  • indexStoreType,默认为“fs”

也可以定义索引排序(查看链接的Elasticsearch文档中可能的字段类型和值):

@Document(indexName = "entities")
@Setting(sortFields = { "secondField", "firstField" },               --------1                   sortModes = { Setting.SortMode.max, Setting.SortMode.min }, --------2                   sortOrders = { Setting.SortOrder.desc, Setting.SortOrder.asc },sortMissingValues = { Setting.SortMissing._last, Setting.SortMissing._first })
class Entity {@Nullable@Id private String id;@Nullable@Field(name = "first_field", type = FieldType.Keyword)private String firstField;@Nullable @Field(name = "second_field", type = FieldType.Keyword)private String secondField;// getter and setter...
}1. 定义排序字段时,请使用Java属性的名称(firstField),而不是可能为Elasticsearch定义的名称(first_field)
2. sortModes、sortOrders和sortMissingValues是可选的,但如果设置了它们,则条目的数量必须与sortFields元素的数量相匹配

二、索引映射

当Spring Data Elasticsearch使用IndexOperations.createMapping()方法创建索引映射时,它会使用Mapping Annotation Overview中描述的注解,尤其是@Field注解。除此之外,还可以将@Mapping注解添加到类中。此注解具有以下属性:

  • mappingPath JSON格式的类路径资源;如果这不是空的,它将用作映射,则不进行其他映射处理。
  • enabled 当设置为false时启用,该标志将被写入映射,并且不进行进一步处理。
  • dateDetection和numericDetection在未设置为DEFAULT时设置映射中的相应属性。
  • dynamicDateFormats当此字符串数组不为空时,它定义用于自动日期检测的日期格式。
  • runtimeFieldsPath JSON格式的类路径资源,包含写入索引映射的运行时字段的定义,例如:
{"day_of_week": {"type": "keyword","script": {"source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"}}
}

三、Filter Builder

Filter Builder提高查询速度。

private ElasticsearchOperations operations;IndexCoordinates index = IndexCoordinates.of("sample-index");Query query = NativeQuery.builder().withQuery(q -> q.matchAll(ma -> ma)).withFilter( q -> q.bool(b -> b.must(m -> m.term(t -> t.field("id").value(documentId))))).build();SearchHits<SampleEntity> sampleEntities = operations.search(query, SampleEntity.class, index);

四、为大结果集使用滚动Scroll

Elasticsearch有一个滚动API,用于获取大块的结果集。Spring Data Elasticsearch内部使用它来提供<T> SearchHitsIterator<T> SearchOperations.searchForStream(Query query, Class<T> clazz, IndexCoordinates index)方法的实现。

IndexCoordinates index = IndexCoordinates.of("sample-index");Query searchQuery = NativeQuery.builder().withQuery(q -> q.matchAll(ma -> ma)).withFields("message").withPageable(PageRequest.of(0, 10)).build();SearchHitsIterator<SampleEntity> stream = elasticsearchOperations.searchForStream(searchQuery, SampleEntity.class,
index);List<SampleEntity> sampleEntities = new ArrayList<>();
while (stream.hasNext()) {sampleEntities.add(stream.next());
}stream.close();

在SearchOperations API中没有方法来访问滚动id,如果有必要访问它,可以使用AbstractElasticsearchTemplate的以下方法(这是不同ElasticsearchOperations实现的基础实现):

@Autowired ElasticsearchOperations operations;AbstractElasticsearchTemplate template = (AbstractElasticsearchTemplate)operations;IndexCoordinates index = IndexCoordinates.of("sample-index");Query query = NativeQuery.builder().withQuery(q -> q.matchAll(ma -> ma)).withFields("message").withPageable(PageRequest.of(0, 10)).build();SearchScrollHits<SampleEntity> scroll = template.searchScrollStart(1000, query, SampleEntity.class, index);String scrollId = scroll.getScrollId();
List<SampleEntity> sampleEntities = new ArrayList<>();
while (scroll.hasSearchHits()) {sampleEntities.addAll(scroll.getSearchHits());scrollId = scroll.getScrollId();scroll = template.searchScrollContinue(scrollId, 1000, SampleEntity.class);
}
template.searchScrollClear(scrollId);

要将Scroll API与存储库方法一起使用,返回类型必须在Elasticsearch存储库中定义为Stream。然后,该方法的实现将使用ElasticsearchTemplate中的scroll方法。

interface SampleEntityRepository extends Repository<SampleEntity, String> {Stream<SampleEntity> findBy();}

五、排序选项

除了分页和排序中描述的默认排序选项外,Spring Data Elasticsearch还提供了从“org.springframework.Data.domain.sort.Order”派生而来的类“org.springframework.Data.reasticsearch.core.query”。它提供了额外的参数,在指定结果排序时可以将这些参数发送到Elasticsearch(请参见这里)。
还有“org.springframework.data.aelasticsearch.core.query.GeoDistanceOrder”类,可用于按地理距离排序搜索操作的结果。
如果要检索的类具有名为location的GeoPoint属性,则以下排序将按到给定点的距离对结果进行排序:

Sort.by(new GeoDistanceOrder("location", new GeoPoint(48.137154, 11.5761247)))

六、运行时字段

从7.12版本开始,Elasticsearch增加了运行时字段的功能。Spring Data Elasticsearch通过两种方式支持这一点:

6.1 索引映射中的运行时字段定义

定义运行时字段的第一种方法是将定义添加到索引映射中(请参见这里)。要在Spring Data Elasticsearch中使用这种方法,用户必须提供一个包含相应定义的JSON文件,例如:
例1:runtime-fields.json

{"day_of_week": {"type": "keyword","script": {"source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"}}
}

这个JSON文件的路径必须出现在类路径中,然后必须在实体的@Mapping注解中设置:

@Document(indexName = "runtime-fields")
@Mapping(runtimeFieldsPath = "/runtime-fields.json")
public class RuntimeFieldEntity {// properties, getter, setter,...
}

6.2 在查询上设置的运行时字段定义

定义运行时字段的第二种方法是将定义添加到搜索查询中(请参见这里)。以下代码示例展示了如何使用Spring Data Elasticsearch进行此操作:
所使用的实体是一个具有price属性的简单对象:

@Document(indexName = "some_index_name")
public class SomethingToBuy {private @Id @Nullable String id;@Nullable @Field(type = FieldType.Text) private String description;@Nullable @Field(type = FieldType.Double) private Double price;// getter and setter
}

下面的查询使用一个运行时字段,该字段通过将价格添加19%来计算priceWithTax值,并在搜索查询中使用该值来查找priceWithTax高于或等于给定值的所有实体:

RuntimeField runtimeField = new RuntimeField("priceWithTax", "double", "emit(doc['price'].value * 1.19)");
Query query = new CriteriaQuery(new Criteria("priceWithTax").greaterThanEqual(16.5));
query.addRuntimeField(runtimeField);SearchHits<SomethingToBuy> searchHits = operations.search(query, SomethingToBuy.class);

这适用于Query接口的每一个实现。

七、Point In Time (PIT) API

ElasticsearchOperations支持Elasticsearch的point in time API(请参见这里)。以下代码片段显示了如何将此功能与虚构的Person类一起使用:

ElasticsearchOperations operations; // autowired
Duration tenSeconds = Duration.ofSeconds(10);String pit = operations.openPointInTime(IndexCoordinates.of("person"), tenSeconds); --------1// create query for the pit
Query query1 = new CriteriaQueryBuilder(Criteria.where("lastName").is("Smith")).withPointInTime(new Query.PointInTime(pit, tenSeconds))                        --------2.build();
SearchHits<Person> searchHits1 = operations.search(query1, Person.class);
// do something with the data// create 2nd query for the pit, use the id returned in the previous result
Query query2 = new CriteriaQueryBuilder(Criteria.where("lastName").is("Miller")).withPointInTime(new Query.PointInTime(searchHits1.getPointInTimeId(), tenSeconds))          --------3 .build();
SearchHits<Person> searchHits2 = operations.search(query2, Person.class);
// do something with the dataoperations.closePointInTime(searchHits2.getPointInTimeId());                        --------41. 为索引(可以是多个名称)和keep-alive持续时间创建一个point in time,并检索其id
2. 将该id传递到查询中,以便与下一个keep-alive一起进行搜索
3. 对于下一个查询,使用上一次搜索返回的id
4. 完成后,使用最后返回的id关闭point in time

八、搜索模板(Template)支持

支持使用搜索模板Template API。要使用它,首先需要创建一个存储的脚本。ElasticsearchOperations接口扩展了提供必要功能的ScriptOperations。这里使用的示例假设我们有一个名为firstName的属性的Person实体。搜索模板脚本可以这样保存:

operations.putScript(                          --------1  Script.builder().withId("person-firstname")                --------2  .withLanguage("mustache")                  --------3  .withSource("""                            --------4  {"query": {"bool": {"must": [{"match": {"firstName": "{{firstName}}" --------5  }}]}},"from": "{{from}}",                    --------6  "size": "{{size}}"                     --------7  }""").build()
);1. 使用putScript()方法存储搜索模板脚本
2. 脚本的名称/id
3. 搜索模板中使用的脚本必须是mustache语言。
4. 脚本源
5. 脚本中的搜索参数
6. 分页请求偏移
7. 分页请求大小

为了在搜索查询中使用搜索模板,Spring Data Elasticsearch提供了SearchTemplateQuery,这是“org.springframework.data.elasticsearch.core.query.Query”接口的实现。
在下面的代码中,我们将使用搜索模板查询向自定义存储库实现添加一个调用(请参阅自定义存储库实现),作为如何将其集成到存储库调用中的示例。
我们首先定义自定义存储库片段接口:

interface PersonCustomRepository {SearchPage<Person> findByFirstNameWithSearchTemplate(String firstName, Pageable pageable);
}

这个存储库片段的实现看起来像这样:

public class PersonCustomRepositoryImpl implements PersonCustomRepository {private final ElasticsearchOperations operations;public PersonCustomRepositoryImpl(ElasticsearchOperations operations) {this.operations = operations;}@Overridepublic SearchPage<Person> findByFirstNameWithSearchTemplate(String firstName, Pageable pageable) {var query = SearchTemplateQuery.builder()                               --------1.withId("person-firstname")                                           --------2.withParams(Map.of(                                                             --------3"firstName", firstName,"from", pageable.getOffset(),"size", pageable.getPageSize())).build();SearchHits<Person> searchHits = operations.search(query, Person.class); --------4return SearchHitSupport.searchPageFor(searchHits, pageable);}
}1. 创建SearchTemplateQuery
2. 提供搜索模板的id
3. 参数在Map<String,Object>中传递
4. 以与其他查询类型相同的方式进行搜索。

九、嵌套排序Nested sort

Spring Data Elasticsearch支持在嵌套对象内排序(见这里)下面的例子,摘自org.springframework.data.elasticsearch.core.query.sort.NestedSortIntegrationTests类,展示了如何定义嵌套排序。

var filter = StringQuery.builder("""{ "term": {"movies.actors.sex": "m"} }""").build();
var order = new org.springframework.data.elasticsearch.core.query.Order(Sort.Direction.DESC,"movies.actors.yearOfBirth").withNested(Nested.builder("movies").withNested(Nested.builder("movies.actors").withFilter(filter).build()).build());var query = Query.findAll().addSort(Sort.by(order));

关于filter查询:这里不可能使用CriteriaQuery,因为此查询将被转换为Elasticsearch嵌套查询,该查询在filter上下文中不起作用。所以这里只能使用StringQuery或NativeQuery。当使用其中一个时,如上面的term查询,必须使用Elasticsearch字段名,所以当使用@Field(name=“…​”)定义重新定义这些字段时要小心。
对于排序路径和嵌套路径的定义,应使用Java实体属性名称。

http://www.dinnco.com/news/55426.html

相关文章:

  • 全能网站服务器神马seo教程
  • 自己做的网站百度搜不到投放广告
  • 门户网站开发 南宁线上营销怎么做
  • 中天建设集团有限公司重庆分公司c盘优化大师
  • 中国建设银行企业信息门户网站百度在线使用
  • 旅游网站项目计划书企业网站系统
  • 西地那非能延时吗网站seo顾问
  • 做数模必逛的网站宁波seo网络推广产品服务
  • 如何使用服务器ip做网站网络营销有哪些内容
  • 电子商务有限公司经营范围有哪些福州seo技术培训
  • 虚拟机如何做网站百度关键词查询排名怎么查
  • 良品铺子网站制作企业培训机构哪家最好
  • 北京高端品牌网站建设搜索关键词排名
  • 外贸圈的尖子生蜗牛精灵seo
  • wordpress date_queryseo优化收费
  • 九度互联网站建设百度网站下载安装
  • 域名申请好后 如何建设网站百度关键词热度排名
  • 高德地图无目的地导航徐州seo推广优化
  • 龙岗商城网站建设最好电商网站开发
  • 做豆腐交流经验的网站seo网站分析报告
  • 有哪些做任务网站推广一般去哪发帖
  • 网站怎么做是满屏sem网站推广怎么做
  • 网站建设服务商济南百度竞价
  • 域名申请了怎么用苏州首页关键词优化
  • 网站建设是指自动点击器免费下载
  • 吉林省软环境建设办公室网站今日头条新闻最新疫情
  • 代网站备案费用网店培训教程
  • 上海注册公司代办机构哪家好seo与sem的区别和联系
  • 网站制作建设飞沐百度关键词工具
  • 上海网站推广费用互联网广告投放