spring-boot集成elasticsearch

version

java1.8 elasticsearch6.8 spring boot2.1.4 ik6.8

pom
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

 

Start abnormal

nested exception is java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]

Insert the following start code is based

static {
       System.setProperty("es.set.netty.runtime.available.processors", "false");
    }
dao

dao inherited ElasticsearchRepository 

 

 

 Add package scanning startup class

@EnableElasticsearchRepositories(basePackages = {"com.aipinto.ams.esdao"})
Create an index mapping

Used in the entity class

Package com.aipinto.ams.dmo.elasticsearch; 

Import org.elasticsearch.common.geo.GeoPoint;
 Import org.springframework.data.annotation.Id;
 Import org.springframework.data.elasticsearch.annotations.Document;
 Import org.springframework .data.elasticsearch.annotations.Field;
 Import org.springframework.data.elasticsearch.annotations.FieldType;
 Import org.springframework.data.elasticsearch.annotations.GeoPointField; 

Import java.util.List; 

// index database names do not use uppercase Do not type easily play with a capital 
@Document (indexName = "Hotel", of the type = "docs" )
 public  class{ElasticsearchHotelDmo
     
    @Id // corresponding to the ESID 
    @Field (type = FieldType.Keyword) // field used for accurately searching or sorting keyword, no default index
     Private String ID; 
    @Field (type = FieldType.Text, Analyzer = "ik_max_word" ) // Text is automatically indexed, analyzer word is used here to specify a minimum fineness ik word
     Private String Brand; 

    
    @Field (of the type = FieldType.Text, analyzer = "ik_max_word" ) // es support arrays, but to maintain the same type
     Private List <String> keywords;
 
    @GeoPointField 
    Private GeoPoint LOCATION; // es latitude and longitude supports search 

    
    @Field (of the type = FieldType.Long)
     Private Long. price;


    @Field(type = FieldType.Integer)
    private Integer SystemSort;

}

Creating indexes and maps

    @Resource
     private ElasticsearchTemplate elasticsearchTemplate;

    @Test
     public void test() {
        elasticsearchTemplate.createIndex(ElasticsearchHotelDmo.class);
        elasticsearchTemplate.putMapping(ElasticsearchHotelDmo.class);
    }
Add to
elasticsearchHotelDao.save( ElasticsearchHotelDmo )
delete
elasticsearchHotelDao.deleteById();
The search interface service
Private  void serach () {
         // build native searcher 
        NativeSearchQueryBuilder QueryBuilder = new new NativeSearchQueryBuilder ();
         // Boolean query, for many conditions 
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery ();
         // Should or equivalent relationship; mechanism is termQuery : go directly to the matching token. 
        boolQuery.should (QueryBuilders.termQuery ( "", "" ));
         // matchQuery mechanism is: first check whether the field type is analyzed, and if so, the first word, go to the match; if not, then go directly to the match
         // boolQuery.should (QueryBuilders.matchQuery ( "", ""));
         // boolQuery.filter (); // filter filtering
         //boolQuery.must (); // MUST equivalent and relation
         // boolQuery.mustNot (); // MUST NOT not contain 
        queryBuilder.withQuery (boolQuery);
         // geoDistanceQuery location mapper field name from the search point on withFilter matched filter (double lat, double lon) lat longitude lon dimension distance (String distance, DistanceUnit unit) distance from the unit unit 
        queryBuilder.withFilter (QueryBuilders.geoDistanceQuery ( "LOCATION"). Point (. 1,. 1) .distance ( ". 3" , DistanceUnit.KILOMETERS ));
         // withQuery normal query sort rangeQuery (String name) zone searching by name mapper hits you want to search the fields get lte greater than or equal ge greater than or less lt less than 
        queryBuilder.withQuery (QueryBuilders.rangeQuery ( "price") gte. (100) .lte (500 ));
         //withSort sort fieldSort (String field) field sort column order (SortOrder order) Ascending or Descending 
        queryBuilder.withSort (SortBuilders.fieldSort ( "SystemSort" ) .order (SortOrder.DESC));
         // tab PageRequest of (int page, int size) this page is the first page of the number es not 0. 1 
        queryBuilder.withPageable (PageRequest.of (0, 10 ));
         // results 
        page <ElasticsearchHotelDmo> Search = elasticsearchHotelDao.search (queryBuilder.build ()); 
    }

 

 

Guess you like

Origin www.cnblogs.com/adfaf/p/12133873.html