记录es的URI search搜索方式

Posted 泛舟五湖之间

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记录es的URI search搜索方式相关的知识,希望对你有一定的参考价值。

在前一篇的文章中我们讲述了关于es的一些关于文档、索引、集群的基本的应用,仅仅起了个开头的作用。
在这一篇中我们将学习es的一些更高级的用法以及更详细的用法。主要是包括搜索和聚合。其中是搜索有普通搜索,
结构化搜索、全文检索、join查询、复合查询、地理信息查询、对查询结果处理、调整相关度进行查询。
聚合主要包括桶聚合、指标聚合、管道聚合,可能会稍微介绍一下矩阵聚合。
接下里我们开始我们的学习之路!!!

一、 搜索

一、URI search

首先我们摘取从《elasticsearch技术解析与实战》中的一张关于URI search的参数表格

参数解释
q查询字符串,例如q=syslog
df当查询中没有定义前缀的时候默认使用的字段
analyzer当分析查询字符串的时候使用的分词器
lowercase_expanded_terms搜索的时候忽略大小写标志,默认为true
analyzer_wildcard通配符或者前缀查询是否被分析,默认为false
default_operator默认多个条件的关系,AND或者OR,默认OR
lenient如果设置为true,字段类型转换失败的时候将被忽略,默认为false
explain在每个返回结果中,将包含评分机制的解释
_source是否包含元数据,同时支持_source_include和_source_exclude
fields只返回索引中指定的列,多个列中间用逗号分开
sort根据字段名排序,例如fieldName:asc或者fieldName:desc
track_scores评分轨迹,当排序的时候,true表示返回评分的信息
timeout超时的时间设置
terminate_after在每个分片查询的最大条数,如果设置,返回结果中会有一个terminate_early字段
from返回的索引匹配结果的开始值,默认为0
size搜索结果返回的条数,默认为10
search_type搜索的类型,可以是dfs_query_then_fetch、query_then_fetch,默认为query_then_fetch

上述多个参数在使用的时候用&分开即可。下面我们开始学习使用URL search:

一、Query String Syntax的用法

GET bank/_search?q=Nanette

据说性能不好,因为它是对所有的文档进行了查询,搜索是否包含Nanette。

GET bank/_search?q=Nanette

"profile": "true"

