1、restful api查询css
1.一、查询订单前端
POST order/_search?q=P201903230001
1.二、更新订单web
POST order / area / 1 / _update { "doc": { " price": "5", "payee": "tom" } }
1.三、删除订单(删除order索引/库)sql
DELETE order
1.四、增长订单json
POST order / area / 1 { " price": "5", "payee": "tom" }
1.五、查询索引数据,或者索引下某类型数据api
一、 GET /lib/user/_search : 查询lib索引下的user类型的所有数据 二、 GET /lib/_search :查询lib索引下的所有类型的数据 三、 GET /_search :查询所有索引下的数据
2、DSL格式理解数组
2.一、DSL查询语句属性值是json数组仍是json,取决于你条件是多个仍是单个。 JSON数据格式灵活性安全
一个条件时候能够用json must:{ "match": { "title": "Search" } } 多个条件时候能够用json数组 must:[ { "match": { "title": "Search" }}, { "match": { "content": "Elasticsearch" }} ]
3、Query DSLrestful
Elasticsearch 提供了一个完整的 query DSL,而且是 JSON 形式的多线程
叶子查询语句(Leaf Query):用于查询某个特定的字段,如 match , term 或 range 等
复合查询语句 (Compound query clauses):用于合并其余的叶查询或复合查询语句,也就是说复合语句之间能够嵌套,用来表示一个复杂的单一查询
3.一、查询order索引下全部数据
GET order / area / _search { "query": { "match_all": {} } }
3.二、分词查询:订单、多小、商品,包含这三个词中的一个或多个的文档就会被搜索出来
GET / _search { "query": { "match": { "content": { "query": "个人订单有多小种类商品" } } } }
3.三、分词查询:订单、多小、商品,包含这三个词中的文档就会被搜索出来。
match与match_phrase区别:match_phrase匹配到全部分词才能查询出来
GET / _search { "query": { "match_phrase": { "content": { "query": "个人订单有多小种类商品" } } } }
3.四、多字段匹配
GET order / area / _search { "query": { "multi_match": { "query": "this is a test", "fields": ["subject", "message"] } } }
3.五、彻底匹配
GET order / area / _search { "query": { "term": { "NUM": "P201903230001" } } }
3.六、逻辑运算
6.3 逻辑运算 若是有多个搜索关键字, Elastic 认为它们是or关系。 $ curl 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "软件 系统" }} }' 上面代码搜索的是软件 or 系统。 若是要执行多个关键词的and搜索,必须使用布尔查询。 $ curl 'localhost:9200/accounts/person/_search' -d ' { "query": { "bool": { "must": [ { "match": { "desc": "软件" } }, { "match": { "desc": "系统" } } ] } } }'
3.七、range范围查询。下面查询出订单销售在100-200之间数据
GET order/area/_search { "query":{ "bool": { "must": [ { "range": { "sales": { "from": 100, "to": null } } }, { "range": { "sales": { "from": null, "to": 200 } } } ] } } } 或 GET order/area/_search { "query":{ "bool": { "must": [ { "range": { "sales": { "from": 100, "to": 200 } } } ] } } }
3.八、term精确匹配
3.8.一、term精确匹配:对字段进行确切值(exact value)的查询,如数字、时间、bool、not_analyzed字段等。
3.8.二、注意:使用term要肯定的是这个字段是否“被分析”(analyzed),默认的字符串是被分析的。字段被分析将查询不出内容尽管字符串彻底匹配
3.8.三、为何有时候使用term查不出数据? 只是string类型才查不出,其它类型是能够的。
缘由:字段被分析将查询不出内容尽管字符串彻底匹配。须要修改为:index:not_analyzed:将字段的原始值放入索引中,做为一个独立的term,它是除string字段之外的全部字段的默认值。
注意:在 elasticsearch2.x 版本,字符串数据只有string类型。ES更新到5版本后,取消了 string 数据类型,代替它的是 keyword 和 text 数据类型
官方解析
Add the field value to the index unchanged, as a single term. This is the default for all fields that support this option except for string fields. not_analyzed fields are usually used with term-level queries for structured search.
GET /megacorp/employee/_search { "query" :{ "bool": { "must" : [ {"term" : {"NAME":"我是小明"}} ] } } }
实践:1:
只有分词彻底匹配、彻底匹配、彻底匹配“大白菜”这三个字后才能够返回数据。JAVA程序中采用QueryBuilders类的termQuery(String name, Object value)方法。
GET /my_index/ny_type/_search { "query":{ "term":{ "productName":"大白菜" } } }
实践2:
第一次使用"term":{"productName":"大白菜" } 没返回数据以为很疑惑。明明存在数据“今天大白菜很新鲜” ,确没匹配到任何数据。
缘由:productName字段type=text而且使用默认分词器,大白菜 => [大,白,菜]。分词后变成3个字,而term查询须要彻底匹配“大白菜”,大白菜做1个分词去匹配查找数据。
因此最后没有数据返回。
GET /my_index/ny_type/_search { "query":{ "term":{ "productName":"大白菜" } } }
3.8.四、字段text类型,怎样可使用term精确查询?
3.8.4.1. text类型:会分词,先把对象进行分词处理,而后再再存入到es中。
当使用多个单词进行查询的时候,固然查不到已经分词过的内容!
3.8.4.2. keyword:不分词,没有把es中的对象进行分词处理,而是存入了整个对象! 这时候固然能够进行完整地查询!默认是256个字符!
{ "mapping": { "my_type": { "properties": { "title": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } }
GET my_index/my_type/_search { "query":{ "term": { "title.keyword": "elasticsearch is good" } } }
3.九、terms多词精确匹配
sql中的in: select * from tbl where col in ("value1", "value2") sql中的in,在elasticsearch使用terms实现 term: {"field": "value"} terms: {"field": ["value1", "value2"]}
3.十、Bool Query
使用must语法,bool必须加上
咱们能够在查询条件中使用AND/OR/NOT操做符,这就是布尔查询(Bool Query)。布尔查询能够接受一个must参数(等价于AND),一个must_not参数(等价于NOT),以及一个should参数(等价于OR)。好比,我想查询about中出现music或者climb关键字的员工,员工的名字是John,但姓氏不是smith,咱们能够这么来查询:
GET /megacorp/employee/_search { "query": { "bool": { "must": { "bool" : { "should": [ { "match": { "about": "music" }}, { "match": { "about": "climb" }} ] } }, "must": { "match": { "first_nale": "John" } }, "must_not": { "match": {"last_name": "Smith" } } } } } GET /megacorp/employee/_search { "query" :{ "bool": { "must" : [ {"match" : {"NAME":"我是小明"}}, {"match" : {"INTEREST":"足球"}} ] } } }
3.10.一、Bool Query语法
bool过滤:用来合并多个过滤条件的查询结果的布尔逻辑,必须包含must和should中的一个或多个。它包含如下一些操做 must: 至关于and must_not: 至关于 not should: 至关于or(链接多个匹配条件,列表形式) { "query": { "bool":{ "must":{ //and age==50 "term":{"age":50} }, "must_not":{ //not date=2014-09-01 "term":{"date": "2014-09-01"} }, "should":[ // _id==8 or _id=9 (举的不太恰当) {"term":{"_id":8}}, {"term":{"_id":19}} ] } } } 下面错误语法。bool里面只能有must,must_not,should等相同单词只能出现一次。 { "query": { "bool":{ "must":{ //and age==50 "term":{"age":50} }, "must":{ //and firstName==jack "term":{"firstName":"jack"} } "must_not":{ //not date=2014-09-01 "term":{"date": "2014-09-01"} }, "should":[ // _id==8 or _id=9 (举的不太恰当) {"term":{"_id":8}}, {"term":{"_id":19}} ] } } }
3.十一、处理 Null 值:exists/missing
exists/missing做用:用来查找某个字段是否有值, 相似SQL中的 not is_null/is_null
它返回某个特定有值字段的文档,用 SQL 的话就是用 IS NOT NULL 非空进行查询: SELECT tags FROM posts WHERE tags IS NOT NULL GET /my_index/posts/_search { "query" : { "constant_score" : { "filter" : { "exists" : { "field" : "tags" } } } } } 这个 missing 查询本质上与 exists 刚好相反: 它返回某个特定 _无_ 值字段的文档,与如下 SQL 表达的意思相似: SELECT tags FROM posts WHERE tags IS NULL GET /my_index/posts/_search { "query" : { "constant_score" : { "filter": { "missing" : { "field" : "tags" } } } }
3.十二、检查语法是否正确
GET my_index/my_type/_validate/query?explain
3.1三、multi_match多字段匹配查询
//查询title,content包含elasticsearch GET /website/article/_search { "query": { "multi_match": { "query": "elasticsearch", "fields": ["title","content"] } } }
3.1四、filter使用
//查找书名包含elasticsearch,而且价格在100-200之间 GET book/area/_search { "query": { "bool": { "must": [ {"match": { "name": "elasticsearch" }} ], "filter": { "range": { "price": { "gt": 100, "lt": 200 } } } } } }
3.1五、编写高亮检索代码
public void highlightSearch() throws ParseException { //建立加载配置文件的客户端工具,用来检索文档,单实例多线程安全 ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil(mappath); //设定查询条件,经过map传递变量参数值,key对于dsl中的变量名称 //dsl中有三个变量 // condition // startTime // endTime Map<String,Object> params = new HashMap<String,Object>(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间范围,时间参数接受long值 params.put("startTime",dateFormat.parse("2017-09-02 00:00:00")); params.put("endTime",new Date()); params.put("condition","喜欢唱歌");//全文检索条件,匹配上的记录的字段值对应的匹配内容都会被高亮显示 //执行查询,demo为索引表,_search为检索操做action ESDatas<Demo> esDatas = //ESDatas包含当前检索的记录集合,最多1000条记录,由dsl中的size属性指定 clientUtil.searchList("demo/_search",//demo为索引表,_search为检索操做action "testHighlightSearch",//esmapper/demo.xml中定义的dsl语句 params,//变量参数 Demo.class);//返回的文档封装对象类型 //获取总记录数 long totalSize = esDatas.getTotalSize(); System.out.println(totalSize); //获取结果对象列表,最多返回1000条记录 List<Demo> demos = esDatas.getDatas(); for(int i = 0; demos != null && i < demos.size(); i ++){//遍历检索结果列表 Demo demo = demos.get(i); //记录中匹配上检索条件的全部字段的高亮内容 Map<String,List<Object>> highLights = demo.getHighlight(); Iterator<Map.Entry<String, List<Object>>> entries = highLights.entrySet().iterator(); while(entries.hasNext()){ Map.Entry<String, List<Object>> entry = entries.next(); String fieldName = entry.getKey(); System.out.print(fieldName+":"); List<Object> fieldHighLightSegments = entry.getValue(); for (Object highLightSegment:fieldHighLightSegments){ /** * 在dsl中经过<mark></mark>来标识须要高亮显示的内容,而后传到web ui前端的时候,经过为mark元素添加css样式来设置高亮的颜色背景样式 * 例如: * <style type="text/css"> * .mark,mark{background-color:#f39c12;padding:.2em} * </style> */ System.out.println(highLightSegment); } } } }
3.1五、wildcard通配符查询,中文只能支持关键字查询,即只支持 type=keywork
#有数据返回,wildcard只支持type=keyword GET /my_index/my_type/_search{ "query": { "wildcard": {"name.keyword":"*标准化*"} } } #没数据返回,wildcard只支持type=keyword GET /my_index/my_type/_search{ "query": { "wildcard": {"name":"*标准化*"} } }