[toc]


实践007-elasticsearch查询之2-Request Body与DSL查询

1. The query statement is sent to ES through HTTP Request Body

 POST movies/_search?ignore_unavailable=true
{
  "profile": "true",
  "query": {
    "match_all": {}
  }
}

2. Paging: from+size

 POST movies/_search?ignore_unavailable=true
{
  "profile": "true",
  "from": 0,
  "size": 2, 
  "query": {
    "match_all": {}
  }
}

3. Sort: sort

 POST movies/_search?ignore_unavailable=true
{
  "profile": "true",
  "from": 0,
  "size": 5,
  "sort": [
    {
      "year": "desc"
    },
    {
      "title.keyword": "desc"
    }
  ],
  "query": {
    "match_all": {}
  }
}

4. Source field filtering: only return the required columns

 POST movies/_search?ignore_unavailable=true
{
  "profile": "true",
  "_source": [
    "id",
    "title",
    "year"
  ],
  "from": 0,
  "size": 5,
  "sort": [
    {
      "year": "desc"
    },
    {
      "title.keyword": "desc"
    }
  ],
  "query": {
    "match_all": {}
  }
}
Wildcard *: _"source": [ "name*", "desc\*" ]

5. script_fields script field: self-processing result

 POST movies/_search
{
  "from": 0,
  "size": 5,
  "sort": [
    {
      "year": {
        "order": "desc"
      }
    },
    {
      "title.keyword": {
        "order": "desc"
      }
    }
  ],
  "script_fields": {
    "year_and_title": {
      "script": {
        "lang": "painless",
        "source": "doc['year'].value + '->' + doc['title.keyword'].value"
      }
    }
  },
  "query": {
    "match_all": {}
  }
}

title=When We First Met; year=2018;

year_and_title=year->title

But it seems that the _source data is gone:

 {
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 9743,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "187717",
        "_score" : null,
        "fields" : {
          "year_and_title" : [
            "2018->Won't You Be My Neighbor?"
          ]
        },
        "sort" : [
          2018,
          "Won't You Be My Neighbor?"
        ]
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "184015",
        "_score" : null,
        "fields" : {
          "year_and_title" : [
            "2018->When We First Met"
          ]
        },
        "sort" : [
          2018,
          "When We First Met"
        ]
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "184471",
        "_score" : null,
        "fields" : {
          "year_and_title" : [
            "2018->Tomb Raider"
          ]
        },
        "sort" : [
          2018,
          "Tomb Raider"
        ]
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "183959",
        "_score" : null,
        "fields" : {
          "year_and_title" : [
            "2018->Tom Segura: Disgraceful"
          ]
        },
        "sort" : [
          2018,
          "Tom Segura: Disgraceful"
        ]
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "188833",
        "_score" : null,
        "fields" : {
          "year_and_title" : [
            "2018->The Man Who Killed Don Quixote"
          ]
        },
        "sort" : [
          2018,
          "The Man Who Killed Don Quixote"
        ]
      }
    ]
  }
}

6. match query expression: TermQuery

6.1 TermQuery

 POST movies/_search
{
  "query": {
    "match": {
      "title": "beautiful mind"
    }
  }
}

Results: including beautiful (16), or including mind (5), total (20):

 {
  "took" : 0,
  "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",
          "@version" : "1",
          "year" : 2001
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "3912",
        "_score" : 8.723258,
        "_source" : {
          "id" : "3912",
          "genre" : [
            "Comedy",
            "Drama"
          ],
          "title" : "Beautiful",
          "@version" : "1",
          "year" : 2000
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "47404",
        "_score" : 8.576847,
        "_source" : {
          "id" : "47404",
          "genre" : [
            "Adventure",
            "Animation",
            "Comedy",
            "Fantasy",
            "Romance",
            "Sci-Fi"
          ],
          "title" : "Mind Game",
          "@version" : "1",
          "year" : 2004
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "1046",
        "_score" : 7.317063,
        "_source" : {
          "id" : "1046",
          "genre" : [
            "Drama",
            "Romance"
          ],
          "title" : "Beautiful Thing",
          "@version" : "1",
          "year" : 1996
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "94",
        "_score" : 7.317063,
        "_source" : {
          "id" : "94",
          "genre" : [
            "Comedy",
            "Drama",
            "Romance"
          ],
          "title" : "Beautiful Girls",
          "@version" : "1",
          "year" : 1996
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "4242",
        "_score" : 7.317063,
        "_source" : {
          "id" : "4242",
          "genre" : [
            "Comedy",
            "Crime",
            "Drama",
            "Thriller"
          ],
          "title" : "Beautiful Creatures",
          "@version" : "1",
          "year" : 2000
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "4372",
        "_score" : 7.317063,
        "_source" : {
          "id" : "4372",
          "genre" : [
            "Drama",
            "Romance"
          ],
          "title" : "Crazy/Beautiful",
          "@version" : "1",
          "year" : 2001
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "3302",
        "_score" : 7.317063,
        "_source" : {
          "id" : "3302",
          "genre" : [
            "Comedy"
          ],
          "title" : "Beautiful People",
          "@version" : "1",
          "year" : 1999
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "90353",
        "_score" : 7.317063,
        "_source" : {
          "id" : "90353",
          "genre" : [
            "Drama"
          ],
          "title" : "Beautiful Boy",
          "@version" : "1",
          "year" : 2010
        }
      },
      {
        "_index" : "movies",
        "_type" : "_doc",
        "_id" : "100487",
        "_score" : 7.317063,
        "_source" : {
          "id" : "100487",
          "genre" : [
            "Drama",
            "Fantasy",
            "Romance"
          ],
          "title" : "Beautiful Creatures",
          "@version" : "1",
          "year" : 2013
        }
      }
    ]
  }
}

6.2 TermQuery+AND

 POST movies/_search
{
  "query": {
    "match": {
      "title": {
        "query": "beautiful mind",
        "operator": "and"
      }
    }
  }
}
  • operator: and/or/not restricts the logical relationship to the entry of "query", and after adding and , there is only one record:
 {
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "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",
          "@version" : "1",
          "year" : 2001
        }
      }
    ]
  }
}

7.match_phrase query expression: PhraseQuery

7.1 PhraseQuery

 POST movies/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "beautiful mind"
      }
    }
  }
}
#// 或者下面形式都可
POST movies/_search
{
  "query": {
    "match_phrase": {
      "title": "beautiful mind"
    }
  }
}

There is only one result, because it is PhraseQuery: both word segmentation and order are required;

7.2 PhraseQuery+slop

  • slop /sl>p/ n. Dirty water, bland and tasteless semi-liquid food; swill
 POST movies/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "friday part",
        "slop": 2
      }
    }
  }
}

slop=2, which is to allow friday and part to mix 2 words (irrelevant arbitrary words) in the middle


丰木
322 声望19 粉丝

遇见超乎想象的自己!