这是给elasticsearch 2.3的
让我们假设我有这样的映射:
index1:
'title': {'type': 'string'},
'tags': {'type': 'string', 'index': 'not_analyzed'} index2:
'title': {'type': 'string'},
'tags': {'type': 'string', 'index': 'not_analyzed'},
'tag_special': {'type': 'string', 'index': 'not_analyzed'}注意:当推到index1和index2时,“标记”是一个字符串数组。在index2中,"tag_special“只是一个字符串
我想要完成的是一个查询,其中我们跨两个索引进行查询,首先在index1中的标记数组中查找确切的tag_special匹配,或者在index2中查找tag_special的单个字符串值,并将这些匹配提升到堆的顶部。然后,我想接受相同的查询,然后在两个索引中针对标题字段运行一个普通的match查询。
示例文档
{
"_index": "index1",
"_type": "index1",
"title": "Test Doc 1",
"tags": ["tag-1", "tag-2"]
}
{
"_index": "index1",
"_type": "index1",
"title": "Test Doc 2",
"tags": ["tag-1"]
}
{
"_index": "index1",
"_type": "index1",
"title": "Test Doc 3",
"tags": ["tag-2", "tag-3"]
}
{
"_index": "index2",
"_type": "index2",
"title": "Test Doc inx2 1",
"tags": ["tag-1", "tag-2"],
"tag_special": "tag-1"
}
{
"_index": "index2",
"_type": "index2",
"title": "Test Doc inx2 2",
"tags": ["tag-2"]
}
{
"_index": "index2",
"_type": "index2",
"title": "Test Doc inx2 3",
"tags": ["tag-3"],
"tag_special": "tag-4"
}当然,我正在尝试的任何事情都没有很好的效果。
"query": {
"bool": {
"should": [
{"term": {"tags": "tag-2"}},
]
}
}奇怪的是,我什么也没回,但是
"query": {
"bool": {
"should": [
{"match": {"tags": "tag-2"}},
]
}
}返回太多(如果您使用分析器查找"tag-2“并搜索"tag”和“2”,则会返回所有帖子)。
一旦我可以对字符串数组进行术语查询,我就需要将准确的匹配提升到结果的顶部,然后使用针对title字段的标准匹配。
没有一个术语与任何查询相匹配,这应该是可以的,它们必须是完全可选的。因此,术语匹配不能充当filters或constant_score,因为我需要能够进行正常的标题查询,并按分数值排序结果。
我到目前为止
"query": {
"bool": {
"should": [
{"term": {"tags": "tag-2"}},
{"term": {"tag_special": "tag-2"}},
{"match": {"title": {"query": "tag-2", "operator": "and"}}}
],
}
}但就在这一秒,什么都没有归还。使用multi_match似乎也已退出,因为它使用了match子句。
我觉得我想要完成的事情其实很简单,就像我在这里错过了一件事,而我在这里,因为经过几个小时的尝试和错误,这是我即将退出的时候,我希望我明天早上能做些什么。
耽误您时间,实在对不起!
发布于 2016-11-15 11:45:21
我和你一样做了同样的事情,我得到了示例文档的结果。您发布的查询中有一个小问题,因为在bool查询中有“",尽管只有一个应该这样做。所以查询应该是这样的。
"query": {
"bool": {
"should": [
{"term": {"tags": "tag-2"}},
{"term": {"tag_special": "tag-2"}},
{"match": {"title": {"query": "tag-2", "operator": "and"}}}
]
}
}如果没有工作,请确保将标记和tag_special字段设置为not_analyzed
GET index1/_mapping应该显示这个结果
"index1": {
"mappings": {
"index1": {
"properties": {
"tags": {
"type": "string",
"index": "not_analyzed"
},
"title": {
"type": "string"
}
}
}
}
}如果标记和tag_special字段是analyzed,则术语查询不会给出任何结果。
发布于 2016-11-15 21:01:14
解决了我的问题。这和任何事都没有关系,但无论如何我都会回答的。
我有两个问题,其实在这里不会显示,但作为一个警示性的故事。
array用作一个无关字段的类型,而不是string。显然,它并没有告诉我有一个错误,它只是决定使地图完全失效,在文件上传之前不费吹灰之力。当我在那个领域把array改成string的时候,一切都变得很有魅力。https://stackoverflow.com/questions/40600972
复制相似问题