此处仅仅添加了条件q=Nanette,所以查询的时候会去查询所有的字段中包含Nanette的信息,这一点我们通过profile返回的信息也可以看出来。
profile是在查询的时候可以输出查询的具体执行路径。即执行计划。由于篇幅的问题,我只在这一个查询中使用,后续的查询我们只学习查询的方式,不会再去查看具体的查询执行流程。自己学习的时候可以私下运行执行计划。
返回结果为:


  "took" : 2,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 1,
      "relation" : "eq"
    ,
    "max_score" : 6.5042877,
    "hits" : [
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "13",
        "_score" : 6.5042877,
        "_source" : 
          "account_number" : 13,
          "balance" : 32838,
          "firstname" : "Nanette",
          "lastname" : "Bates",
          "age" : 28,
          "gender" : "F",
          "address" : "789 Madison Street",
          "employer" : "Quility",
          "email" : "nanettebates@quility.com",
          "city" : "Nogal",
          "state" : "VA"
        
      
    ]
  ,
  "profile" : 
    "shards" : [
      
        "id" : "[jW8PbSdhTOOpESX13DRBJQ][bank][0]",
        "searches" : [
          
            "query" : [
              
                "type" : "DisjunctionMaxQuery",
                "description" : """(MatchNoDocsQuery("failed [account_number] query, caused by number_format_exception:[For input string: "Nanette"]") | firstname:nanette | address:nanette | gender:nanette | city:nanette | lastname.keyword:Nanette | gender.keyword:Nanette | city.keyword:Nanette | lastname:nanette | email.keyword:Nanette | firstname.keyword:Nanette | employer.keyword:Nanette | MatchNoDocsQuery("failed [balance] query, caused by number_format_exception:[For input string: "Nanette"]") | state.keyword:Nanette | employer:nanette | state:nanette | address.keyword:Nanette | email:nanette | MatchNoDocsQuery("failed [age] query, caused by number_format_exception:[For input string: "Nanette"]"))""",
                "time_in_nanos" : 285257,
                "breakdown" : 
                  "set_min_competitive_score_count" : 0,
                  "match_count" : 0,
                  "shallow_advance_count" : 0,
                  "set_min_competitive_score" : 0,
                  "next_doc" : 1995,
                  "match" : 0,
                  "next_doc_count" : 1,
                  "score_count" : 1,
                  "compute_max_score_count" : 0,
                  "compute_max_score" : 0,
                  "advance" : 8089,
                  "advance_count" : 1,
                  "score" : 6033,
                  "build_scorer_count" : 3,
                  "create_weight" : 157223,
                  "shallow_advance" : 0,
                  "create_weight_count" : 1,
                  "build_scorer" : 111917
                ,
                "children" : [
                  
                    "type" : "MatchNoDocsQuery",
                    "description" : """MatchNoDocsQuery("failed [account_number] query, caused by number_format_exception:[For input string: "Nanette"]")""",
                    "time_in_nanos" : 2115,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 1346,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 769
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "firstname:nanette",
                    "time_in_nanos" : 35870,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 3,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 1,
                      "compute_max_score_count" : 3,
                      "compute_max_score" : 731,
                      "advance" : 863,
                      "advance_count" : 2,
                      "score" : 3138,
                      "build_scorer_count" : 3,
                      "create_weight" : 22778,
                      "shallow_advance" : 896,
                      "create_weight_count" : 1,
                      "build_scorer" : 7464
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "address:nanette",
                    "time_in_nanos" : 7593,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 7119,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 474
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "gender:nanette",
                    "time_in_nanos" : 1900,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 1560,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 340
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "city:nanette",
                    "time_in_nanos" : 11893,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 11525,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 368
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "lastname.keyword:Nanette",
                    "time_in_nanos" : 5070,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 4715,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 355
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "gender.keyword:Nanette",
                    "time_in_nanos" : 1801,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 1526,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 275
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "city.keyword:Nanette",
                    "time_in_nanos" : 7737,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 7355,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 382
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "lastname:nanette",
                    "time_in_nanos" : 4524,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 4178,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 346
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "email.keyword:Nanette",
                    "time_in_nanos" : 1986,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 1661,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 325
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "firstname.keyword:Nanette",
                    "time_in_nanos" : 11564,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 3,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 1,
                      "compute_max_score_count" : 3,
                      "compute_max_score" : 2440,
                      "advance" : 497,
                      "advance_count" : 2,
                      "score" : 264,
                      "build_scorer_count" : 3,
                      "create_weight" : 5632,
                      "shallow_advance" : 455,
                      "create_weight_count" : 1,
                      "build_scorer" : 2276
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "employer.keyword:Nanette",
                    "time_in_nanos" : 6058,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 5688,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 370
                    
                  ,
                  
                    "type" : "MatchNoDocsQuery",
                    "description" : """MatchNoDocsQuery("failed [balance] query, caused by number_format_exception:[For input string: "Nanette"]")""",
                    "time_in_nanos" : 1217,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 479,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 738
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "state.keyword:Nanette",
                    "time_in_nanos" : 4342,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 3525,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 817
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "employer:nanette",
                    "time_in_nanos" : 6783,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 6042,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 741
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "state:nanette",
                    "time_in_nanos" : 3225,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 2904,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 321
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "address.keyword:Nanette",
                    "time_in_nanos" : 2001,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 1614,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 387
                    
                  ,
                  
                    "type" : "TermQuery",
                    "description" : "email:nanette",
                    "time_in_nanos" : 7383,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 7023,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 360
                    
                  ,
                  
                    "type" : "MatchNoDocsQuery",
                    "description" : """MatchNoDocsQuery("failed [age] query, caused by number_format_exception:[For input string: "Nanette"]")""",
                    "time_in_nanos" : 852,
                    "breakdown" : 
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 0,
                      "match" : 0,
                      "next_doc_count" : 0,
                      "score_count" : 0,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 0,
                      "build_scorer_count" : 2,
                      "create_weight" : 410,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 442
                    
                  
                ]
              
            ],
            "rewrite_time" : 4769,
            "collector" : [
              
                "name" : "SimpleTopScoreDocCollector",
                "reason" : "search_top_hits",
                "time_in_nanos" : 15204
              
            ]
          
        ],
        "aggregations" : [ ]
      
    ]
  

