Elasticsearch-词条级搜索(term-level queries)

词条级搜索(term-level queries)

可以使用term-level queries根据结构化数据中的精确值查找文档。结构化数据的值包括日期范围、IP地址、价格或产品ID。
与全文查询不同,term-level queries不分析搜索词。相反,词条与存储在字段级别中的术语完全匹配。

PUT /book
{
  "settings": {},
  "mappings": {
    "properties": {
      "description": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "name": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "price": {
        "type": "float"
      },
      "timestamp": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      }
    }
  }
}


PUT /book/_doc/1
{ 
"name": "lucene",
"description": "Lucene Core is a Java library providing powerful indexing and search features, as well as spellchecking, hit highlighting and advanced analysis/tokenization capabilities. The PyLucene sub project provides Python bindings for Lucene Core. ",
"price":100.45,
"timestamp":"2020-08-21 19:11:35"
}

PUT /book/_doc/2
{
"name": "solr",
"description": "Solr is highly scalable, providing fully fault tolerant distributed indexing, search and analytics. It exposes Lucenes features through easy to use JSON/HTTP interfaces or native clients for Java and other languages.",
"price":320.45,
"timestamp":"2020-07-21 17:11:35"
}

PUT /book/_doc/3
{ 
"name": "Hadoop",
"description": "The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models.",
"price":620.45,
"timestamp":"2020-08-22 19:18:35"
} 
PUT /book/_doc/4
{
 "name": "ElasticSearch",
"description": "Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、ApacheGroovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。",
"price":999.99,
"timestamp":"2020-08-15 10:11:35"
}

  1. 词条搜索(term query)
    term 查询用于查询指定字段包含某个词项的文档
POST /book/_search
{"query":{"term":{"name":{"value":"solr"}}}}
  1. 词条集合搜索(terms query)
    terms 查询用于查询指定字段包含某些词项的文档

POST /book/_search
{"query":{"terms":{"name":["solr","elasticsearch"]}}}

  1. 范围搜索(range query)
    gte:大于等于
    gt:大于
    lte:小于等于
    lt:小于
    boost:查询权重
GET /book/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 10,
        "lte": 300,
        "boost": 2.0
      }
    }
  }
}


  • 时间

GET /book/_search
{
  "query": {
    "range": {
      "timestamp": {
        "gte": "now-2d/d",
        "lt": "now/d"
      }
    }
  }
}


GET book/_search
{
  "query": {
    "range": {
      "timestamp": {
        "gte": "18/08/2020",
        "lte": "2021",
        "format": "dd/MM/yyyy||yyyy"
      }
    }
  }
}
  • 不为空搜索(exists query)
    查询指定字段值不为空的文档。相当 SQL 中的 column is not null
# 价格不为空
GET /book/_search
{
  "query": {
    "exists": {
      "field": "price"
    }
  }
}

  1. 词项前缀搜索(prefix query)
GET /book/_search
{
  "query": {
    "prefix": {
      "name": {
        "value": "so"
      }
    }
  }
}
  1. 通配符搜索(wildcard query)
GET /book/_search
{
  "query": {
    "wildcard": {
      
        "name": "so*r"
      
    }
  }
}


GET /book/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "lu*",
        "boost": 2
      }
    }
  }
}

  • 正则搜索(regexp query)
    regexp允许使用正则表达式进行term查询.注意regexp如果使用不正确,会给服务器带来很严重的性能压力。比如.*开头的查询,将会匹配所有的倒排索引中的关键字,这几乎相当于全表扫描,会很慢。因此如果可以的话,最好在使用正则前,加上匹配的前缀
GET /book/_search
{
  "query": {
    "regexp": {
      "name": "s.*"
    }
  }
}
    1. 模糊搜索(fuzzy query)
GET /book/_search
{
  "query": {
    "fuzzy": {
      "name": {
        "value": "so",
        "boost": 1,
        "fuzziness": 2
      }
    }
  }
}


GET /book/_search
{
  "query": {
    "fuzzy": {
      "name": {
        "value": "sorl",
        "boost": 1,
        "fuzziness": 2
      }
    }
  }
}

9) ids搜索(id集合查询)

GET /book/_search
{
  "query": {
    "ids": {
      "type": "_doc",
      "values": [
        "1",
        "3"
      ]
    }
  }
}

复合搜索(compound query)

  1. constant_score query
    用来包装另一个查询,将查询匹配的文档的评分设为一个常值
