ElasticSearch(四):基本搜索

ElasticSearch(四):基本搜索

學習課程連接《Elasticsearch核心技術與實戰》java

<br/> ## 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`用於展現查詢是如何被執行的node

#基本查詢
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"
}

<br/> ## Request Body Search 使用Elasticsearch提供的,基於JSON格式的更加完備的Query Domain Specific Language (DSL)golang

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"]
		}
	}
}
相關文章
相關標籤/搜索