elasticsearch 操作
文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
客户端:https://www.elastic.co/guide/en/elasticsearch/client/index.html
API:https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-from-size.html
.net
1.连接,项目中引用Nest包
var settings = new ConnectionSettings(new Uri("http://example.com:9200")) .DefaultIndex("people"); var client = new ElasticClient(settings);
2.索引
public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
var person = new Person { Id = 1, FirstName = "Martijn", LastName = "Laarman" }; var indexResponse = client.IndexDocument(person); var asyncIndexResponse = await client.IndexDocumentAsync(person);
3.查询
var searchResponse = client.Search<Person>(s => s .From(0) .Size(10) .Query(q => q .Match(m => m .Field(f => f.FirstName) .Query("Martijn") ) ) ); var people = searchResponse.Documents;
4.删除
var person = new Person { Id = 1 }; var deleteResponse= client.Delete<Person>(dept);
在kibana中操作
查看
删除
添加一个新的Index
public class StudentQuery { public int StudentId { get; set; } public List<string> Items { get; set; } }
var settings = new ConnectionSettings(new Uri("http://10.15.4.155:9200/")).DefaultIndex("student"); var client = new ElasticClient(settings); for (int i = 0; i < 10; i++) { StudentQuery student = new StudentQuery(); student.StudentId = i; student.Items = new List<string>(); student.Items.Add("Code"+i); student.Items.Add("Name"+i); student.Items.Add("CardId"+i); student.Items.Add("IdentityId"+i); var indexResponse = client.IndexDocument(student); }
查询
var searchResponse = client.Search<StudentQuery>(s => s. Query(q=>q. Match(m=>m. Field(f=>f.Items) .Query("CardId1") ) ) );
在kibana中查询
GET /student/studentquery/_search/ { "query": { "match": { "items": { "query": "Code8" } } } }
对同一字段多个值查询
或者使用term
GET /_search { "from" : 0, "size" : 10, "query" : { "term" : { "user" : "kimchy" } } }
这里要注意match和term的区别
Why doesn’t the term
query match my document?
简单来说就是string类型的字段在index之前会已fulltext的方式被es进行默认分词,比如放入“基金交易”,用term来查“基金”可能就查不到,用match就可以。
Java
@Service public class ElasticService { @Value("${elastic.host}") private String host; @Value("${elastic.port}") private int port; private RestHighLevelClient client; public void index(String index, List<MetisQuestion> list) throws Exception { client = new RestHighLevelClient( RestClient.builder( new HttpHost(host, port, "http"))); for(MetisQuestion o :list){ IndexRequest request = new IndexRequest(index,"doc"); ObjectMapper mapper=new ObjectMapper(); String json=mapper.writeValueAsString(o); request.source(json, XContentType.JSON); IndexResponse response = client.index(request, RequestOptions.DEFAULT); System.out.println(response.status()); } } public void search(String index,String key){ client = new RestHighLevelClient( RestClient.builder( new HttpHost(host, port, "http"))); SearchRequest searchRequest = new SearchRequest(index); searchRequest.types("doc"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.termQuery("question", key)); searchRequest.source(sourceBuilder); getResponse(searchRequest); } public void search(String index,String key,String... values){ client = new RestHighLevelClient( RestClient.builder( new HttpHost(host, port, "http"))); SearchRequest searchRequest = new SearchRequest(index); searchRequest.types("doc"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.termsQuery(key,values)); sourceBuilder.size(5); searchRequest.source(sourceBuilder); getResponse(searchRequest); } private void getResponse(SearchRequest searchRequest) { try { SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = searchResponse.getHits(); for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); ObjectMapper mapper=new ObjectMapper(); MetisQuestion question=mapper.readValue(sourceAsString,MetisQuestion.class); System.out.println(question.getQuestion()); } } catch (IOException e) { e.printStackTrace(); } } }