企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## **query_string查询** 例如: ``` POST my_index/_doc/_search { "query":{ "query_string":{ "default_field":"title", "query":"宝马" } } } ``` 说明: * query_string查询跟url query查询类似,就是将查询条件写在json体中 * default_field 指的是默认查询的字段 * query 查询条件 ‘AND’ 或则 'OR'记的大写 * fields 指定查询字段集合,数组形式,例如:['title','author']指查询title和author字段 ## **Simple Query String Query** * 类似Query String ,但会忽略错误的查询表达式,并且仅支持部分查询语法 * 其常用的逻辑服务如下,不能使用 AND OR NOT 等关键字,否则当普通字符串处理 + 代表AND | 代表OR - 代表NOT ## **query关键字** 要搜索的body 例如: ``` GET /_search { "query" : { "term" : { "user" : "kimchy" } } } ``` ## **from / Size** 如果需要分页,则使用from和size关键字。 例如: ``` #从0位置开始,获取10条数据。即获取:0-10的数据 GET /_search { "from" : 0, "size" : 10, "query" : { "term" : { "user" : "kimchy" } } } ``` ## **sort** 指定结果的排序方式,可以指定某个字段排序,也可用于 _score 按分数排序,以及 _doc 按索引顺序排序 例如: ``` PUT /my_index { "mappings": { "_doc": { "properties": { "post_date": { "type": "date" }, "user": { "type": "keyword" }, "name": { "type": "keyword" }, "age": { "type": "integer" } } } } } GET /my_index/_search { "sort" : [ { "post_date" : {"order" : "asc"}}, "user", { "name" : "desc" }, { "age" : "desc" }, "_score" ], "query" : { "term" : { "user" : "kimchy" } } } ``` ## **_source** 指定结果返回的字段,默认情况下返回所有字段,也可不返回字段。 ``` #不返回_source字段,即没有文档内容返回 POST my_index/_doc/_search { "_source":false, "query":{ "query_string":{ "default_field":"title", "query":"宝马" } } } #返回结果 { "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.2876821, "hits": [ { "_index": "my_index", "_type": "_doc", "_id": "2", "_score": 0.2876821 }, { "_index": "my_index", "_type": "_doc", "_id": "1", "_score": 0.2876821 } ] } } #返回指定字段开头的字段 POST my_index/_doc/_search { "_source":"tit*", "query":{ "query_string":{ "default_field":"title", "query":"宝马" } } } #返回结果 { "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.2876821, "hits": [ { "_index": "my_index", "_type": "_doc", "_id": "2", "_score": 0.2876821, "_source": { "title": "我的宝马x5有260马力" } }, { "_index": "my_index", "_type": "_doc", "_id": "1", "_score": 0.2876821, "_source": { "title": "我的宝马有222马力" } } ] } } #多个时可以这样用(返回以"tit"开头和"na"的字段) POST my_index/_doc/_search { "_source":["tit*","na*"], "query":{ "query_string":{ "default_field":"title", "query":"宝马" } } } #指定不返回的字段 POST my_index/_doc/_search { "_source":{ "includes":["title"], "excludes":["name"] }, "query":{ "query_string":{ "default_field":"title", "query":"宝马" } } } ``` ## **highlight** 用于高亮搜索,将搜索出来的结果用标签包裹起来,不指定标签默认"`<em>`"标签包裹 ``` POST my_index/_doc/_search { "query":{ "match":{ "title":"宝马" } }, "highlight":{ "fields":{ "title":{} } } } #返回结果 { "took": 19, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.2876821, "hits": [ { "_index": "my_index", "_type": "_doc", "_id": "2", "_score": 0.2876821, "_source": { "name": "宝马", "title": "我的宝马x5有260马力" }, "highlight": { "title": [ "我的<em>宝马</em>x5有260马力" ] } }, { "_index": "my_index", "_type": "_doc", "_id": "1", "_score": 0.2876821, "_source": { "name": "张三", "title": "我的宝马有222马力" }, "highlight": { "title": [ "我的<em>宝马</em>有222马力" ] } } ] } } ``` 自定义标签 ``` POST my_index/_doc/_search { "query":{ "match":{ "title":"宝马" } }, "highlight":{ "fields":{ "title":{ "pre_tags":["<mark>"], "post_tags":["</mark>"] } } } } #返回结果 { "took": 6, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.2876821, "hits": [ { "_index": "my_index", "_type": "_doc", "_id": "2", "_score": 0.2876821, "_source": { "name": "宝马", "title": "我的宝马x5有260马力" }, "highlight": { "title": [ "我的<mark>宝马</mark>x5有260马力" ] } }, { "_index": "my_index", "_type": "_doc", "_id": "1", "_score": 0.2876821, "_source": { "name": "张三", "title": "我的宝马有222马力" }, "highlight": { "title": [ "我的<mark>宝马</mark>有222马力" ] } } ] } } ``` 多字段高亮 ``` POST my_index/_doc/_search { "query":{ "match":{ "title":"宝马" } }, "highlight":{ "require_field_match":false, "fields":{ "title":{ "pre_tags":["<mark>"], "post_tags":["</mark>"] }, "name":{} } } } #返回结果 { "took": 5, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.2876821, "hits": [ { "_index": "my_index", "_type": "_doc", "_id": "2", "_score": 0.2876821, "_source": { "name": "宝马", "title": "我的宝马x5有260马力" }, "highlight": { "name": [ "<em>宝马</em>" ], "title": [ "我的<mark>宝马</mark>x5有260马力" ] } }, { "_index": "my_index", "_type": "_doc", "_id": "1", "_score": 0.2876821, "_source": { "name": "张三", "title": "我的宝马有222马力" }, "highlight": { "title": [ "我的<mark>宝马</mark>有222马力" ] } } ] } } ``` 说明:默认情况下只高亮显示被索引的字段,即:require_filed_match 默认为 true,当 require_filed_match 为 false时,未被索引的字段也可以被高亮显示。