考虑下列文件:
{
"Title": "Western Europe"
}我想对标题字段运行这样的搜索查询。
我可以运行一个简单的匹配查询:
POST /_search
{
"query": {
"match": {
"Title": "Apple in Western Europe"
}
}
}不管我会用哪个搜索短语,它显然都会匹配并带回来。但是,我想要做一个查询,只有当标题字段短语与我的搜索查询匹配时,才会返回我的文档。这有可能吗?还有其他参数吗?对于短语匹配来说,情况似乎正好相反。
如果不是,我是否应该考虑用小片来重新索引我的数据?
因此,在这个场景中,运行这个(带有附加参数)不会得分,也不会带回我的文档。
POST /_search
{
"query": {
"match": {
"Title": "Apple in Eastern Europe"
}
}
}tl;博士
如果文档的所有字段(我正在搜索的字段)标记都在我的搜索查询中,我如何编写一个返回文档的查询?例如,文档中的字段仅包含这两个令牌:
如果我的搜索短语是,例如,Lorem ipsum dolor同坐,consectetur adipiscing elit abc xyz,document is back。
如果是Lorem ipsum dolor同坐,则为xyz,则为,而不是。
发布于 2020-11-23 15:26:08
您可以使用Percolator查询(ES文档):
用例示例
创建映射
query:接受带有所有功能的ES查询Title:元数据(可选)PUT /my_index
{
"mappings": {
"properties": {
"Title": {
"type": "text"
},
"query": {
"type": "percolator"
}
}
}
}添加文档
PUT /my_index/_doc/1
{
"query": {
"match_phrase": {
"Title": "Western Europe"
}
},
"Title": "Western Europe"
}获取匹配的查询
POST /my_index/_search
{
"query": {
"percolate": {
"field": "query",
"document": {
"Title": "Apple in Western Europe"
}
}
}
}发布于 2019-11-08 12:23:44
发布于 2019-11-08 14:43:04
我知道Stefan在评论中给出了一个简单而高效的解决方案,但您也可能希望将Span查询视为FYI!
我创建了示例映射、文档、查询和响应:
制图:
PUT my_span_index
{
"mappings": {
"properties": {
"Title":{
"type": "text"
}
}
}
}样本文件:
POST my_span_index/_doc/1
{
"Title": "Western Europe"
}
POST my_span_index/_doc/2
{
"Title": "Eastern Europe"
}
//slop - distance between words Western and Europe here is 13
POST my_span_index/_doc/3
{
"Title": "As far as Western culture is America, we see gradually more and more of the same in Europe"
}Span查询:
POST my_span_index/_search
{
"query": {
"span_near" : {
"clauses" : [
{ "span_term" : { "Title": "western" } },
{ "span_term" : { "Title": "europe" } }
],
"slop" : 12, <---- Distance Between Words
"in_order" : true <---- If order is important
}
}
}响应:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.5420371,
"hits" : [
{
"_index" : "my_span_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.5420371,
"_source" : {
"Title" : "Western Europe"
}
},
{
"_index" : "my_span_index",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.028773852,
"_source" : {
"Title" : "As far as Western culture is America, we see gradually more and more of the same in Europe"
}
}
]
}
}请注意,在响应中,具有id:3的文档也会被返回,但是如果将斜率更改为较小的值,则不会出现它。
如果您的请求有更多的令牌,那么在应用程序端编写/生成长查询将是痛苦的。
希望我能帮上忙!
https://stackoverflow.com/questions/32779023
复制相似问题