我一直在使用简单的查询字符串来查询Elastic内部的信息,但是遇到了问题。
当我创建查询时:
"simple_query_string":{
"query":"\"this is a phrase\" | KEYWORD_1 | KEYWORD_2",
"fields":[
"field_1",
"field_2",
"field_3",
],
}
它返回0个结果,同时如果查询是用括号构造的:
"simple_query_string":{
"query":"(\"this is a phrase\" | KEYWORD_1) | KEYWORD_2",
"fields":[
"field_1",
"field_2",
"field_3",
],
}
工作顺利。
我想知道我的弹性文档中是否缺少某些东西。
答案 0 :(得分:1)
IanGabes在评论中说:
根据elasticsearch's documentation:
foo bar +baz -qux
还支持熟悉的布尔运算符AND,OR和NOT(也写为&&,||和!),但要注意它们不遵守通常的优先级规则,因此,每当多个运算符一起使用时,都应使用括号。例如,先前的查询可以重写为:
((foo AND baz) OR (bar AND baz) OR baz) AND NOT qux
或者在简单的查询lenguaje中:
((foo + baz) | (bar + baz) | baz) + -qux
关于我的示例:
"this is a phrase" | KEYWORD_1 | KEYWORD_2
可以扩展到
("this is a phrase" | KEYWORD_1) | ("this is a phrase" | KEYWORD_2) | (KEYWORD_1 | KEYWORD_2)
但是,以一种更简单的方式:
("this is a phrase" | KEYWORD_1) | KEYWORD_2
答案 1 :(得分:0)
添加带有索引数据,搜索查询和搜索结果的工作示例。要了解有关简单查询字符串参数的更多信息,请参阅simple query string syntax
上的此官方文档索引数据
{
"name":"multi",
"title":"Science",
"shape":"triangle"
}
{
"name":"multi grain bread",
"title":"Maths",
"shape":"square"
}
{
"name":"multi",
"title":"Science",
"shape":"square"
}
搜索查询
{
"query": {
"simple_query_string": {
"query": "\"multi grain bread\" | Maths | square",
"fields": [
"title",
"name",
"shape"
]
}
}
}
搜索结果
"hits": [
{
"_index": "my-index",
"_type": "_doc",
"_id": "1",
"_score": 2.6891878,
"_source": {
"name": "multi grain bread",
"title": "Maths",
"shape": "square"
}
},
{
"_index": "my-index",
"_type": "_doc",
"_id": "2",
"_score": 0.47000363,
"_source": {
"name": "multi",
"title": "Science",
"shape": "square"
}
}
]