栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PHPelasticsearch过滤查询字符串搜索

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHPelasticsearch过滤查询字符串搜索

实现此目的的一种方法是对字段使用muti_fields映射

description
。多字段中的一个字段不应进行分析。数据重新索引后,您可以使用简单的布尔查询来实现所需的目标:

创建索引:

put test{    "mappings": {        "data" : { "properties": {     "description" : {         "type": "string",          "fields": {  "raw" : {"type": "string","index": "not_analyzed"}          }     } }}    }}

索引数据:

put test/data/1 {    "description" : "a",    "test_id" : 10}put test/data/2{    "description" : "",    "test_id" : 10}put test/data/3{    "description" : "hello",    "test_id" : 10}put test/data/4{    "description": "a",    "test_id" : 20}

查询:

post test/data/_search{   "query": {      "filtered": {         "query": { "bool": {    "disable_coord": "true",    "should": [       {          "query_string": {  "fields": [     "description"  ],  "query": "a"          }       },       {          "constant_score": {  "filter": {     "term": {        "description.raw": ""     }  },  "boost": 0.2          }       },       {          "constant_score": {  "filter": {     "exists": {        "field": "description"     }  },  "boost": 0.1          }       }    ] }         },         "filter": { "terms": {    "test_id": [       10    ] }         }      }   }}

结果:

 "hits": [         { "_index": "test", "_type": "data", "_id": "1", "_score": 0.5113713, "_source": {    "description": "a",    "test_id": 10 }         },         { "_index": "test", "_type": "data", "_id": "2", "_score": 0.29277003, "_source": {    "description": "",    "test_id": 10 }         },         { "_index": "test", "_type": "data", "_id": "3", "_score": 0.097590014, "_source": {    "description": "hello",    "test_id": 10 }         }      ]

查询空字符串:

{   "query": {      "filtered": {         "query": { "bool": {    "disable_coord": "true",    "should": [       {          "query_string": {  "fields": [     "description"  ],  "query": ""          }       },       {          "constant_score": {  "filter": {     "term": {        "description.raw": ""     }  },  "boost": 0.2          }       },       {          "constant_score": {  "filter": {     "exists": {        "field": "description"     }  },  "boost": 0.1          }       }    ] }         },         "filter": { "terms": {    "test_id": [       10    ] }         }      }   }}

结果:

  "hits": [         { "_index": "test", "_type": "data", "_id": "2", "_score": 1.3416407, "_source": {    "description": "",    "test_id": 10 }         },         { "_index": "test", "_type": "data", "_id": "1", "_score": 0.44721356, "_source": {    "description": "a",    "test_id": 10 }         },         { "_index": "test", "_type": "data", "_id": "3", "_score": 0.44721356, "_source": {   "description": "hello",    "test_id": 10 }         }      ]


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/386044.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号