[英]Searching for whole URL using elastic4s
我正在使用elastic4s以便在ES中索引和搜索数据。 我要存储的文档的一部分包含一个我需要搜索的URL字段(整个URL)。 当我搜索包含url字段的文档并获得0个结果时,会出现问题。
为了进行此搜索,我在将数据插入索引之前定义了一个映射:
client.execute {
create index <my_index> mappings {
<my_type> as {
"url" typed StringType analyzer NotAnalyzed
}
}
}
我正在插入数据:
client.execute { index into <my_index> -> <my_type> fields (
"url" -> "http://sample.com"
)
}.await
然后我搜索文档:
val filter =
"""
|{
| "filtered" : {
| "query" : {
| "match_all" : {}
| },
| "filter" : {
| "bool" : {
| "should" : [
| { "bool" : {
| "must" : [
| { "term" : { "url" : "http://sample.com" } }
| ]
| } }
| ]
| }
| }
| }
|}
""".stripMargin
client.execute { search in <my_index> -> <my_type> rawQuery { filter } }.await
执行此查询后,我得到0个结果。 我究竟做错了什么?
谢谢!
问题解决了。
在映射中,而不是这样做:
client.execute {
create index <my_index> mappings {
<my_type> as {
"url" typed StringType analyzer NotAnalyzed
}
}
}
我应该做的:
client.execute {
create index <my_index> mappings {
<my_type> as {
"url" typed StringType index "not_analyzed"
}
}
}
要么
client.execute {
create index <my_index> mappings {
<my_type> as {
"url" typed StringType index NotAnalyzed
}
}
}
我认为您的查询可以简化为:
{
"query": {
"term": {
"url": {
"value": "http://example.com"
}
}
}
}
奇怪的是,由于布尔查询的嵌套,我希望您的查询返回所有文档。 should
意味着与数组中任何查询匹配的文档的得分均高于不匹配的文档。 因此,一个should
只包含一个must
要返回所有的文件,用匹配的文档得分比不匹配更高。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.