@@ -666,6 +666,295 @@ POST kibana_sample_data_ecommerce/_msearch
666666{" query" : {" match_all" : {}}," size" :2}
667667` ` `
668668
669+ # ## URI Search 查询语义
670+
671+ Elasticsearch URI Search 遵循 QueryString 查询语义,其形式如下:
672+
673+ ` ` ` bash
674+ GET /movies/_search? q=2012& df=title& sort=year:desc& from=0& size=10& timeout=1s
675+ {
676+ " profile" : true
677+ }
678+ ` ` `
679+
680+ - ** ` q` ** 指定查询语句,使用 QueryString 语义
681+ - ** ` df` ** 默认字段,不指定时
682+ - ** ` sort` ** 排序:from 和 size 用于分页
683+ - ** ` profile` ** 可以查看查询时如何被执行的
684+
685+ ` ` ` bash
686+ GET /movies/_search? q=title:2012& sort=year:desc& from=0& size=10& timeout=1s
687+ {
688+ " profile" :" true"
689+ }
690+ ` ` `
691+
692+ # ### Term 和 Phrase
693+
694+ Beautiful Mind 等效于 Beautiful OR Mind
695+
696+ " Beautiful Mind" 等效于 Beautiful AND Mind
697+
698+ ` ` ` bash
699+ # Term 查询
700+ GET /movies/_search? q=title:Beautiful Mind
701+ {
702+ " profile" :" true"
703+ }
704+
705+ # 使用引号,Phrase 查询
706+ GET /movies/_search? q=title:" Beautiful Mind"
707+ {
708+ " profile" :" true"
709+ }
710+ ` ` `
711+
712+ # ### 分组与引号
713+
714+ title:(Beautiful AND Mind)
715+
716+ title=" Beautiful Mind"
717+
718+ # ### AND、OR、NOT 或者 &&、||、!
719+
720+ > 注意:AND、OR、NOT 必须大写
721+
722+ ` ` ` bash
723+ # 布尔操作符
724+ GET /movies/_search? q=title:(Beautiful AND Mind)
725+ {
726+ " profile" :" true"
727+ }
728+
729+ GET /movies/_search? q=title:(Beautiful NOT Mind)
730+ {
731+ " profile" :" true"
732+ }
733+ ` ` `
734+
735+ # ### 范围查询
736+
737+ - ` []` 表示闭区间
738+ - ` {}` 表示开区间
739+
740+ 示例:
741+
742+ ` ` ` bash
743+ # 范围查询 ,区间写法
744+ GET /movies/_search? q=title:beautiful AND year:{2010 TO 2018%7D
745+ {
746+ " profile" :" true"
747+ }
748+
749+ GET /movies/_search? q=title:beautiful AND year:[* TO 2018]
750+ {
751+ " profile" :" true"
752+ }
753+ ` ` `
754+
755+ # ### 算数符号
756+
757+ ` ` ` bash
758+ # 2010 年以后的记录
759+ GET /movies/_search? q=year:> 2010
760+ {
761+ " profile" :" true"
762+ }
763+
764+ # 2010 年到 2018 年的记录
765+ GET /movies/_search? q=year:(> 2010 && < =2018)
766+ {
767+ " profile" :" true"
768+ }
769+
770+ # 2010 年到 2018 年的记录
771+ GET /movies/_search? q=year:(+> 2010 +< =2018)
772+ {
773+ " profile" :" true"
774+ }
775+ ` ` `
776+
777+ # ### 通配符查询
778+
779+ - ` ? ` 代表 1 个字符
780+ - ` * ` 代表 0 或多个字符
781+
782+ 示例:
783+
784+ ` ` ` bash
785+ GET /movies/_search? q=title:mi? d
786+ {
787+ " profile" :" true"
788+ }
789+
790+ GET /movies/_search? q=title:b*
791+ {
792+ " profile" :" true"
793+ }
794+ ` ` `
795+
796+ # ### 正则表达式
797+
798+ title:[bt]oy
799+
800+ # ### 模糊匹配与近似查询
801+
802+ 示例:
803+
804+ ` ` ` bash
805+ # 相似度在 1 个字符以内
806+ GET /movies/_search? q=title:beautifl~1
807+ {
808+ " profile" :" true"
809+ }
810+
811+ # 相似度在 2 个字符以内
812+ GET /movies/_search? q=title:" Lord Rings" ~2
813+ {
814+ " profile" :" true"
815+ }
816+ ` ` `
817+
818+ # ## Request Body & DSL
819+
820+ Elasticsearch 除了 URI Search 查询方式,还支持将查询语句通过 Http Request Body 发起查询。
821+
822+ ` ` ` bash
823+ GET /kibana_sample_data_ecommerce/_search? ignore_unavailable=true
824+ {
825+ " profile" :" true" ,
826+ " query" : {
827+ " match_all" : {}
828+ }
829+ }
830+ ` ` `
831+
832+ # ### 分页
833+
834+ ` ` ` bash
835+ GET /kibana_sample_data_ecommerce/_search? ignore_unavailable=true
836+ {
837+ " profile" : " true" ,
838+ " from" : 0,
839+ " size" : 10,
840+ " query" : {
841+ " match_all" : {}
842+ }
843+ }
844+ ` ` `
845+
846+ # ### 排序
847+
848+ 最好在数字型或日期型字段上排序
849+
850+ 因为对于多值类型或分析过的字段排序,系统会选一个值,无法得知该值
851+
852+ ` ` ` bash
853+ GET /kibana_sample_data_ecommerce/_search? ignore_unavailable=true
854+ {
855+ " profile" : " true" ,
856+ " sort" : [
857+ {
858+ " order_date" : " desc"
859+ }
860+ ],
861+ " from" : 1,
862+ " size" : 10,
863+ " query" : {
864+ " match_all" : {}
865+ }
866+ }
867+ ` ` `
868+
869+ # ### \_source 过滤
870+
871+ 如果 ` _source` 没有存储,那就只返回匹配的文档的元数据
872+
873+ ` _source` 支持使用通配符,如:` _source[" name*" , " desc*" ]`
874+
875+ 示例:
876+
877+ ` ` ` bash
878+ GET /kibana_sample_data_ecommerce/_search? ignore_unavailable=true
879+ {
880+ " profile" : " true" ,
881+ " _source" : [
882+ " order_date" ,
883+ " category.keyword"
884+ ],
885+ " from" : 1,
886+ " size" : 10,
887+ " query" : {
888+ " match_all" : {}
889+ }
890+ }
891+ ` ` `
892+
893+ # ### 脚本字段
894+
895+ ` ` ` bash
896+ GET /kibana_sample_data_ecommerce/_search? ignore_unavailable=true
897+ {
898+ " profile" : " true" ,
899+ " script_fields" : {
900+ " new_field" : {
901+ " script" : {
902+ " lang" : " painless" ,
903+ " source" :" doc['order_date'].value+' hello'"
904+ }
905+ }
906+ },
907+ " from" : 1,
908+ " size" : 10,
909+ " query" : {
910+ " match_all" : {}
911+ }
912+ }
913+
914+ ` ` `
915+
916+ # ### 使用查询表达式 - Match
917+
918+ ` ` ` bash
919+ POST movies/_search
920+ {
921+ " query" : {
922+ " match" : {
923+ " title" : " last christmas"
924+ }
925+ }
926+ }
927+
928+ POST movies/_search
929+ {
930+ " query" : {
931+ " match" : {
932+ " title" : {
933+ " query" : " last christmas" ,
934+ " operator" : " and"
935+ }
936+ }
937+ }
938+ }
939+
940+ ` ` `
941+
942+ # ### 短语搜索 - Match Phrase
943+
944+ ` ` ` bash
945+ POST movies/_search
946+ {
947+ " query" : {
948+ " match_phrase" : {
949+ " title" :{
950+ " query" : " last christmas"
951+
952+ }
953+ }
954+ }
955+ }
956+ ` ` `
957+
669958# # 集群 API
670959
671960> [Elasticsearch 官方之 Cluster API](https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster.html)
0 commit comments