1.match all
执行结果:
2.match
执行结果
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": {
"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 最少返回的查询数
}
}
}