zoukankan      html  css  js  c++  java
  • Elasticsearch 多字段搜索

    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)
  • 相关阅读:
    事件对象
    事件方法on()
    each()遍历
    链接式操作
    理解选取更新范围
    net3.5 无网络环境安装
    visual studio 2017 报错 无法下载安装文件。请检查Internet连接,然后重试
    删除数据恢复数据语句 Oracle
    sqlserver还原数据库(mdf与ldf文件如何还原到SQLserver数据库)
    sqlserver2012卸载
  • 原文地址:https://www.cnblogs.com/linkworld/p/12047967.html
Copyright © 2011-2022 走看看