Elasticsearch实现类Google高级检索

一、高级检索的功能点

通过高级搜索配置搜索项,能更准确的过滤掉不相干信息,获取最想要的检索信息。 
以Google搜索为例(截取核心片段): 
这里写图片描述

二、高级检索拆分

1、包含以下全部的关键词:

需要分词处理; 
若需要指定字段,则使用matchQuery实现; 
若无需指定字段的全文检索,则使用queryStringQuery实现;

2、包含以下的完整关键词 :

需要完整匹配字符,使用wildcardQuery结合”*”实现;

3、包含以下任意一个关键词 :

空格分隔每个关键词,需要将多个词空格拆分,然后对每个关键词通过should结合wildcardQuery遍历实现;

4、不包括以下关键词 :

包含以下全部关键词的反面, 
若需要指定字段,则使用must_not结合matchQuery实现; 
若无需指定字段,则使用must_not结合queryStringQuery实现;

5、限定要搜索的网页的时间是:

限定搜索的开始和结束时间,通过rangequery实现。

6、关键词位置:

根据关键词位置的不同,采用不同的实现; 
关键词位于title字段:指定title进行检索; 
关键词位于content字段:指定content进行检索; 
关键词位于全部字段:不指定字段进行检索,多使用 queryStringQuery实现。

三、DSL高级检索实现

POST detail_index/_search
{
  "from" : 0,
  "size" : 10,
  "query" : {
  "bool" : {
  "must" : [
  {
  "match" : {
  "title" : {
  "query" : "苹果乔布斯",
  "operator" : "OR",
  "prefix_length" : 0,
  "max_expansions" : 50,
  "fuzzy_transpositions" : true,
  "lenient" : false,
  "zero_terms_query" : "NONE",
  "boost" : 1.0
  }
  }
  },
  {
  "wildcard" : {
  "title.keyword" : {
  "wildcard" : "*苹果总裁库克*",
  "boost" : 1.0
  }
  }
  },
  {
  "range" : {
  "public_time" : {
  "from" : "2017-09-07 00:00:00",
  "to" : "2017-09-15 23:59:59",
  "include_lower" : true,
  "include_upper" : true,
  "boost" : 1.0
  }
  }
  }
  ],
  "must_not" : [
  {
  "wildcard" : {
  "title.keyword" : {
  "wildcard" : "*苹果梨*",
  "boost" : 1.0
  }
  }
  }
  ],
  "should" : [
  {
  "wildcard" : {
  "title.keyword" : {
  "wildcard" : "*苹果手机*",
  "boost" : 1.0
  }
  }
  },
  {
  "wildcard" : {
  "title.keyword" : {
  "wildcard" : "*iphoneX*",
  "boost" : 1.0
  }
  }
  }
  ],
  "disable_coord" : false,
  "adjust_pure_negative" : true,
  "boost" : 1.0
  }
  },
  "_source" : {
  "includes" : [
  "title",
  "content"
  ],
  "excludes" : [ ]
  },
  "highlight" : {
  "pre_tags" : [
  "<span style=\"color:red\">"
  ],
  "post_tags" : [
  "</span>"
  ],
  "fragment_size" : 100,
  "number_of_fragments" : 5,
  "require_field_match" : true,
  "fields" : {
  "title" : { }
  }
  }
}

以上DSL对应如下: 
这里写图片描述

猜你喜欢

转载自blog.csdn.net/hellozhxy/article/details/81458012
今日推荐