1,最佳字段
dis_max 查询
(分离最大化查询,Disjunction Max Query):将任何与任一查询匹配的文档作为结果返回,但只将最佳匹配的评分作为查询的评分结果返回;
// 1,初始化数据
PUT /my_index/my_type/1
{
"title": "Quick brown rabbits",
"body": "Brown rabbits are commonly seen."
}
PUT /my_index/my_type/2
{
"title": "Keeping pets healthy",
"body": "My quick brown fox eats rabbits on a regular basis."
}
// 2,dis_max 查询
GET /my_index/my_type/_search
{
"query": {
"dis_max": {
"queries": [
{ "match": { "title": "Brown fox" }},
{ "match": { "body": "Brown fox" }}
]
}
}
}
2,multi_match
查询
multi_match
查询为能在多个字段上反复执行相同查询提供了一种便捷方式;
// 初始版本
{
"dis_max": {
"queries": [
{
"match": {
"title": {
"query": "Quick brown fox",
"minimum_should_match": "30%"
}
}
},
{
"match": {
"body": {
"query": "Quick brown fox",
"minimum_should_match": "30%"
}
}
},
],
"tie_breaker": 0.3
}
}
// multi_match 简洁形式
{
"multi_match":{
"query":"Quick brown fox",
"type":"best_fields",
"fields":["title","body"],
"tie_breaker":0.3,
"minimum_should_match":"30%"
}
}
3,多数字段
// 1,多字段映射
DELETE /my_index
PUT /my_index
{
"settings": { "number_of_shards": 1 },
"mappings": {
"my_type": {
"properties": {
"title": {
"type": "text",
"analyzer": "english",
"fields": {
"std": {
"type": "text",
"analyzer": "standard"
}
}
}
}
}
}
}
// 2, 初始化数据
PUT /my_index/my_type/1
{ "title": "My rabbit jumps" }
PUT /my_index/my_type/2
{ "title": "Jumping jack rabbits" }
// 3,简单搜索
GET /my_index/_search
{
"query": {
"match": {
"title": "jumping rabbits"
}
}
}
// 3.1 multi_match 搜索
GET /my_index/_search
{
"query": {
"multi_match": {
"query": "jumping rabbits",
"type": "most_fields",
"fields": [ "title", "title.std" ]
}
}
}
// 3.2 通过boost自定义最终评分贡献
GET /my_index/_search
{
"query": {
"multi_match": {
"query": "jumping rabbits",
"type": "most_fields",
"fields": [ "title^10", "title.std" ]
}
}
}
4,cross-fields
跨字段查询
// 字段中心式的 most_fields 查询的 explanation
GET /_validate/query?explain
{
"query":{
"multi_match": {
"query": "peter smith",
"type": "most_fields",
"operator": "and",
"fields":["first_name", "last_name"]
}
}
}
// 词中心式的 cross_fields 查询的 explanation
GET /_validate/query?explain
{
"query":{
"multi_match": {
"query": "peter smith",
"type": "cross_fields",
"operator": "and",
"fields":["first_name", "last_name"]
}
}
}
**参考资料:** -[多字段搜索](https://www.elastic.co/guide/cn/elasticsearch/guide/current/multi-field-search.html)