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     最少返回的查询数
		}
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值