PHP弹性搜索过滤的查询字符串搜索 [英] PHP Elastic Search Filtered Query String Search
问题描述
所有人都希望使用过滤的查询,其中结果应包含来自query_string的数据,也可以使用term - filter应用的
GET blog / _search
{
query:{
filtered:{
query:{
query_string {
fields:[description],
query:a//或只是
}
},
filter :{
terms:{
topic_id:[
10
]
}
}
}
}
}
预期结果是:
- 所有具有letter_id为a或的博客记录为10。
- 还有其余的topic_id为10的记录如果说明为空/空。
So fi最终结果应该是 - 具有较高分数的匹配记录应该在顶部,然后记录与过滤器中的topic_id匹配。
实现此目的的一种方法是使用 muti_fields 映射描述
字段。多领域的一个领域应该是不分析的。
数据重新编号后,您可以使用简单的 bool query 以达到您想要的目的:
示例
创建索引
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
}
放test / data / 3
{
description:hello,
test_id:10
}
put test / data / 4
{
description:a,
test_id:20
}
查询:
数据/ _search
{
查询:{
已过滤:{
查询:{
bool:{
disable_coord :true,
should:[
{
query_string:{
fields:[
description
] ,
query:a
}
},
{
constant_score:{
filter:{
term:{
description.raw:
},
boost:0.2
}
},
{
constant_score:{
filter
存在:{
field:description
}
},
boost:0.1
}
}
]
}
},
过滤器:{
条款:{
test_id:[
10
]
}
}
}
}
}
结果
hits:[
{
_index:test,
_type:data,
_id:1,
_sco re: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:{
已过滤:{
查询:{
bool:{
disable_coord:true,
应该:[
{
query_string:{
fields:[
description
],
query
}
},
{
constant_score:{
filter:{
term:{
description 。
}
},
boost:0.2
}
},
{
constant_score {
filter:{
exists:{
field:description
}
},
boost:0.1
}
]
}
},
过滤器:{
条款:{
test_id:[
10
]
}
}
}
}
}
结果:
hits:[
pre>
{
_index:test,
_type:data,
_id:2,
_score:1.3416407,
_source:{
description:,
test_id:10
}
},
{
_index:test
_type:data,
_id:1,
_score:0.44721356,
_source:{
描述:a,
test_id:10
}
},
{
_index:test,
_type:data,
_id:3,
_score:0.44721356,
_source {
description:hello,
test_id:10
}
}
]
All would like to use the filtered query where results should contain data from the "query_string" and also from the "term - filter" applied.
GET blog/_search { "query": { "filtered": { "query": { "query_string": { "fields": [ "description" ], "query": "a" // or just "" } }, "filter": { "terms": { "topic_id": [ 10 ] } } } } }
The expected result is:
- all blog records having letter "a" or "" in it with topic_id is 10.
- also rest of the records where topic_id is 10 even if the description is blank/empty.
So final result should be - the matching records with higher score and should come at the top, then the records just matching the "topic_id" from the filter.
解决方案One way to achieve this is use muti_fields mapping for
description
field. One of the fields in multi-field should be non-analyzed. Once the data has been reindexed you can use a simple bool query to achieve what you want :Example
Create Index:
put test { "mappings": { "data" : { "properties": { "description" : { "type": "string", "fields": { "raw" : {"type": "string","index": "not_analyzed"} } } } } } }
Index Data:
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 }
Query:
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 ] } } } } }
Results :
"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 Empty string:
{ "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 ] } } } } }
Result :
"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 } } ]
这篇关于PHP弹性搜索过滤的查询字符串搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!