GET /book/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "description": "solr"
        }
      },
      "boost": 1.2
    }
  }
}

GET /book/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "description": "hadoop"
        }
      },
      "boost": 1.2
    }
  }
}


    1. 布尔搜索(bool query)
      bool 查询用bool操作来组合多个查询字句为一个查询。 可用的关键字:
      must:必须满足
      filter:必须满足,但执行的是filter上下文,不参与、不影响评分
      should:或
      must_not:必须不满足,在filter上下文中执行,不参与、不影响评分

POST /book/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "description": "java"
        }
      },
      "filter": {
        "term": {
          "name": "solr"
        }
      },
      "must_not": {
        "range": {
          "price": {
            "gte": 200,
            "lte": 300
          }
        }
      },
      "minimum_should_match": 1,
      "boost": 1
    }
  }
}

minimum_should_match代表了最小匹配精度,如果设置minimum_should_match=1,那么should
语句中至少需要有一个条件满足

排序

相关性评分排序
默认情况下,返回的结果是按照 相关性 进行排序的——最相关的文档排在最前。
为了按照相关性来排序,需要将相关性表示为一个数值。在 Elasticsearch 中, 相关性得分 由一个浮点数进行表示,并在搜索结果中通过 _score 参数返回, 默认排序是 _score 降序,按照相关性评分升序排序如下

POST /book/_search
{
  "query": {
    "match": {
      "description": "solr"
    }
  }
}
#升序
POST /book/_search
{
  "query": {
    "match": {
      "description": "solr"
    }
    
  },
  "sort": [
    {
      "_score": {
        "order": "asc"
      }
    }
  ]
}

{
“took” : 1,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 2,
“relation” : “eq”
},
“max_score” : null,
“hits” : [
{
“_index” : “book”,
“_type” : “_doc”,
“_id” : “4”,
“_score” : 0.4271554,
“_source” : {
“name” : “ElasticSearch”,
“description” : “Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、ApacheGroovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。”,
“price” : 999.99,
“timestamp” : “2020-08-15 10:11:35”
},
“sort” : [
0.4271554
]
},
{
“_index” : “book”,
“_type” : “_doc”,
“_id” : “2”,
“_score” : 0.85726815,
“_source” : {
“name” : “solr”,
“description” : “Solr is highly scalable, providing fully fault tolerant distributed indexing, search and analytics. It exposes Lucenes features through easy to use JSON/HTTP interfaces or native clients for Java and other languages.”,
“price” : 320.45,
“timestamp” : “2020-07-21 17:11:35”
},
“sort” : [
0.85726815
]
}
]
}
}

  • 字段值排序
    按price降序
POST /book/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}
  • 多级排序
    假定我们想要结合使用 price和 _score(得分) 进行查询,并且匹配的结果首先按照价格排序,然后按照相关性得分排序
POST /book/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    },
    {
      "timestamp": {
        "order": "desc"
      }
    }
  ]
}

{
“took” : 1,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 4,
“relation” : “eq”
},
“max_score” : null,
“hits” : [
{
“_index” : “book”,
“_type” : “_doc”,
“_id” : “4”,
“_score” : null,
“_source” : {
“name” : “ElasticSearch”,
“description” : “Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、ApacheGroovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。”,
“price” : 999.99,
“timestamp” : “2020-08-15 10:11:35”
},
“sort” : [
999.99,
1597486295000
]
},
{
“_index” : “book”,
“_type” : “_doc”,
“_id” : “3”,
“_score” : null,
“_source” : {
“name” : “Hadoop”,
“description” : “The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models.”,
“price” : 620.45,
“timestamp” : “2020-08-22 19:18:35”
},
“sort” : [
620.45,
1598123915000
]
},
{
“_index” : “book”,
“_type” : “_doc”,
“_id” : “2”,
“_score” : null,
“_source” : {
“name” : “solr”,
“description” : “Solr is highly scalable, providing fully fault tolerant distributed indexing, search and analytics. It exposes Lucenes features through easy to use JSON/HTTP interfaces or native clients for Java and other languages.”,
“price” : 320.45,
“timestamp” : “2020-07-21 17:11:35”
},
“sort” : [
320.45,
1595351495000
]
},
{
“_index” : “book”,
“_type” : “_doc”,
“_id” : “1”,
“_score” : null,
“_source” : {
“name” : “lucene”,
“description” : "Lucene Core is a Java library providing powerful indexing and search features, as well as spellchecking, hit highlighting and advanced analysis/tokenization capabilities. The PyLucene sub project provides Python bindings for Lucene Core. ",
“price” : 100.45,
“timestamp” : “2020-08-21 19:11:35”
},
“sort” : [
100.45,
1598037095000
]
}
]
}
}

