Elasticsearch查询-Match Query

来源:互联网 发布:数据库索引原理 编辑:程序博客网 时间:2024/04/29 11:13

新建索引数据

PUT /_bulk{ "index" : { "_index" : "students", "_type" : "student", "_id" : "1" } }{ "name" : "瑞文" ,"age":9,"grade":"三年级","class":"1班"}{ "index" : { "_index" : "students", "_type" : "student", "_id" : "2" } }{ "name" : "瑞兹" ,"age":8,"grade":"三年级","class":"2班"}{ "index" : { "_index" : "students", "_type" : "student", "_id" : "3" } }{ "name" : "男刀" ,"age":9,"grade":"三年级","class":"3班"}{ "index" : { "_index" : "students", "_type" : "student", "_id" : "4" } }{ "name" : "女刀" ,"age":8,"grade":"三年级","class":"1班"}{ "index" : { "_index" : "students", "_type" : "student", "_id" : "5" } }{ "name" : "蜘蛛" ,"age":9,"grade":"三年级","class":"2班"}{ "index" : { "_index" : "students", "_type" : "student", "_id" : "6" } }{ "name" : "挖掘机" ,"age":8,"grade":"三年级","class":"3班"}{ "index" : { "_index" : "students", "_type" : "student", "_id" : "7" } }{ "name" : "劫" ,"age":9,"grade":"三年级","class":"1班"}{ "index" : { "_index" : "students", "_type" : "student", "_id" : "8" } }{ "name" : "乐芙兰" ,"age":8,"grade":"三年级","class":"2班"}{ "index" : { "_index" : "students", "_type" : "student", "_id" : "9" } }{ "name" : "狐狸" ,"age":9,"grade":"三年级","class":"2班"}{ "index" : { "_index" : "students", "_type" : "student", "_id" : "10" } }{ "name" : "盖伦" ,"age":8,"grade":"三年级","class":"3班"}

查询的json:

{  "match" : {    "name" : {      "query" : " 狐狸",      "type" : "boolean"    }  }}

java-api

 QueryBuilder queryBuilder = QueryBuilders.matchQuery(                "name",                " 狐狸"        );        SearchResponse searchResponse = client.prepareSearch("students").setTypes("student").setQuery(queryBuilder).execute().actionGet();        SearchHits searchHits = searchResponse.getHits();        SearchHit[] hits = searchHits.getHits();        for (SearchHit searchHitFields : hits){            System.out.println(searchHitFields.getSource());        }

结果如下:

{grade=三年级, name=狐狸, class=2班, age=9}
2 0
原创粉丝点击