二、GET bank/_search?q=+Nanette

q=+Nanette的意思就是搜索一定包含Nanette这个词的文档,相反q=-Nanette的意思就是一定不包含Nanette这个词的文档。
由于q=-Nanette输出的文档过多,这里就不展示了,读者自行测试即可

返回结果:


  "took" : 2,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 1,
      "relation" : "eq"
    ,
    "max_score" : 6.5042877,
    "hits" : [
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "13",
        "_score" : 6.5042877,
        "_source" : 
          "account_number" : 13,
          "balance" : 32838,
          "firstname" : "Nanette",
          "lastname" : "Bates",
          "age" : 28,
          "gender" : "F",
          "address" : "789 Madison Street",
          "employer" : "Quility",
          "email" : "nanettebates@quility.com",
          "city" : "Nogal",
          "state" : "VA"
        
      
    ]
  


三、GET bank/_search?q=firstname:Nanette

GET bank/_search?q=firstname:Nanette
其实就是指定firstname这个字段进行查询

返回结果为:


  "took" : 1,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 1,
      "relation" : "eq"
    ,
    "max_score" : 6.5042877,
    "hits" : [
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "13",
        "_score" : 6.5042877,
        "_source" : 
          "account_number" : 13,
          "balance" : 32838,
          "firstname" : "Nanette",
          "lastname" : "Bates",
          "age" : 28,
          "gender" : "F",
          "address" : "789 Madison Street",
          "employer" : "Quility",
          "email" : "nanettebates@quility.com",
          "city" : "Nogal",
          "state" : "VA"
        
      
    ]
  


四、GET bank/_search?q=address:“Hutchinson Court”

GET bank/_search?q=address:"Hutchinson Court"
加双引号的是Phrase查询,即短语查询,可以通过执行计划看出来,底层使用的是短语查询。
GET bank/_search?q=address:(Hutchinson AND Court)
这里底层进行的是boolean查询,也可以自己通过执行计划看出来。
其中比较重要的一点就是短语查询的话,必须保证词语的顺序,不然查询不出来。
两者都实现了AND功能。但是底层机制是不一样的。

返回结果:


  "took" : 1,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 1,
      "relation" : "eq"
    ,
    "max_score" : 8.628372,
    "hits" : [
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "18",
        "_score" : 8.628372,
        "_source" : 
          "account_number" : 18,
          "balance" : 4180,
          "firstname" : "Dale",
          "lastname" : "Adams",
          "age" : 33,
          "gender" : "M",
          "address" : "467 Hutchinson Court",
          "employer" : "Boink",
          "email" : "daleadams@boink.com",
          "city" : "Orick",
          "state" : "MD"
        
      
    ]
  


五、GET bank/_search?q=address:Hutchinson Court

GET bank/_search?q=address:Hutchinson Court
这里其实是一个term查询,通过执行计划可以看出来
GET bank/_search?q=address:(Hutchinson Court)
这里又是一个boolean查询,
虽然这两个都实现了OR查询,即address字段中包含Hutchinson或者Court,但是两者底层查询的方式是不一样。可以自己执行,
查看他们的具体执行计划

返回结果:


  "took" : 1,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 119,
      "relation" : "eq"
    ,
    "max_score" : 8.628372,
    "hits" : [
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "18",
        "_score" : 8.628372,
        "_source" : 
          "account_number" : 18,
          "balance" : 4180,
          "firstname" : "Dale",
          "lastname" : "Adams",
          "age" : 33,
          "gender" : "M",
          "address" : "467 Hutchinson Court",
          "employer" : "Boink",
          "email" : "daleadams@boink.com",
          "city" : "Orick",
          "state" : "MD"
        
      ,
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "56",
        "_score" : 2.1258574,
        "_source" : 
          "account_number" : 56,
          "balance" : 14992,
          "firstname" : "Josie",
          "lastname" : "Nelson",
          "age" : 32,
          "gender" : "M",
          "address" : "857 Tabor Court",
          "employer" : "Emtrac",
          "email" : "josienelson@emtrac.com",
          "city" : "Sunnyside",
          "state" : "UT"
        
      
    ]
  


