设为首页 收藏本站
查看: 2514|回复: 0

[经验分享] elasticsearch 精确,模糊查询实例

[复制链接]

尚未签到

发表于 2017-5-21 06:28:33 | 显示全部楼层 |阅读模式
实例
http://www.bubuko.com/infodetail-648214.html
GET _search
{
  "query": {
    "match_all": {}
  }
}
PUT /useraudit
POST /useraudit/t_unparticiple_test/_mapping
{
  "t_unparticiple_test": {
    "properties": {
      "uid": {
        "type": "long"
      },
      "country": {
        "type": "string"
      },
      "city": {
        "type": "string"
      },
      "area": {
        "type": "string"
      },
      "address": {
        "type": "string"
      },
      "time": {
        "type": "long"
      }
    }
  }
}
POST /useraudit/t_participle_test/_mapping
{
  "t_participle_test": {
    "properties": {
      "uid": {
        "type": "long"
      },
      "country": {
        "type": "string"
      },
      "city": {
        "type": "string"
      },
      "area": {
        "type": "string"
      },
      "address": {
        "type": "string",
        "include_in_all": true,
        "analyzer": "ik",
        "term_vector": "with_positions_offsets"
      },
      "time": {
        "type": "long"
      }
    }
  }
}
POST /useraudit/t_participle_index_test/_mapping
{
  "t_participle_index_test": {
    "properties": {
      "uid": {
        "type": "long",
        "index":  "not_analyzed"
      },
      "country": {
        "type": "string",
        "index":  "not_analyzed"  
      },
      "city": {
        "type": "string",
        "index":  "not_analyzed"
      },
      "area": {
        "type": "string",
        "index":  "not_analyzed"
      },
      "address": {
        "type": "string",
        "include_in_all": true,
        "analyzer": "ik",
        "term_vector": "with_positions_offsets"
      },
      "time": {
        "type": "long",
        "index":  "not_analyzed"
      }
    }
  }
}
PUT /useraudit/t_unparticiple_test/1
{
  "uid": 200111,
  "country": "四川省",
  "city":"眉山市",
  "area":"东坡区",
  "time": 1441293300,
  "address": "红星东路二段139号"
}
PUT /useraudit/t_unparticiple_test/6
{
  "uid": 200111,
  "country": "四川省",
  "city":"眉山市",
  "area":"东坡区",
  "time": 1441293300,
  "address": "东坡胡公园"
}

PUT /useraudit/t_unparticiple_test/2
{
  "uid": 200115,
  "country": "四川省",
  "city":"眉山市",
  "area":"东坡区",
  "time": 1441293300,
  "address": "红星东路西三段123号冒菜店"
}
PUT /useraudit/t_unparticiple_test/3
{
  "uid": 362201,
  "country": "浙江省",
  "city":"杭州市",
  "area":"萧山区",
  "time": 1441293300,
  "address": "宁围镇二桥村顺发康庄4栋1单元902"
}
PUT /useraudit/t_unparticiple_test/4
{
  "uid": 362203,
  "country": "浙江省",
  "city":"衢州市",
  "area":"常山县",
  "time": 1441293300,
  "address": "新都工业区文东路852号天幕家纺"
}
PUT /useraudit/t_unparticiple_test/5
{
  "uid": 662203,
  "country": "浙江省",
  "city":"衢州市",
  "area":"常山县",
  "time": 1441293300,
  "address": "低塘街道新堰西路45号"
}
PUT /useraudit/t_participle_test/1
{
  "uid": 200111,
  "country": "四川省",
  "city":"眉山市",
  "area":"东坡区",
  "time": 1441293300,
  "address": "红星东路二段139号"
}
PUT /useraudit/t_participle_test/2
{
  "uid": 200115,
  "country": "四川省",
  "city":"眉山市",
  "area":"东坡区",
  "time": 1441293300,
  "address": "红星东路西三段123号冒菜店"
}
PUT /useraudit/t_participle_test/3
{
  "uid": 362201,
  "country": "浙江省",
  "city":"杭州市",
  "area":"萧山区",
  "time": 1441293300,
  "address": "宁围镇二桥村顺发康庄4栋1单元902"
}
PUT /useraudit/t_participle_test/4
{
  "uid": 362203,
  "country": "浙江省",
  "city":"衢州市",
  "area":"常山县",
  "time": 1441293300,
  "address": "新都工业区文东路852号天幕家纺"
}
PUT /useraudit/t_participle_test/5
{
  "uid": 662203,
  "country": "浙江省",
  "city":"衢州市",
  "area":"常山县",
  "time": 1441293300,
  "address": "低塘街道新堰西路45号"
}

