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)
  • 相关阅读:
    hdu 5723 Abandoned country 最小生成树 期望
    OpenJ_POJ C16G Challenge Your Template 迪杰斯特拉
    OpenJ_POJ C16D Extracurricular Sports 打表找规律
    OpenJ_POJ C16B Robot Game 打表找规律
    CCCC 成都信息工程大学游记
    UVALive 6893 The Big Painting hash
    UVALive 6889 City Park 并查集
    UVALive 6888 Ricochet Robots bfs
    UVALive 6886 Golf Bot FFT
    UVALive 6885 Flowery Trails 最短路
  • 原文地址:https://www.cnblogs.com/linkworld/p/12047967.html
Copyright © 2011-2022 走看看