1. 程式人生 > >ElasticSearch36:初識搜尋引擎_快速上機動手實戰Query DSL搜尋語法

ElasticSearch36:初識搜尋引擎_快速上機動手實戰Query DSL搜尋語法

1.match all
GET /website/article/_search
{
  "query": {
    "match_all": {}
  }
}


執行結果:
{
  "took": 38,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "website",
        "_type": "article",
        "_id": "2",
        "_score": 1,
        "_source": {
          "content": "h1 h2"
        }
      },
      {
        "_index": "website",
        "_type": "article",
        "_id": "1",
        "_score": 1,
        "_source": {
          "content": "hl"
        }
      },
      {
        "_index": "website",
        "_type": "article",
        "_id": "3",
        "_score": 1,
        "_source": {
          "content": "h1 h3"
        }
      }
    ]
  }
}




2.match
GET /website/article/_search
{
  "query": {
    "match": {
      "content":"h1 h2"
    }
  }
}

執行結果

{
  "took": 95,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.51623213,
    "hits": [
      {
        "_index": "website",
        "_type": "article",
        "_id": "2",
        "_score": 0.51623213,
        "_source": {
          "content": "h1 h2"
        }
      },
      {
        "_index": "website",
        "_type": "article",
        "_id": "3",
        "_score": 0.25811607,
        "_source": {
          "content": "h1 h3"
        }
      }
    ]
  }
}



3.構造一個需求

must表示必須滿足
shoul表示可以滿足
must_not表示必須不滿足

查詢title必須包含elasticsearch,content可以包含elasticsearch,author_id不是100的資料

構造三條資料:

PUT /website/article/1
{
  "title":"my elasticsearch article1",
  "content":"elasticsearch is good",
  "author_id":10010
}
PUT /website/article/2
{
  "title":"my hadoop article1",
  "content":"hadoop is good",
  "author_id":10010
}
PUT /website/article/3
{
  "title":"my elasticsearch article1",
  "content":"elasticsearch is bad",
  "author_id":100
}

搜尋我們需要的資料。

GET /website/article/_search
{
  "query":{
    "bool": {
      "must": [
        {"match": {
          "title": "elasticsearch"
        }}
      ],
      "should":[
        {"match": {
          "content": "elasticsearch"
        }}
        ],
      "must_not":[
        {"match": {
          "author_id": 100
        }}
        ]
    }
  }
}



執行結果:
{
  "took": 135,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.5063205,
    "hits": [
      {
        "_index": "website",
        "_type": "article",
        "_id": "1",
        "_score": 0.5063205,
        "_source": {
          "title": "my elasticsearch article1",
          "content": "elasticsearch is good",
          "author_id": 10010
        }
      }
    ]
  }
}

更加複雜的DSL查詢,巢狀

GET /text_index/_search
{
	"query":{
		"bool":{
			"must":{
				"match":{
					"name":"tom"
				}
			},
			"should":[    巢狀查詢條件
				{
					"match":{
						"hired":true
					}				
				},
				{
					"bool":{
						{
							"must":{
								"match":{
									"personality":"good"
								}
							},
							"must_not":{
								"match":{
									"rude":true
								}
							}
						}
					}
				}
			],
			"minimum_should_match":1     最少返回的查詢數
		}
	}
}