当前位置:网站首页>0621~ES&Lucene

0621~ES&Lucene

2022-07-24 17:47:00 Life is so hard

The concept of full text retrieval : Index first , Then the process of searching the index is called full-text search ;

The characteristics of full-text retrieval :1. Key words highlight ;

                          2. Process text only , Do not process semantics ;

                         3. Correlation degree , The most keywords are in the front ;

ES: Page port :9200;

Code port :9300;

Kibana Port number :5601;

Full text indexing architecture ( a key ):

Index creation : 1. participle ;

2. Word case conversion ;

3. Sort ;

4. Merge ;

5. Form inverted index document ;

Index search

1. Search inverted index documents according to keywords , find id;

2. according to id Locate the data area of the index library to the data ;

ES How to create a connection :

// Create connection 
public static TransportClient getClient(){
    // on startup
    TransportClient client = null;
    try {
        client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    return client;
}

use es stay Java Add operation in

// New operation 
@Test
public void save(){
    // Create links 
    TransportClient client = EsDemo.getClient();

    IndexRequestBuilder indexRequestBuilder = client.prepareIndex("pethome", "user", "37");
    Map<String, Object> map = new HashMap<>();
    map.put("name"," New name ");
    map.put("age",26);
    map.put("sex",0);
    map.put("id",1);
    indexRequestBuilder.setSource(map).get();
    System.out.println(indexRequestBuilder.setSource(map));
    client.close();
}

  Conditions of the query :

//  The query user name contains zs,1 2
//  Gender equals 0,1 2
//  Age 18-80 year  ,1 2
//  Sort backwards by age , 1  2
//  Take the second page , each page 10 strip  1 2
@Test
public void Estest(){
    TransportClient client = getClient();
    SearchRequestBuilder prepareSearch = client.prepareSearch("pethome");
    prepareSearch.setTypes("user");
    //  Take the second page , each page 10 strip  1 2
    prepareSearch.setFrom(1).setSize(5);
    //  Sort backwards by age , 1  2
    prepareSearch.addSort("age",SortOrder.DESC);

    BoolQueryBuilder query = QueryBuilders.boolQuery();
    //  The query user name contains zs,1
    query.must(QueryBuilders.matchQuery("name","zs"));
    //  Age 18-80 year  ,1
    query.filter(QueryBuilders.rangeQuery("age").gte(18).lte(80));
    //  Gender equals 0,1 2
    query.filter(QueryBuilders.termQuery("sex","1"));
    SearchResponse searchResponse = prepareSearch.setQuery(query).get();
    SearchHits hits = searchResponse.getHits();
    SearchHit[] searchHits = hits.getHits();
    for (SearchHit searchHit : searchHits) {
        System.out.println(searchHit.getSource());
    }

原网站

版权声明
本文为[Life is so hard]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/205/202207241743302028.html