分页

Elasticsearch中实现分页的语法非常简单

POST /book/_search
{
  "query": {
    "match_all": {}
  },
  "size": 2,
  "from": 0
}
POST /book/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ],
  "size": 2,
  "from": 2
}

size:每页显示多少条
from:当前页起始索引, int start = (pageNum - 1) * size

高亮

Elasticsearch中实现高亮的语法比较简单

POST /book/_search
{
  "query": {
    "match": {
      "name": "elasticsearch"
    }
  },
  "highlight": {
    "pre_tags": "<font color='pink'>",
    "post_tags": "</font>",
    "fields": [{"name":{}}]
    
  }
}







POST /book/_search
{
  "query": {
    "match": {
      "name": "elasticsearch"
    }
  },
  "highlight": {
    "pre_tags": "<font color='pink'>",
    "post_tags": "</font>",
    "fields": [
      {
        "name": {}
      },
      {
        "description": {}
      }
    ]
  }
}


POST /book/_search
{
  "query": {
    "query_string": {
      "query": "elasticsearch"
    }
  },
  "highlight": {
    "pre_tags": "<font color='pink'>",
    "post_tags": "</font>",
    "fields": [
      {
        "name": {}
      },
      {
        "description": {}
      }
    ]
  }
}

在使用match查询的同时,加上一个highlight属性:
pre_tags:前置标签
post_tags:后置标签
fields:需要高亮的字段
name:这里声明title字段需要高亮,后面可以为这个字段设置特有配置,也可以空

{
“took” : 10,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 1,
“relation” : “eq”
},
“max_score” : 1.6126393,
“hits” : [
{
“_index” : “book”,
“_type” : “_doc”,
“_id” : “4”,
“_score” : 1.6126393,
“_source” : {
“name” : “ElasticSearch”,
“description” : “Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、ApacheGroovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。”,
“price” : 999.99,
“timestamp” : “2020-08-15 10:11:35”
},
“highlight” : {
“name” : [
ElasticSearch
],
“description” : [
Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。”,
Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。”,
Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。”,
“根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。”
]
}
}
]
}
}

文档批量操作(bulk 和 mget)

mget 批量查询
单条查询 GET /test_index/_doc/1,如果查询多个id的文档一条一条查询,网络开销太大

GET /_mget
{
  "docs": [
    {
      "_index": "book",
      "_id": 1
    },
    {
      "_index": "book",
      "_id": 2
    }
  ]
}
  • 同一索引下批量查询:
GET /book/_mget
{
  "docs": [
    {
      "_id": 2
    },
    {
      "_id": 3
    }
  ]
}

搜索简化写法

POST /book/_search
{
  "query": {
    "ids": {
      "values": [
        "1",
        "4"
      ]
    }
  }
}

bulk 批量增删改

Bulk 操作解释将文档的增删改查一些列操作,通过一次请求全都做完。减少网络传输次数。语法:
POST /_bulk
{“action”: {“metadata”}}
{“data”}

删除1,新增5,修改2

POST /_bulk
{ "delete": { "_index": "book", "_id": "1" }}
{ "create": { "_index": "book", "_id": "5" }}
{ "name": "test14","price":100.99 }
{ "update": { "_index": "book", "_id": "2"} }
{ "doc" : {"name" : "test"} }

功能:
delete:删除一个文档,只要1个json串就可以了 删除的批量操作不需要请求体
create:相当于强制创建 PUT /index/type/id/_create
index:普通的put操作,可以是创建文档,也可以是全量替换文档
update:执行的是局部更新partial update操作
格式:每个json不能换行。相邻json必须换行。
隔离:每个操作互不影响。操作失败的行会返回其失败信息

实际用法:bulk请求一次不要太大,否则一下积压到内存中,性能会下降。所以,一次请求几千个操作、大小在几M正好。

bulk会将要处理的数据载入内存中,所以数据量是有限的,最佳的数据两不是一个确定的数据,它取决于你的硬件,你的文档大小以及复杂性,你的索引以及搜索的负载。

一般建议是1000-5000个文档,大小建议是5-15MB,默认不能超过100M,可以在es的配置文件(ES的config下的elasticsearch.yml)中配置。
http.max_content_length: 10mb

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值