Skip to content

Latest commit

 

History

History
606 lines (571 loc) · 9.45 KB

01_text_search.md

File metadata and controls

606 lines (571 loc) · 9.45 KB

1、二次评分

POST library/_search
{
  "_source": {
    "includes": ["title", "available"]
  },
  "query": {
    "match_all": {}
  },
  "rescore":{
    "query":{
      "rescore_query":{
        "function_score":{
          "query":{
            "match_all":{}
          },
          "script_score":{
            "script":"doc['year'].value"
          }
        }
      }
    }
  }
}

2、多字段检索

GET library/_search
{
  "query": {
    "multi_match": {
      "query": "complete conan doyle",
      "fields": ["title", "author", "characters"]
    }
  }
}

这种方式,逐渐被废弃和替换

GET library/_search
{
  "query": {
    "multi_match": {
      "query": "complete conan doyle",
      "fields": ["title", "author", "charactor"], 
      "use_dis_max": true
    }
  }
}

3、多字段检索——best fields匹配

GET library/_search
{
  "query": {
    "multi_match": {
      "query": "complete conan doyle",
      "fields": ["title", "author", "characters"], 
      "type":"best_fields",
      "tie_breaker": 0.8
    }
  }
}



GET library/_search
{
  "query": {
   "dis_max": {
     "tie_breaker": 0.8,
     "queries": [
       {
         "match": {
           "title": "complete conan doyle"
         }
       },{
         "match": {
           "author": "complete conan doyle"
         }
       },{"match":{
         "characters": "complete conan doyle"
       }}]
   }
  }
}

4、多字段检索——cross_fields匹配

GET library/_search
{
  "query": {
    "multi_match": {
      "query": "complete conan doyle",
      "fields": ["title", "author", "characters"], 
      "type":"cross_fields",
    "operator":"and"
    }
  }
}

5、多字段检索——most_fields匹配

POST library/_search
{
  "query": {
    "multi_match": {
      "query": "Die Leiden",
      "fields": ["title", "otitle"], 
      "type":"most_fields"
    }
  }
}


GET library/_search
{
  "query": {
  "bool": {
    "should": [
      {"match": {
        "title": "die leiden"
      }},
      {
        "match": {
          "otitle": "die leiden"
        }
      }
    ]
  }
   }
  }

6、多字段检索——phrase匹配

GET library/_search
{
  "query": {
   "multi_match": {
     "query": "sherlock holmes",
     "fields": ["title","author"],
     "type":"phrase"
   }
}
}

GET library/_search
{
  "query": {
    "dis_max": {
      "queries": [
        {
        "match_phrase":{"title":"sherlock holmes"}},
       { "match_phrase":{
          "author":"sherlock holmes"
        }}]
    }
  }
}

7、多字段检索——phrase_prefix匹配

  GET library/_search
{
  "query": {
   "multi_match": {
     "query": "sherlock hol",
     "fields": ["title","author"],
     "type":"phrase_prefix"
   }
}
}

GET library/_search
{
  "query": {
    "dis_max": {
      "queries": [
        {
        "match_phrase_prefix":{"title":"sherlock holmes"}},
       { "match_phrase_prefix":{
          "author":"sherlock holmes"
        }}]
    }
  }
}

#7、聚合操作

POST interns/review/1
{
  "intern":"Richard",
  "grade":"bad",
  "type":"grade"
}

POST interns/review/2
{
  "intern":"Ralf",
  "grade":"perfact",
  "type":"grade"
}

POST interns/review/3
{
  "intern":"Richard",
  "grade":"bad",
  "type":"grade"
}

POST interns/review/3
{
  "intern":"Richard",
  "grade":"bad",
  "type":"grade"
}
POST interns/review/4
{
  "intern":"Richard",
  "grade":"bad",
  "type":"review"
}
POST interns/review/5
{
  "intern":"Richard",
  "grade":"good",
  "type":"grade"
}
POST interns/review/6
{
  "intern":"Ralf",
  "grade":"good",
  "type":"grade"
}

POST interns/review/7
{
  "intern":"Ralf",
  "grade":"perfect",
  "type":"grade"
}
     POST interns/review/8
{
  "intern":"Richard",
  "grade":"medium",
  "type":"review"
}
     POST interns/review/9
{
  "intern":"Monica",
  "grade":"medium",
  "type":"grade"
}

POST interns/review/10
{
  "intern":"Monica",
  "grade":"medium",
  "type":"grade"
}

POST interns/review/11
{
  "intern":"Ralf",
  "grade":"good",
  "type":"grade"
}

POST interns/review/12
{
  "intern":"Ralf",
  "grade":"good",
  "type":"grade"
}

//聚合——查找对于Richard的典型评价
GET /interns/_search
{
"query": {
  "match": {
    "intern": "Richard"
  }
},
"aggregations":{
  "description":{
    "significant_terms": {
      "field": "grade.keyword"
    }
  }
}
}

