elasticsearch的嵌套结构的两种DSL

栏目: 后端 · 发布时间: 7年前

内容简介:针对嵌套结构的基础知识,可以参考文章文章

嵌套结构

针对嵌套结构的基础知识,可以参考文章 嵌套对象

文章 Elasticsearch Nested类型深入详解 对其场景有较好描述:

elasticsearch的嵌套结构的两种DSL

问题

某个elasticsearch的索引有如下mapping:

"Types": {
  "type": "nested",
  "properties": {
    "FirstTypeName": {
      "type": "text",
      "fields": {
        "Raw": {
          "type": "keyword",
          "ignore_above": 256
        }
      },
      "analyzer": "ik_smart"
    },
    "Tags": {
      "type": "text",
      "fields": {
        "Raw": {
          "type": "keyword",
          "ignore_above": 256
        }
      },
      "analyzer": "ik_smart"
    }
  }
}

描述了商品和(一级分类-二级分类)的一对多关系,两点值得说明:

nested
Raw

需要精准搜索时

在业务上需要精准匹配一级分类的时候,需要 term 查询并加强 Raw 属性:

{
  "query": {
    "bool": {
      "must": {
        "nested": {
          "path": "Types",
          "query": {
            "bool": {
              "must": {
                "term": {
                  "Types.FirstTypeName.Raw": "YJT的数码智能"
                }
              }
            }
          }
        }
      }
    }
  }
}

需要全无搜索时

在用户搜索场景,需要分类提供一定的score时,则只需要 match 查询:

{
  "from": 0,
  "query": {
    "function_score": {
      "boost_mode": "multiply",
      "field_value_factor": {
        "factor": 2,
        "field": "RecentOrderCnt",
        "modifier": "log2p"
      },
      "max_boost": 4,
      "query": {
        "bool": {
          "must": {
            "match": {
              "GoodsName": {
                "operator": "and",
                "query": "华为"
              }
            }
          },
          "should": {
            "multi_match": {
              "fields": [
                "Types.FirstTypeName^4",
                "Types.Tags^4",
                "Brand^30",
                "Labels^2"
              ],
              "query": "华为",
              "tie_breaker": 0.1,
              "type": "best_fields"
            }
          }
        }
      },
      "score_mode": "sum"
    }
  },
  "size": 2,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ]
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Artificial Intelligence

Artificial Intelligence

Stuart Russell、Peter Norvig / Pearson / 2009-12-11 / USD 195.00

The long-anticipated revision of this #1 selling book offers the most comprehensive, state of the art introduction to the theory and practice of artificial intelligence for modern applications. Intell......一起来看看 《Artificial Intelligence》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具