六、GET bank/_search?q=address:(Hutchinson NOT Tabor)

GET bank/_search?q=address:(Hutchinson NOT Tabor)
表示包含Hutchinson不包含Tabor

返回结果:


  "took" : 1,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 1,
      "relation" : "eq"
    ,
    "max_score" : 6.5025153,
    "hits" : [
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "18",
        "_score" : 6.5025153,
        "_source" : 
          "account_number" : 18,
          "balance" : 4180,
          "firstname" : "Dale",
          "lastname" : "Adams",
          "age" : 33,
          "gender" : "M",
          "address" : "467 Hutchinson Court",
          "employer" : "Boink",
          "email" : "daleadams@boink.com",
          "city" : "Orick",
          "state" : "MD"
        
      
    ]
  

七、GET bank/_search?q=age:<36

GET bank/_search?q=age:<36
表示的是范围查询,当然范围查询还有另一种形式:
GET bank/_search?q=age:30 TO 36;开区间
GET bank/_search?q=age:[* TO 36];闭区间

返回结果:


  "took" : 1,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 762,
      "relation" : "eq"
    ,
    "max_score" : 1.0,
    "hits" : [
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "13",
        "_score" : 1.0,
        "_source" : 
          "account_number" : 13,
          "balance" : 32838,
          "firstname" : "Nanette",
          "lastname" : "Bates",
          "age" : 28,
          "gender" : "F",
          "address" : "789 Madison Street",
          "employer" : "Quility",
          "email" : "nanettebates@quility.com",
          "city" : "Nogal",
          "state" : "VA"
        
      ,
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "18",
        "_score" : 1.0,
        "_source" : 
          "account_number" : 18,
          "balance" : 4180,
          "firstname" : "Dale",
          "lastname" : "Adams",
          "age" : 33,
          "gender" : "M",
          "address" : "467 Hutchinson Court",
          "employer" : "Boink",
          "email" : "daleadams@boink.com",
          "city" : "Orick",
          "state" : "MD"
        
      
    ]
  

八、GET bank/_search?q=firstname:De*

GET bank/_search?q=firstname:De*
表示的是通配符查询,当然还有?,问号表示的是一个字符,而*表示的是0个或者多个字符

返回结果:


  "took" : 1,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 12,
      "relation" : "eq"
    ,
    "max_score" : 1.0,
    "hits" : [
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "70",
        "_score" : 1.0,
        "_source" : 
          "account_number" : 70,
          "balance" : 38172,
          "firstname" : "Deidre",
          "lastname" : "Thompson",
          "age" : 33,
          "gender" : "F",
          "address" : "685 School Lane",
          "employer" : "Netplode",
          "email" : "deidrethompson@netplode.com",
          "city" : "Chestnut",
          "state" : "GA"
        
      ,
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "102",
        "_score" : 1.0,
        "_source" : 
          "account_number" : 102,
          "balance" : 29712,
          "firstname" : "Dena",
          "lastname" : "Olson",
          "age" : 27,
          "gender" : "F",
          "address" : "759 Newkirk Avenue",
          "employer" : "Hinway",
          "email" : "denaolson@hinway.com",
          "city" : "Choctaw",
          "state" : "NJ"
        
      
    ]
  


九、GET bank/_search?q=firstname:Deidr~2

GET bank/_search?q=firstname:Deidr~2
模糊查询,但是我也不知道这个后面的2是什么意思,看了一圈也没几个解释清楚的,也尝试自己改变了这个值,但是还是捉摸不透,
等后面越来越熟悉了再回来编辑一波。如果有知道的小伙伴,可以在下方留言告知我一声,我直接编辑。

返回结果:


  "took" : 2,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 2,
      "relation" : "eq"
    ,
    "max_score" : 5.2034307,
    "hits" : [
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "70",
        "_score" : 5.2034307,
        "_source" : 
          "account_number" : 70,
          "balance" : 38172,
          "firstname" : "Deidre",
          "lastname" : "Thompson",
          "age" : 33,
          "gender" : "F",
          "address" : "685 School Lane",
          "employer" : "Netplode",
          "email" : "deidrethompson@netplode.com",
          "city" : "Chestnut",
          "state" : "GA"
        
      ,
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "463",
        "_score" : 3.902573,
        "_source" : 
          "account_number" : 463,
          "balance" : 36672,
          "firstname" : "Heidi",
          "lastname" : "Acosta",
          "age" : 20,
          "gender" : "F",
          "address" : "692 Kenmore Terrace",
          "employer" : "Elpro",
          "email" : "heidiacosta@elpro.com",
          "city" : "Ezel",
          "state" : "SD"
        
      
    ]
  


