elastic search的基本用法

版本7.9.2

1.kibana

PUT /book

PUT /book/_mapping 
{
  "properties":{
    "tweet": { 
      "properties": {
        "tweetKey":            { "type": "text" },
        "user": { 
          "type":             "object",
          "properties": {
            "id":           { "type": "text" },
            "gender":       { "type": "text" },
            "age":          { "type": "long"   },
            "name":   { 
              "type":         "object",
              "properties": {
                "full":     { "type": "text" },
                "first":    { "type": "text" },
                "last":     { "type": "text" }
              }
            }
          }
        }
      }
    }
  }
}

PUT /book/_doc/1
{
  "tweet":{
    "tweetKey":"abc",
    "user":{
      "id":"1",
      "age":10
    }
  },
  "page":10
}
#查询所有的core(索引)
GET /_cat/indices

GET /book/_mapping

GET /book/_search?q=tweet.user.id=1

GET /book/_search
{
  "from":0,
  "size":10
}

GET /book/_search
{
  "query": {
    "match": {
      "page": 8
    }
  }
}

GET /book/_search
{
  "query": {
    "match": {
      "content": "chenqionghe"
    }
  }
}
#多条件查询
GET /book/_search
{
  "query":{
    "bool": {
      "must": [
        {
          "match": {
            "content": "chenqionghe"
          }
        },
        {
          "match": {
            "_id": "*B*"
          }
        }
      ]
    }
  }
}
#模糊匹配
GET /book/_search
{
    "query": {
        "wildcard" : { "content" : "*hen*" }
    }
}
#explain 
GET /book/_validate/query?explain
{
  "query":{
    "exists": {
        "field" :  "tweet" 
    }
  }
}

POST file/_update_by_query
{
  "query": {
    "match": {
      "_id": "61e2b030-784f-4755-91e6-39ac6e402668"
    }
  },
  "script": {
    "source": "ctx._source['newField'] ='abc'"
  }
}

POST /cz_file/_delete_by_query
{
  "query": {
    "match_all": {}
  }
}






GET _cat/indices

DELETE /cz_pro

PUT /cz_pro

GET /cz_pro/_search
{
  
  "track_total_hits": true, 
  "query": {
    "match_all": {
     
    }
  }
}

PUT /cz_pro/_mapping 
{
  "properties" : {
      "itemcode" : { 
        "type" : "keyword"
      }
  }
  
}

GET /cz_pro/_search
{
  
  "track_total_hits": true, 
  "query": {
    "match": {
      "itemcode":"398855052"
    }
  }
}

#设置返回字段
GET /proinfo_20201124172924/_search
{
  
  "track_total_hits": true, 
  "query": {
    "match_all": {
    }
  },
  "_source":["itemcode"]
}
#必须存在某字段
GET /cz_pro/_search
{
  "track_total_hits": true, 
  "query": {
    "bool": {
      "must": [
        {
          "exists": {"field": "adproduct"}
        }
      ]
    }
  },
  "_source":["itemcode","adproduct"]
}



使用id,禁用source:
DELETE /cz_pro
PUT /cz_pro
PUT /cz_pro/_mapping 
{
  "_source": {
      "enabled": false
  },
  
  "properties": {
    "id": { "type": "text"  },
    "age":    { "type": "integer" },  
    "email":  { "type": "keyword"  }, 
    "name":   { "type": "text"  }     
  }

}

GET /cz_pro/_stats

PUT /cz_pro/_doc/1
{
  "age":   1,  
  "email":  "1@com", 
  "name":   "1"
}


GET /cz_pro/_search
{
  "query": {
    "match_all": {
    }
  }
}

POST /cz_pro/_update/1
{
  "doc" : {
    "age":2
  }
}


2.java api更新

RestHighLevelClient client= new RestHighLevelClient(RestClient.builder(new HttpHost("xxx.xx.114",9620)));
String index="cz_pro";
String id="1";
Map<String, Object> setValMap=new HashMap(){{
    put("age",2);
}};
client.update(new UpdateRequest(index, id).doc(setValMap),RequestOptions.DEFAULT);
client.close();
3.java api增加

RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("xx.xx.xx.43", 9820, "http")));
String index="cz_pro";
String id="1";
BulkRequest bulkRequest = new BulkRequest();

Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("gmvscore", 0);

IndexRequest indexRequest = new IndexRequest(index);
indexRequest.id(id).source(jsonMap);
bulkRequest.add(indexRequest);
BulkResponse rsp= client.bulk(bulkRequest, RequestOptions.DEFAULT);
client.close();

4.java api查询

RestHighLevelClient client= new RestHighLevelClient(
        RestClient.builder(new HttpHost("xx.xx.xx.114",9820)));
String index="cz_pro";
int countPage=0;
int len=10;
//查询条件
RangeQueryBuilder rangeQueryBuilder =QueryBuilders.rangeQuery("age").gt(0);


SearchRequest searchRequest = new SearchRequest(index);
// 2,构建SearchSourceBuilder查询对象
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.trackTotalHits(true);

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(rangeQueryBuilder);
// 检索条件
sourceBuilder.query(boolQueryBuilder);

// 分页、排序
//.sort("id", SortOrder.ASC) 可加排序
sourceBuilder.from(countPage*len).size(len).timeout(TimeValue.timeValueSeconds(60));

sourceBuilder.fetchSource(new String[]{"id","age","name"}, null);

searchRequest.source(sourceBuilder);
SearchResponse rsp = client.search(searchRequest, RequestOptions.DEFAULT);
if (rsp != null) {
    SearchHits hits = rsp.getHits();
    int rstLen = hits.getHits().length;
    Iterator<SearchHit> it = hits.iterator();
    System.out.println("return "+rstLen+((countPage-1)*len));
    while (it.hasNext()) {
        SearchHit sh = it.next();

        Map<String, Object> sourceAsMap = sh.getSourceAsMap();
       // List itemCode = (List) sourceAsMap.get("itemcode");

        System.out.println("age:"+sourceAsMap.get("age"));
    }

}
client.close();

文/程忠 浏览次数:0次   2021-01-26 13:55:01

相关阅读


评论:
点击刷新

↓ 广告开始-头部带绿为生活 ↓
↑ 广告结束-尾部支持多点击 ↑