欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Elasticsearch (九)搜索排序和高亮

程序员文章站 2022-07-05 14:25:53
...

准备测试案例:

DELETE  blog
PUT blog
GET blog/_mapping
PUT blog/article/_mapping
{
 "dynamic":"strict", 
 "properties": {
   "id":{
     "type": "long"
   },
   "title":{
     "type": "text",
     "analyzer": "ik_max_word",
     "search_analyzer": "ik_max_word"
   },
   "content":{
     "type": "text",
     "analyzer": "ik_max_word",
     "search_analyzer": "ik_max_word"
   },
   "postdate":{
     "type": "date",
     "format": "yyyy-MM-dd HH:mm || yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
   }
   
 } 
  
}

PUT blog/article/1
{
  "id":1,
  "title":"git简介",
  "content":"Svn与Git的最主要区别。",
  "postdate":"2016-06-09"
}

PUT blog/article/2
{
  "id":2,
  "title":"Java中泛型的介绍与简单应用",
  "content":"学习目标 掌握泛型的产生意义。",
  "postdate":"2016-08-06"
}

PUT blog/article/3
{
  "id":3,
  "title":"SQL的基本操作",
  "content":"基本操作 CRUD。",
  "postdate":"2017-05-06"
}

PUT blog/article/4
{
  "id":4,
  "title":"Hibernate框架基础",
  "content":"Hibernate框架基础。",
  "postdate":"2017-08-03"
}

PUT blog/article/5
{
  "id":5,
  "title":"Shell基本知识",
  "content":"Shell是什么。",
  "postdate":"2017-11-03"
}

 

1.match_all 查询

查询一个索引中的全部文档

GET blog/_search   与 

GET blog/_search
{
"query":{
"match_all":{}
}
}
查询结果相同

查询结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
      {
        "_index": "blog",
        "_type": "article",
        "_id": "5",
        "_score": 1,
        "_source": {
          "id": 5,
          "title": "Shell基本知识",
          "content": "Shell是什么。",
          "postdate": "2017-11-03"
        }
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "2",
        "_score": 1,
        "_source": {
          "id": 2,
          "title": "Java中泛型的介绍与简单应用",
          "content": "学习目标 掌握泛型的产生意义。",
          "postdate": "2016-08-06"
        }
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "4",
        "_score": 1,
        "_source": {
          "id": 4,
          "title": "Hibernate框架基础",
          "content": "Hibernate框架基础。",
          "postdate": "2017-08-03"
        }
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "1",
        "_score": 1,
        "_source": {
          "id": 1,
          "title": "git简介",
          "content": "Svn与Git的最主要区别。",
          "postdate": "2016-06-09"
        }
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "3",
        "_score": 1,
        "_source": {
          "id": 3,
          "title": "SQL的基本操作",
          "content": "基本操作 CRUD。",
          "postdate": "2017-05-06"
        }
      }
    ]
  }
}

2.term 查询

词项查询,需要精确匹配

GET blog/_search
{
  "query": {
    "term": {
      "title": {
        "value": "java"
      }
    }
  }
}

查询结果:

{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.561078,
    "hits": [
      {
        "_index": "blog",
        "_type": "article",
        "_id": "2",
        "_score": 0.561078,
        "_source": {
          "id": 2,
          "title": "Java中泛型的介绍与简单应用",
          "content": "学习目标 掌握泛型的产生意义。",
          "postdate": "2016-08-06"
        }
      }
    ]
  }
}

3.terms 查询

多词项查询,精确匹配,字段中包含其中一个即可term即可匹配

GET blog/_search
{
  "query": {
    "terms": {
      "title": [
        "java",
        "shell"
      ]
    }
  }
}

查询结果:

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.561078,
    "hits": [
      {
        "_index": "blog",
        "_type": "article",
        "_id": "2",
        "_score": 0.561078,
        "_source": {
          "id": 2,
          "title": "Java中泛型的介绍与简单应用",
          "content": "学习目标 掌握泛型的产生意义。",
          "postdate": "2016-08-06"
        }
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "5",
        "_score": 0.2876821,
        "_source": {
          "id": 5,
          "title": "Shell基本知识",
          "content": "Shell是什么。",
          "postdate": "2017-11-03"
        }
      }
    ]
  }
}

4.查询高亮

词项查询,精确匹配,并高亮

GET blog/_search
{
  "query": {
    "term": {
      "title": {
        "value": "java"
      }
    }
  },
  "highlight": {
    "fields": {
      "title": {}
    }
  }
}

查询结果:

{
  "took": 98,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.561078,
    "hits": [
      {
        "_index": "blog",
        "_type": "article",
        "_id": "2",
        "_score": 0.561078,
        "_source": {
          "id": 2,
          "title": "Java中泛型的介绍与简单应用",
          "content": "学习目标 掌握泛型的产生意义。",
          "postdate": "2016-08-06"
        },
        "highlight": {
          "title": [
            "<em>Java</em>中泛型的介绍与简单应用"
          ]
        }
      }
    ]
  }
}

