zoukankan      html  css  js  c++  java
  • ElasticSearch(四):基本搜索

    ElasticSearch(四):基本搜索

    学习课程链接《Elasticsearch核心技术与实战》


    ## URI Search 使用HTTP的GET方法,在URL中使用查询参数进行查询。 ``` GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s { "profile":"true" } ``` * `q`指定查询语句,使用`Query String Syntax` * `df`默认字段,若不指定,会对所有字段进行查询 * `sort`用于排序 * `from`、`size`用于分页 * `profile`用于展示查询是如何被执行的
    #基本查询
    GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
    
    #基本查询返回结果
    {
      "took" : 50,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "relation" : "eq"
        },
        "max_score" : null,
        "hits" : [
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "105254",
            "_score" : null,
            "_source" : {
              "id" : "105254",
              "genre" : [
                "Adventure",
                "Comedy"
              ],
              "title" : "Crystal Fairy & the Magical Cactus and 2012",
              "year" : 2013,
              "@version" : "1"
            },
            "sort" : [
              2013
            ]
          },
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "72378",
            "_score" : null,
            "_source" : {
              "id" : "72378",
              "genre" : [
                "Action",
                "Drama",
                "Sci-Fi",
                "Thriller"
              ],
              "title" : "2012",
              "year" : 2009,
              "@version" : "1"
            },
            "sort" : [
              2009
            ]
          }
        ]
      }
    }
    
    #带profile
    GET /movies/_search?q=2012&df=title
    {
    	"profile":"true"
    }
    #指定字段q=title:2012等价于q=2012&df=title
    GET /movies/_search?q=title:2012&sort=year:desc&from=0&size=10&timeout=1s
    {
    	"profile":"true"
    }
    
    #带profile返回结果
    {
      "took" : 14,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "relation" : "eq"
        },
        "max_score" : 11.303033,
        "hits" : [
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "72378",
            "_score" : 11.303033,
            "_source" : {
              "id" : "72378",
              "genre" : [
                "Action",
                "Drama",
                "Sci-Fi",
                "Thriller"
              ],
              "title" : "2012",#只查询title中的2012
              "year" : 2009,
              "@version" : "1"
            }
          },
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "105254",
            "_score" : 5.2497,
            "_source" : {
              "id" : "105254",
              "genre" : [
                "Adventure",
                "Comedy"
              ],
              "title" : "Crystal Fairy & the Magical Cactus and 2012",
              "year" : 2013,
              "@version" : "1"
            }
          }
        ]
      },
      "profile" : {
        "shards" : [
          {
            "id" : "[u-4S1mfbQiuA1Bqe-wfPJQ][movies][0]",
            "searches" : [
              {
                "query" : [
                  {
                    "type" : "TermQuery",
                    "description" : "title:2012",
                    "time_in_nanos" : 1105745,
                    "breakdown" : {
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 0,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 7966,
                      "match" : 0,
                      "next_doc_count" : 4,
                      "score_count" : 2,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 133876,
                      "build_scorer_count" : 9,
                      "create_weight" : 187971,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 775916
                    }
                  }
                ],
                "rewrite_time" : 4990,
                "collector" : [
                  {
                    "name" : "CancellableCollector",
                    "reason" : "search_cancelled",
                    "time_in_nanos" : 574426,
                    "children" : [
                      {
                        "name" : "SimpleTopScoreDocCollector",
                        "reason" : "search_top_hits",
                        "time_in_nanos" : 158099
                      }
                    ]
                  }
                ]
              }
            ],
            "aggregations" : [ ]
          }
        ]
      }
    }
    
    #泛查询,正对_all,所有字段,查询所有字段中的2012
    GET /movies/_search?q=2012
    {
    	"profile":"true"
    }
    
    #使用引号,Phrase查询,还要求前后顺序保持一致,"Beautiful Mind"等效于Beautiful AND Mind 
    GET /movies/_search?q=title:"Beautiful Mind"
    {
    	"profile":"true"
    }
    
    #Phrase查询返回结果
    {
      "took" : 16,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 13.68748,
        "hits" : [
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "4995",
            "_score" : 13.68748,
            "_source" : {
              "id" : "4995",
              "genre" : [
                "Drama",
                "Romance"
              ],
              "title" : "Beautiful Mind, A", #查询条件
              "year" : 2001,
              "@version" : "1"
            }
          }
        ]
      },
      "profile" : {
        "shards" : [
          {
            "id" : "[u-4S1mfbQiuA1Bqe-wfPJQ][movies][0]",
            "searches" : [
              {
                "query" : [
                  {
                    "type" : "PhraseQuery", #查询类型为PhraseQuery
                    "description" : """title:"beautiful mind"""",
                    "time_in_nanos" : 10670089,
                    "breakdown" : {
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 1,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 28250,
                      "match" : 27343,
                      "next_doc_count" : 3,
                      "score_count" : 1,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 9171,
                      "build_scorer_count" : 9,
                      "create_weight" : 7583491,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 3021819
                    }
                  }
                ],
                "rewrite_time" : 7818,
                "collector" : [
                  {
                    "name" : "CancellableCollector",
                    "reason" : "search_cancelled",
                    "time_in_nanos" : 39950,
                    "children" : [
                      {
                        "name" : "SimpleTopScoreDocCollector",
                        "reason" : "search_top_hits",
                        "time_in_nanos" : 25137
                      }
                    ]
                  }
                ]
              }
            ],
            "aggregations" : [ ]
          }
        ]
      }
    }
    
    # Term查询为泛查询,(Beautiful Mind)等效于Beautiful OR Mind
    GET /movies/_search?q=title:(Beautiful Mind)
    {
    	"profile":"true"
    }
    
    {
      "took" : 10,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 20,
          "relation" : "eq"
        },
        "max_score" : 13.687479,
        "hits" : [
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "4995",
            "_score" : 13.687479,
            "_source" : {
              "id" : "4995",
              "genre" : [
                "Drama",
                "Romance"
              ],
              "title" : "Beautiful Mind, A",
              "year" : 2001,
              "@version" : "1"
            }
          },
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "3912",
            "_score" : 8.723258,
            "_source" : {
              "id" : "3912",
              "genre" : [
                "Comedy",
                "Drama"
              ],
              "title" : "Beautiful",
              "year" : 2000,
              "@version" : "1"
            }
          },
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "47404",
            "_score" : 8.576847,
            "_source" : {
              "id" : "47404",
              "genre" : [
                "Adventure",
                "Animation",
                "Comedy",
                "Fantasy",
                "Romance",
                "Sci-Fi"
              ],
              "title" : "Mind Game",
              "year" : 2004,
              "@version" : "1"
            }
          },
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "1046",
            "_score" : 7.317063,
            "_source" : {
              "id" : "1046",
              "genre" : [
                "Drama",
                "Romance"
              ],
              "title" : "Beautiful Thing",
              "year" : 1996,
              "@version" : "1"
            }
          },
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "3302",
            "_score" : 7.317063,
            "_source" : {
              "id" : "3302",
              "genre" : [
                "Comedy"
              ],
              "title" : "Beautiful People",
              "year" : 1999,
              "@version" : "1"
            }
          },
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "4242",
            "_score" : 7.317063,
            "_source" : {
              "id" : "4242",
              "genre" : [
                "Comedy",
                "Crime",
                "Drama",
                "Thriller"
              ],
              "title" : "Beautiful Creatures",
              "year" : 2000,
              "@version" : "1"
            }
          },
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "4372",
            "_score" : 7.317063,
            "_source" : {
              "id" : "4372",
              "genre" : [
                "Drama",
                "Romance"
              ],
              "title" : "Crazy/Beautiful",
              "year" : 2001,
              "@version" : "1"
            }
          },
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "94",
            "_score" : 7.317063,
            "_source" : {
              "id" : "94",
              "genre" : [
                "Comedy",
                "Drama",
                "Romance"
              ],
              "title" : "Beautiful Girls",
              "year" : 1996,
              "@version" : "1"
            }
          },
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "90353",
            "_score" : 7.317063,
            "_source" : {
              "id" : "90353",
              "genre" : [
                "Drama"
              ],
              "title" : "Beautiful Boy",
              "year" : 2010,
              "@version" : "1"
            }
          },
          {
            "_index" : "movies",
            "_type" : "_doc",
            "_id" : "100487",
            "_score" : 7.317063,
            "_source" : {
              "id" : "100487",
              "genre" : [
                "Drama",
                "Fantasy",
                "Romance"
              ],
              "title" : "Beautiful Creatures",
              "year" : 2013,
              "@version" : "1"
            }
          }
        ]
      },
      "profile" : {
        "shards" : [
          {
            "id" : "[u-4S1mfbQiuA1Bqe-wfPJQ][movies][0]",
            "searches" : [
              {
                "query" : [
                  {
                    "type" : "BooleanQuery",
                    "description" : "title:beautiful title:mind",
                    "time_in_nanos" : 963305,
                    "breakdown" : {
                      "set_min_competitive_score_count" : 0,
                      "match_count" : 6,
                      "shallow_advance_count" : 0,
                      "set_min_competitive_score" : 0,
                      "next_doc" : 133259,
                      "match" : 2086,
                      "next_doc_count" : 27,
                      "score_count" : 20,
                      "compute_max_score_count" : 0,
                      "compute_max_score" : 0,
                      "advance" : 0,
                      "advance_count" : 0,
                      "score" : 32590,
                      "build_scorer_count" : 14,
                      "create_weight" : 401324,
                      "shallow_advance" : 0,
                      "create_weight_count" : 1,
                      "build_scorer" : 393978
                    },
                    "children" : [
                      {
                        "type" : "TermQuery",
                        "description" : "title:beautiful",
                        "time_in_nanos" : 492649,
                        "breakdown" : {
                          "set_min_competitive_score_count" : 0,
                          "match_count" : 0,
                          "shallow_advance_count" : 6,
                          "set_min_competitive_score" : 0,
                          "next_doc" : 49055,
                          "match" : 0,
                          "next_doc_count" : 15,
                          "score_count" : 16,
                          "compute_max_score_count" : 6,
                          "compute_max_score" : 85498,
                          "advance" : 6172,
                          "advance_count" : 6,
                          "score" : 20213,
                          "build_scorer_count" : 17,
                          "create_weight" : 255976,
                          "shallow_advance" : 4730,
                          "create_weight_count" : 1,
                          "build_scorer" : 70938
                        }
                      },
                      {
                        "type" : "TermQuery",
                        "description" : "title:mind",
                        "time_in_nanos" : 187326,
                        "breakdown" : {
                          "set_min_competitive_score_count" : 0,
                          "match_count" : 0,
                          "shallow_advance_count" : 6,
                          "set_min_competitive_score" : 0,
                          "next_doc" : 2447,
                          "match" : 0,
                          "next_doc_count" : 4,
                          "score_count" : 5,
                          "compute_max_score_count" : 6,
                          "compute_max_score" : 11373,
                          "advance" : 4506,
                          "advance_count" : 5,
                          "score" : 5483,
                          "build_scorer_count" : 15,
                          "create_weight" : 120204,
                          "shallow_advance" : 3324,
                          "create_weight_count" : 1,
                          "build_scorer" : 39947
                        }
                      }
                    ]
                  }
                ],
                "rewrite_time" : 16452,
                "collector" : [
                  {
                    "name" : "CancellableCollector",
                    "reason" : "search_cancelled",
                    "time_in_nanos" : 85953,
                    "children" : [
                      {
                        "name" : "SimpleTopScoreDocCollector",
                        "reason" : "search_top_hits",
                        "time_in_nanos" : 60755
                      }
                    ]
                  }
                ]
              }
            ],
            "aggregations" : [ ]
          }
        ]
      }
    }
    
    
    #布尔操作符
    # 必须包括Beautiful 和 Mind
    GET /movies/_search?q=title:(Beautiful AND Mind)
    {
    	"profile":"true"
    }
    # 必须包括Beautiful 但不能包括 Mind
    GET /movies/_search?q=title:(Beautiful NOT Mind)
    {
    	"profile":"true"
    }
    #‘%2B’即为‘+’号表示必须包括包括 Mind
    GET /movies/_search?q=title:(Beautiful %2BMind)
    {
    	"profile":"true"
    }
    #范围查询 ,区间写法年份在2002~2018
    GET /movies/_search?q=title:beautiful AND year:[2002 TO 2018]
    {
    	"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"
    }
    

    ## Request Body Search 使用Elasticsearch提供的,基于JSON格式的更加完备的Query Domain Specific Language (DSL)

    file

    #query查询
    #ignore_unavailable=true,可以忽略尝试访问不存在的索引“404_idx”导致的报错
    POST /movies,404_idx/_search?ignore_unavailable=true
    {
      "profile": true,
    	"query": {
    		"match_all": {}
    	}
    }
    
    #查询movies分页
    POST /movies/_search
    {
      "from":10,
      "size":20,
      "query":{
        "match_all": {}
      }
    }
    
    
    #对日期排序
    POST kibana_sample_data_ecommerce/_search
    {
      "sort":[{"order_date":"desc"}],
      "query":{
        "match_all": {}
      }
    
    }
    
    #_source 过滤显示的字段
    POST kibana_sample_data_ecommerce/_search
    {
      "_source":["order_date"],#返回结果只显示"order_date"
      "query":{
        "match_all": {}
      }
    }
    
    #脚本字段
    GET kibana_sample_data_ecommerce/_search
    {
      "script_fields": {
        "new_field": {
          "script": {
            "lang": "painless",
            "source": "doc['order_date'].value+'hello'"
          }
        }
      },
      "query": {
        "match_all": {}
      }
    }
    
    #match查询,last  OR christmas
    POST movies/_search
    {
      "query": {
        "match": {
          "title": "last christmas"
        }
      }
    }
    
    #match查询,last  AND christmas
    POST movies/_search
    {
      "query": {
        "match": {
          "title": {
            "query": "last christmas",
            "operator": "and"
          }
        }
      }
    }
    
    #match_phrase查询,one AND love,且顺序不能乱
    POST movies/_search
    {
      "query": {
        "match_phrase": {
          "title":{
            "query": "one love"#不可以查出 "title" : "One I Love, The",
    
          }
        }
      }
    }
    
    #match_phrase查询,slop在one love中间插入指定数量单词
    POST movies/_search
    {
      "query": {
        "match_phrase": {
          "title":{
            "query": "one love",#可以查出 "title" : "One I Love, The",
            "slop": 1
    
          }
        }
      }
    }
    
    
    
    PUT /users/_doc/1
    {
      "name":"Ruan Yiming",
      "about":"java, golang, node, swift, elasticsearch"
    }
    
    PUT /users/_doc/2
    {
      "name":"Li Yiming",
      "about":"Hadoop"
    }
    
    #query_string查询
    POST users/_search
    {
      "query": {
        "query_string": {
          "default_field": "name",
          "query": "Ruan AND Yiming"
        }
      }
    }
    
    #query_string查询
    POST users/_search
    {
      "query": {
        "query_string": {
          "fields":["name","about"],
          "query": "(Ruan AND Yiming) OR (Java AND Elasticsearch)"
        }
      }
    }
    
    
    #Simple Query 类似query_string查询,但会忽略错误的语法,同时只支持部分查询语法;默认的operator是 Or,可以指定;不支持AND OR NOT,会当字符串处理;支持部分逻辑+替代AND,|替代OR,-替代NOT
    POST users/_search
    {
      "query": {
        "simple_query_string": {
          "query": "Ruan AND Yiming",
          "fields": ["name"]
        }
      }
    }
    
    #Simple Query 
    POST users/_search
    {
      "query": {
        "simple_query_string": {
          "query": "Ruan Yiming",
          "fields": ["name"],
          "default_operator": "AND"
        }
      }
    }
    
    
    GET /movies/_search
    {
    	"profile": true,
    	"query":{
    		"query_string":{
    			"default_field": "title",
    			"query": "Beafiful AND Mind"
    		}
    	}
    }
    
    
    # 多fields
    GET /movies/_search
    {
    	"profile": true,
    	"query":{
    		"query_string":{
    			"fields":[
    				"title",
    				"year"
    			],
    			"query": "2012"
    		}
    	}
    }
    
    
    
    GET /movies/_search
    {
    	"profile":true,
    	"query":{
    		"simple_query_string":{
    			"query":"Beautiful +mind",
    			"fields":["title"]
    		}
    	}
    }
    
    
    
    


    作者:牧汜
    出处:http://www.cnblogs.com/czbxdd/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    Alpha冲刺(5/10)
    Alpha冲刺(4/10)
    团队现场编程
    关于合同标准形的专题讨论
    关于相似标准形的专题讨论
    关于数学分析的计算题I(积分)
    关于数学分析的证明题III(级数)
    关于数学分析的证明题II(微分)
    关于数学分析的证明题I(积分)
    关于反常积分收敛的专题讨论
  • 原文地址:https://www.cnblogs.com/czbxdd/p/11672194.html
Copyright © 2011-2022 走看看