小结

上面的Query String Syntax用法基本都讲到了,下面我们就把上面的基本用法结合一下,然后搜索一下。

GET bank/_search?q=firstname:De~2 AND age:30 TO 36] AND address:Court* AND employer:(Entropix Eplosion Boink) 
AND gender:(NOT M) OR state:AL

返回结果:


  "took" : 2,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 2,
      "relation" : "eq"
    ,
    "max_score" : 12.178019,
    "hits" : [
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "903",
        "_score" : 12.178019,
        "_source" : 
          "account_number" : 903,
          "balance" : 10238,
          "firstname" : "Wade",
          "lastname" : "Page",
          "age" : 35,
          "gender" : "F",
          "address" : "685 Waldorf Court",
          "employer" : "Eplosion",
          "email" : "wadepage@eplosion.com",
          "city" : "Welda",
          "state" : "AL"
        
      ,
      
        "_index" : "bank",
        "_type" : "account",
        "_id" : "761",
        "_score" : 8.506945,
        "_source" : 
          "account_number" : 761,
          "balance" : 7663,
          "firstname" : "Rae",
          "lastname" : "Juarez",
          "age" : 34,
          "gender" : "F",
          "address" : "560 Gilmore Court",
          "employer" : "Entropix",
          "email" : "raejuarez@entropix.com",
          "city" : "Northchase",
          "state" : "ID"
        
      
    ]
  


所以相对来讲Query String Syntax的用法不仅挺多的,而且还非常的灵活的。

基本上关于Query String Syntax的相关的用法大概就这么多吧,本来想用一篇博客把其他参数的用法也学习的,但是看着篇幅,我觉得用一篇来写,可能就太多了,所以后面其他参数的使用我会另外开一篇博客来写。

es 基础概念总结 —— URI Search

搜索 API

GET /<index>/_search

POST /<index>/_search

GET /_search

POST /_search

示例:

# 单个索引:
GET /twitter/_search?q=tag:wow

# 多个索引:
GET /kimchy,elasticsearch/_search?q=user:kimchy

# 全部索引:
GET /_search?q=user:kimchy
GET /_all/_search?q=user:kimchy
GET /*/_search?q=user:kimchy

 

URI Search

GET /<index>/_search?q=<parameter>

指定字段

GET /movies/_search?q=title:2012
{
	"profile":"true"
}

泛查询

GET /movies/_search?q=2012
{
	"profile":"true"
}

phrase 查询

GET /movies/_search?q=title:"Beautiful Mind"
{
	"profile":"true"
}

分组查询(非泛查询

GET /movies/_search?q=title:(Beautiful Mind)
{
	"profile":"true"
}

boolean 查询

GET /movies/_search?q=title:(Beautiful AND Mind)
{
  "profile":"true"
}

GET /movies/_search?q=title:(Beautiful NOT Mind)
{
  "profile":"true"
}

GET /movies/_search?q=title:(Beautiful %2BMind)
{
  "profile":"true"
}

通配符查询

GET /movies/_search?q=title:b*
{
	"profile":"true"
}

模糊查询

GET /movies/_search?q=title:beautifl~1
{
	"profile":"true"
}

GET /movies/_search?q=title:"Lord Rings"~2
{
	"profile":"true"
}

范围查询,区间写法

GET /movies/_search?q=title:beautiful AND year:[2002 TO 2018%7D
{
	"profile":"true"
}

 

 

相关文档


https://www.elastic.co/guide/en/elasticsearch/reference/7.6/search-uri-request.html

 

以上是关于记录es的URI search搜索方式的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch: 使用URI Search

elastic search报错——“failed to obtain node locks”

Elastic Search中filter的理解

ES实战Search的滚动查询(Scroll)

Es学习第六课, ES基本搜索_search

ES中高级检索(Query)