8、应该针对非text类型字段进行聚合

GET library/_search
{
  "query": {
    "term":{
      "available": true
    }
  },
  "aggregations":{
    "description":{
      "significant_terms":
      {
        "field":"title.keyword"
      }
    }
  }
}

9、设置过滤条件来缩小后台数据集的大小。

GET /interns/_search
{
  "query": {
    "match": {
      "intern": "Richard"
    }
  },
  "aggregations":{
    "description":{
      "significant_terms": {
        "field": "grade.keyword",
        "background_filter": {
          "term":{
            "type":"grade"
          }
        }
      }
    }
  }
}

10、限定最小文档数

GET /interns/_search
{
  "query": {
    "match": {
      "intern": "Richard"
    }
  },
  "aggregations":{
    "description":{
      "significant_terms": {
        "field": "grade.keyword",
       "min_doc_count": 3
      }
    }
  }
}

11、top_hits聚合——文档分组

GET library/_search

GET library/_search
{
  "size":0,
  "aggs":{
    "when":{
      "histogram": {
        "field": "year",
        "interval": 100
      },
      "aggs":{
        "book":{
          "top_hits": {
            "sort":{
              "year":"asc"
            },
            "_source":{
              "includes":["title","avaliable"]
            }
          }
        }
      }
    }
  }
}

12、聚合的复杂实现

GET library/_search
{
  "query": {
    "bool":{
      "filter": {
       "term":{
         "copies": 1 
       }
      }
    }
  },
  "size":0,
  "aggs": {
    "when": {
      "histogram": {
        "field": "year",
        "interval": 100
      },
      "aggs": {
        "book": {
          "top_hits": {
            "highlight": {
              "fields": {
                "title": {}
              }
            },
            "explain": true,
            "version": true,
            "_source": {
              "includes": ["title","avalible"]},
          "script_fields":{
            "century":{
              "script":"(doc[\"year\"].value / 100).intValue()"
            }
          },
          "size":1
        }
        }
      }
    }
  }
}

13、terms aggeration

注意terms聚合和significant_terms的区别: 前者返回全部信息,后者返回最重要,得分最高的信息。

GET /interns/_search
{
  "query": {
    "match": {
      "intern": "Richard"
    }
  },
  "aggs":{
    "description":{
      "terms": {
        "field": "grade.keyword"
      }
    }
  }
}

14、object类型与检索

PUT object
POST object/_mapping/doc
{
  "doc":{
    "properties":{
      "title":{"type":"text",
      "fields":{
        "keyword":{
        "type":"keyword"
        }
      }
    },
    "quantity":{"type":"integer"},
    "edition":{
      "type":"object",
      "properties":{
        "isbn":{"type":"keyword"},
        "circulation":{"type":"integer"}
      }
    }
  }
}
}

POST object/doc/1
{
  "title":"Title",
  "quantity":100,
  "edition":[
    {
      "isbn":"1234567890",
      "circulation":50000
    },
    {
      "isbn":"9876543210",
      "circulation":2000
    }
    ]
  }
  
  GET object/_search
  {
    "_source": {
      "includes": ["_id","title"]
    },
    "query": {
      "bool": {
        "must": [
          {"term":{
            "edition.isbn": "1234567890"
          }},
          {"term": {
            "edition.circulation": {
              "value": 2000
            }
          }}]
      }
    }
  }

15、nested类型与检索

 PUT nested_test
POST nested_test/_mapping/doc
{
 "doc":{
   "properties":{
     "title":{"type":"text",
     "fields":{
       "keyword":{
       "type":"keyword"
       }
     }
   },
   "quantity":{"type":"integer"},
   "edition":{
     "type":"nested",
     "properties":{
       "isbn":{"type":"keyword"},
       "circulation":{"type":"integer"}
     }
   }
 }
}
}

POST nested_test/doc/1
{
 "title":"Title",
 "quantity":100,
 "edition":[
   {
     "isbn":"1234567890",
     "circulation":50000
   },
   {
     "isbn":"9876543210",
     "circulation":2000
   }
   ]
 }
 
 GET nested_test/_search
 {
   "_source": {
     "includes": ["_id","title"]
   },
   "query": {
     "bool": {
       "must": [
         {"term":{
           "edition.isbn": "1234567890"
         }},
         {"term": {
           "edition.circulation": {
             "value": 2000
           }
         }}]
     }
   }
 }

16、脚本检索

_index方式在6.X版本已不支持,如下官网介绍: https://www.elastic.co/guide/en/elasticsearch/reference/6.x/modules-advanced-scripting.html

GET library/_search
{
  "_source": {
     "includes": ["_id", "_score", "title", "year"]
  },
  "query": {
    "function_score": {
      "query": {"match_all": {}},
      "script_score": {
       "script":"def year = doc[\"year\"].value; if (year < 1800) { return 1.0} else if (year < 1900) { return 2.0 } else { return year - 1000 }"
      }
    }
  }
}