GDo*_*orn 4 python elasticsearch
这是一个由两部分组成的问题.
我的文件看起来像这样:
{"url": "https://someurl.com",
"content": "searchable content here",
"hash": "c54cc9cdd4a79ca10a891b8d1b7783c295455040",
"headings": "more searchable content",
"title": "Page Title"}
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是如何检索"标题" 正好是 "无标题"的所有文档.我不希望出现标题为"此文档没有标题"的文档.
我的第二个问题是如何检索所有文件,其中'url' 恰好出现在一长串网址中.
我正在使用pyelasticsearch,但curl中的通用答案也可以.
dad*_*net 10
您必须为字段定义映射.
如果要查找精确值(区分大小写),可以将index属性设置为not_analyzed
.
就像是 :
"url" : {"type" : "string", "index" : "not_analyzed"}
Run Code Online (Sandbox Code Playgroud)
试试这个方法。这是工作。
import json
from elasticsearch import Elasticsearch
connection = Elasticsearch([{'host': host, 'port': port}])
elastic_query = json.dumps({
"query": {
"match_phrase": {
"UserName": "name"
}
}
})
result = connection.search(index="test_index", body=elastic_query)
Run Code Online (Sandbox Code Playgroud)
如果您存储了源代码(这是默认设置),您可以使用脚本过滤器
它应该是这样的:
$ 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"}
} ]
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我认为值得一提的是“not_analyzed”映射应该是更快的方法。但是,如果您想要此字段的精确匹配和部分匹配,我看到两个选项:使用脚本或对数据进行两次索引(一次分析,一次不分析)。