一.实体
@Document(type = "conversation_content", indexName = "conversation_a",createIndex = false) public class ConversationContent { @Id @Field(type = FieldType.keyword) private Long id; @Field(analyzer = "ik_max_word", searchAnalyzer = "ik_max_word", store = true, type = FieldType.text) private String title; @Field(analyzer = "ik_max_word", searchAnalyzer = "ik_max_word", store = true, type = FieldType.text) private String initiator; @Field(type = FieldType.Nested)//声明嵌套修饰 private List<Item> itemList;
@Id声明主键(在嵌套那成可以不用声明)
@Field中
index:(es底层基于lucene,默认为每个字段创建倒排索引,如果不想通过某个field的关键字来查询到文档就可以将其设 置为false)
type:指定es的解析类型
store:不管store为true或false都会被存到es的_sourch字段中,如果为false在返回结果中将查看不到这个字段的内容
analyzer:声明分词器;ik_smart(粗粒度);ik_max_word(细粒度)
searchAnalyzer:声明搜索关键字使用的分词器
二:常用查询
实现ElasticsearchRepository
public interface ConversiationContentRepository extends ElasticsearchRepository<ConversationContent, Long> { }
##BoolQueryBuilder 当多条件查询的时候可以用它来做拼接,它的should和must相当于mysql中的or和and
##termQueryBuilder 有参构造的参数一:字段名,参数二:值;等值查询,表示查询满足该字段的值的文档
##MatchPhraseQueryBuilder 有参构造的参数一:字段名,参数二:值;短语查询,将用户输入的关键字进行分词后再查询 (如果输入的关键字中有原字段中不存在的内容就会查询不到内容,如果顺序不对也查询不到)
##MatchQueryBuilder 有参构造的参数一:字段名,参数二:值 ;将用户输入的关键字进行分词然后再去查询
##TermsQueryBuilder 它与termQueryBuilder的区别是它的值可以为多个,当然在指定字段后也可以将一个集合作为一个值传入
##WildCardQueryBuild 有参构造的参数一:字段名,参数二:值 ;正则查询;相当于将输入的关键字去匹配,不对输入的关键字进行分词
文档的局部更新
@Async//异步完成 public void upDocument(String value, Long id) { UpdateRequest updateRequest = new UpdateRequest(); try { updateRequest .doc(XContentFactory.jsonBuilder() .startObject() .field("tag", value)//指定更新的字段和更新的值,可指定多个字段 .endObject()); } catch (IOException e) { logger.error(e.getMessage()); } UpdateQuery updateQuery = new UpdateQueryBuilder() .withIndexName("conversation_abcde").withType("conversation_content_abcde")//声明索引名和索引类型 .withClass(String.class) .withId(String.valueOf(id))//声明文档id .withUpdateRequest(updateRequest) .build(); elasticsearchTemplate.update(updateQuery); elasticsearchTemplate.refresh("conversation_abcde");//完成更新后刷新文档