PUT /useraudit/t_participle_index_test/1
{
  "uid": 200111,
  "country": "四川省",
  "city":"眉山市",
  "area":"东坡区",
  "time": 1441293300,
  "address": "红星东路二段139号"
}
PUT /useraudit/t_participle_index_test/2
{
  "uid": 200115,
  "country": "四川省",
  "city":"眉山市",
  "area":"东坡区",
  "time": 1441293300,
  "address": "红星东路西三段123号冒菜店"
}
PUT /useraudit/t_participle_index_test/3
{
  "uid": 362201,
  "country": "浙江省",
  "city":"杭州市",
  "area":"萧山区",
  "time": 1441293300,
  "address": "宁围镇二桥村顺发康庄4栋1单元902"
}
PUT /useraudit/t_participle_index_test/4
{
  "uid": 362203,
  "country": "浙江省",
  "city":"衢州市",
  "area":"常山县",
  "time": 1441293300,
  "address": "新都工业区文东路852号天幕家纺"
}
PUT /useraudit/t_participle_index_test/5
{
  "uid": 662203,
  "country": "浙江省",
  "city":"衢州市",
  "area":"常山县",
  "time": 1441293300,
  "address": "低塘街道新堰西路45号"
}

#精确查询
GET /useraudit/t_unparticiple_test/_search?q=uid:200111
GET /useraudit/t_unparticiple_test/_search
{
  "query": {
    "match": {
      "uid": "200111"
    }
  }
}
# 字段为strin类型,mappin中未定义取消分词,精确查询不到数据
GET /useraudit/t_unparticiple_test/_search
{
  "query": {
    "match": {
      "city": "衢州市"
    }
  }
}
GET /useraudit/t_participle_test/_search
{
  "query": {
    "match": {
      "country": "四川省"
    }
  }
}
# 字段为strin类型,mappin中定义取消分词,精确查询数据
GET /useraudit/t_participle_index_test/_search
{
  "query": {
    "match": {
      "country": "四川省"
    }
  }
}

#模糊查询
GET /useraudit/t_participle_test/_search
{
    "query" : { "term" : { "address" : "红星" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "address" : {}
        }
    }
}
GET /useraudit/t_participle_index_test/_search
{
    "query" : { "term" : { "address" : "红星" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "address" : {}
        }
    }
}
POST /index/fulltext/_search
{
    "query" : { "term" : { "content" : "中国" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}
二, java客户端调用Elasticsearch集群
1, 精准查询
2, 模糊查询(使用分词)

public class ESQueryClient {
static Client CLIENT = null;
static {
// on startup
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch_useraudit").put("client.transport.sniff", true)
.build();
CLIENT = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9301))
.addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9302))
.addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9303));
}
public static void main(String[] args) throws ElasticsearchException, IOException {
// 通过id查询
qryById();
// 通过某个字段查询,例如uid, country
SearchRequestBuilder builder = CLIENT.prepareSearch("useraudit").setTypes("t_participle_test").setSearchType(SearchType.DEFAULT)
.setFrom(0).setSize(100);
BoolQueryBuilder qb = QueryBuilders.boolQuery().must(new QueryStringQueryBuilder("眉山市").field("city"));
builder.setQuery(qb);
SearchResponse response = builder.execute().actionGet();
System.out.println("字段为strin类型, mappin中未定义取消分词,精确查询不到数据, 测试结果:");
System.out.println("通过city字段查询:" + response);
System.out.println("通过city字段查询信息数量:" + response.getHits().getTotalHits());
// 通过某个字段查询,例如uid, country
builder = CLIENT.prepareSearch("useraudit").setTypes("t_participle_index_test").setSearchType(SearchType.DEFAULT).setFrom(0).setSize(100);
qb = QueryBuilders.boolQuery().must(new QueryStringQueryBuilder("眉山市").field("city"));
builder.setQuery(qb);
response = builder.execute().actionGet();
System.out.println("字段为strin类型, mappin中定义取消分词,精确查询数据 , 测试结果:");
System.out.println("通过city字段查询:" + response);
System.out.println("通过city字段查询信息数量:" + response.getHits().getTotalHits());
// .should(new QueryStringQueryBuilder("太多").field("city"))
CLIENT.close();
}
private static void qryById() {
GetResponse response = CLIENT.prepareGet("useraudit", "t_participle_test", "1").setOperationThreaded(false).execute().actionGet();
System.out.println("通过id查询:" + response.getSourceAsString());
}
}

三, 索引数据的维护
四, 批量插入数据
测试数据量5千万
五, 压力测试并发精准查询和模糊查询

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-379415-1-1.html 上篇帖子: 详解ElasticSearch的store属性 下篇帖子: 使用java访问elasticsearch创建索引
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表