ElasticSearch - 简介及常用查询语句

2020-08-12  本文已影响0人  nlpming

1 ElasticSearch简介

ElasticSearch是一个基于Lucene库的分布式全文搜索引擎。提供Restful API操作接口,很方便使用。ElasticSearch是与名为Logstash的数据收集和日志解析引擎、以及名为Kibnana的分析和可视化平台一起开发的。这三个产品被设计成一个集成解决方案,称为“Elastic Stack”;ElasticSearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。

1.1 ElasticSearch基本概念

1.2 ElasticSearch 分析器 【重点关注

1.2.1 内置分析器

1.2.2 什么时候使用分析器

2 ES常用查询语句

2.0 ES查询结果解释

{
  "took":2,
  "timed_out":false,
  "_shards":{"total":5,"successful":5,"failed":0},
  "hits":{
    "total":2,
    "max_score":1.0,
    "hits":[
      {
        "_index":"accounts",
        "_type":"person",
        "_id":"AV3qGfrC6jMbsbXb6k1p",
        "_score":1.0,
        "_source": {
          "user": "李四",
          "title": "工程师",
          "desc": "系统管理"
        }
      },
      {
        "_index":"accounts",
        "_type":"person",
        "_id":"1",
        "_score":1.0,
        "_source": {
          "user" : "张三",
          "title" : "工程师",
          "desc" : "数据库管理,软件开发"
        }
      }
    ]
  }
}

2.1 基础查询

2.2 精确值查询

GET xxxxx/_search
{
  "query": {
    "term": {
      "poi_id": "xxxxx"
    }
  }
}
GET xxxxx/_search
{
  "query": {
    "term": {
      "poi_id": "xxxxx"
    }
  },
  "_source": [ "name", "address"]
}
GET xxxxx/_search
{
  "query": {
    "terms": {
      "name": ["xxxxx", "xxxxx"]
    }
  }
}

2.3 布尔查询

GET xxxxx/_search
{
  "query": {
    "bool": {
      "must":     { "match": { "title": "quick" }},
      "must_not": { "match": { "title": "lazy"  }},
      "should": [
                  { "match": { "title": "brown" }},
                  { "match": { "title": "dog"   }}
      ]
    }
  }
}
{
    "match": {
        "title": {
            "query":    "brown fox",
            "operator": "and"
        }
    }
}

{
  "bool": {
    "must": [
      { "term": { "title": "brown" }},
      { "term": { "title": "fox"   }}
    ]
  }
}

2.4 范围查询

GET xxxxx/_search
{
  "query": {
    "range" : {
        "click_score" : {
          "gte" : xxx,  
          "lt"  : xxx
        }
    }
  }
}

2.5 基于词项与基于全文查询

2.6 匹配查询

GET xxxxx/_search
{
    "from": 0,
    "query": {
        "match": {
            "name": {
                "operator": "and",
                "query": "xxxxx xxxxx"
            }
        }
    },
    "size": 20,
    "sort": [
        {
            "click_score": {
                "order": "desc"
            }
        }
    ]
}

2.7 multi_match查询

GET xxxxx/_search
{
    "query": {
        "multi_match" : {
            "query" : "guide",
            "fields" : ["title", "authors", "summary", "publish_date", "num_reviews", "publisher"]
        }
    }
}
GET xxxxx/_search
{
    "query": {
        "multi_match" : {
            "query" : "elasticsearch guide",
            "fields": ["title", "summary^3"]
        }
    },
    "_source": ["title", "summary", "publish_date"]
}

2.8 模糊查询

GET xxxxx/_search
{
    "query": {
        "multi_match" : {
            "query" : "comprihensiv guide",
            "fields": ["title", "summary"],
            "fuzziness": "AUTO"
        }
    },
    "_source": ["title", "summary", "publish_date"],
    "size": 1
}

2.9 短句查询

GET xxxxx/_search
{
    "query": {
        "match_phrase_prefix" : {
            "summary": {
                "query": "search en",
                "slop": 3,
                "max_expansions": 10
            }
        }
    },
    "_source": [ "title", "summary", "publish_date" ]
}

2.10 地理查询

GET xxxxx/_search
{
  "query":{
    "filtered": {
      "query":{
        "bool":{
          "must":[
            {
              "match":{
                "name": {
                  "query": "xxxxx xxxxx xxxxx",
                  "operator": "and"
                }
              }
            },
            {
              "match":{
                "category_code": "xxxxx"
              }
            }
          ]
        }
      },
      "filter": {
        "geo_distance": {
          "distance": "1km",
          "location": {
            "lon": xxxxx,
            "lat": xxxxx
          }
        }
      }
    }
  },
  "size": 10,
  "from": 0,
  "terminate_after": 200000
}

2.11 相关度函数

GET xxxxx/_search
{
    "query": {
        "function_score": {
            "query": {
                "multi_match" : {
                    "query" : "search engine",
                    "fields": ["title", "summary"]
                }
            },
            "field_value_factor": {
                "field" : "num_reviews",
                "modifier": "log1p",
                "factor" : 2
            }
        }
    },
    "_source": ["title", "summary", "publish_date", "num_reviews"]
}

参考资料

Elasticsearch: 权威指南

上一篇 下一篇

猜你喜欢

热点阅读