如何使弹性搜索执行完全匹配查询? [英] How to get elasticsearch to perform an exact match query?
问题描述
这是一个两部分的问题。
This is a two-part question.
我的文档如下所示:
{"url": "https://someurl.com",
"content": "searchable content here",
"hash": "c54cc9cdd4a79ca10a891b8d1b7783c295455040",
"headings": "more searchable content",
"title": "Page Title"}
我的第一个问题是如何检索所有文件,其中title是完全No Title。我不想要一个标题为此文档没有标题的文档。
My first question is how to retrieve all documents where 'title' is exactly "No Title". I don't want a document with the title of "This Document Has No Title" to appear.
我的第二个问题是如何检索出现url的所有文档
My second question is how to retrieve all documents where 'url' appears exactly in a long list of urls.
我正在使用pyelasticsearch,但curl中的一般答案也可以正常工作。
I'm using pyelasticsearch, but a generic answer in curl would also work.
推荐答案
如果你的源存储(默认是这个),你可以使用脚本过滤器
If you have your source stored (which is the default) you can use a script filter
应该去如下所示:
$ curl -XPUT localhost:9200/index/type/1 -d '{"foo": "bar"}'
$ curl -XPUT localhost:9200/index/type/2 -d '{"foo": "bar baz"}'
$ curl -XPOST localhost:9200/index/type/_search?pretty=true -d '{
"filter": {
"script": {
"script": "_source.foo == \"bar\""
}
}
}'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : "index",
"_type" : "type",
"_id" : "1",
"_score" : 1.0, "_source" : {"foo": "bar"}
} ]
}
}
编辑:我认为值得一提的是,not_analyzed映射应该是更快的方法。但是,如果你想要这个字段的确切和部分匹配,我看到两个选项:使用脚本或索引数据两次(一旦分析,一旦没有分析)。
EDIT: I think it's worth mentioning that the "not_analyzed" mapping should be the faster approach. But if you want both exact and partial matches for this field, I see two options: use scripts or index the data twice (once analyzed, once not analyzed).
这篇关于如何使弹性搜索执行完全匹配查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!