5.match 查询

对查询语句分词,再term查询

GET blog/_search
{
  "query": {
    "match": {
      "title": "java框架"
    }
  },
  "highlight": {
    "fields": {
      "title": {}
    }
  }
}

查询结果:

{
  "took": 32,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1.5072953,
    "hits": [
      {
        "_index": "blog",
        "_type": "article",
        "_id": "4",
        "_score": 1.5072953,
        "_source": {
          "id": 4,
          "title": "Hibernate框架基础",
          "content": "Hibernate框架基础。",
          "postdate": "2017-08-03"
        },
        "highlight": {
          "title": [
            "Hibernate<em>框架</em>基础"
          ]
        }
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "2",
        "_score": 0.561078,
        "_source": {
          "id": 2,
          "title": "Java中泛型的介绍与简单应用",
          "content": "学习目标 掌握泛型的产生意义。",
          "postdate": "2016-08-06"
        },
        "highlight": {
          "title": [
            "<em>Java</em>中泛型的介绍与简单应用"
          ]
        }
      }
    ]
  }
}

6.multi_match查询

对查询语句分词,再term查询,fields中任何一个字段包含分词后的term都可以被匹配

GET blog/_search
{
  "query": {
    "multi_match": {
      "query": "java svn",
      "fields": ["title","content"]
    }
  },
  "highlight": {
    "fields": {
      "title": {}
    }
  }
}

查询结果:

{
  "took": 22,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.561078,
    "hits": [
      {
        "_index": "blog",
        "_type": "article",
        "_id": "2",
        "_score": 0.561078,
        "_source": {
          "id": 2,
          "title": "Java中泛型的介绍与简单应用",
          "content": "学习目标 掌握泛型的产生意义。",
          "postdate": "2016-08-06"
        },
        "highlight": {
          "title": [
            "<em>Java</em>中泛型的介绍与简单应用"
          ]
        }
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "1",
        "_score": 0.26742277,
        "_source": {
          "id": 1,
          "title": "git简介",
          "content": "Svn与Git的最主要区别。",
          "postdate": "2016-06-09"
        }
      }
    ]
  }
}

7.range 查询 

范围查询   gte 大于或等于,gt 大于,lte 小于或等于,lt 小于

GET blog/_search
{
  "query": {
    "range": {
      "postdate": {
        "gte": "2016-01-01",
        "lte": "2016-12-31"
      }
    }
  }
}

查询结果:

{
  "took": 31,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "blog",
        "_type": "article",
        "_id": "2",
        "_score": 1,
        "_source": {
          "id": 2,
          "title": "Java中泛型的介绍与简单应用",
          "content": "学习目标 掌握泛型的产生意义。",
          "postdate": "2016-08-06"
        }
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "1",
        "_score": 1,
        "_source": {
          "id": 1,
          "title": "git简介",
          "content": "Svn与Git的最主要区别。",
          "postdate": "2016-06-09"
        }
      }
    ]
  }
}

8.搜索排序

按发布日期降序排序

GET blog/_search

GET blog/_search
{
"query":{
"match_all":{}
},
"sort": [
  {
    "postdate": {
      "order": "desc"
    }
  }
]
}

查询结果:

{
  "took": 22,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": null,
    "hits": [
      {
        "_index": "blog",
        "_type": "article",
        "_id": "5",
        "_score": null,
        "_source": {
          "id": 5,
          "title": "Shell基本知识",
          "content": "Shell是什么。",
          "postdate": "2017-11-03"
        },
        "sort": [
          1509667200000
        ]
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "4",
        "_score": null,
        "_source": {
          "id": 4,
          "title": "Hibernate框架基础",
          "content": "Hibernate框架基础。",
          "postdate": "2017-08-03"
        },
        "sort": [
          1501718400000
        ]
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "3",
        "_score": null,
        "_source": {
          "id": 3,
          "title": "SQL的基本操作",
          "content": "基本操作 CRUD。",
          "postdate": "2017-05-06"
        },
        "sort": [
          1494028800000
        ]
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "2",
        "_score": null,
        "_source": {
          "id": 2,
          "title": "Java中泛型的介绍与简单应用",
          "content": "学习目标 掌握泛型的产生意义。",
          "postdate": "2016-08-06"
        },
        "sort": [
          1470441600000
        ]
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "1",
        "_score": null,
        "_source": {
          "id": 1,
          "title": "git简介",
          "content": "Svn与Git的最主要区别。",
          "postdate": "2016-06-09"
        },
        "sort": [
          1465430400000
        ]
      }
    ]
  }
}