search - 跨多个字段的not_analyzed字符串数组完全匹配

标签 search elasticsearch match search-engine

这是针对elasticsearch 2.3

假设我有一个这样的映射:

索引1:

'title': {'type': 'string'},
'tags': {'type': 'string', 'index': 'not_analyzed'} 

索引2:
'title': {'type': 'string'},
'tags': {'type': 'string', 'index': 'not_analyzed'},
'tag_special': {'type': 'string', 'index': 'not_analyzed'}

注意:当推送到index1和index2时,“标签”是一个字符串数组。在index2中,“tag_special”只是一个字符串

我要完成的查询是在两个索引之间进行查询,首先查找index1中的标记数组中的term精确匹配,或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”,它将返回所有帖子)

一旦可以对字符串数组进行术语查询,就需要将完全匹配项提升到结果的顶部,然后对标题字段使用标准匹配项

可以确定,术语查询实际上都不匹配任何内容,它们必须是完全可选的。因此,术语“匹配项”不能充当filtersconstant_score,因为我需要能够进行常规的标题查询并按得分值对结果进行排序

我到目前为止所拥有的是
"query": {
            "bool": {
                "should": [
                    {"term": {"tags": "tag-2"}},
                    {"term": {"tag_special": "tag-2"}},
                    {"match": {"title": {"query": "tag-2", "operator": "and"}}}
                ],
            }
        }

但是从第二秒开始,什么也没有返回。似乎还没有使用multi_match,因为它使用了match子句

我觉得我要完成的工作实际上很简单,就像这里只缺少一件事,而在这里是因为经过数小时的反复试验,几乎要浪费时间了,我希望明天能继续前进早上哈哈

谢谢你的时间!

最佳答案

我做的和您做的一样,您的示例文档得到了结果。您发布的查询中存在一个小问题,因为bool查询中只有“,”,尽管应该只有一个。所以查询应该是这样的。

"query": {
        "bool": {
            "should": [
                {"term": {"tags": "tag-2"}},
                {"term": {"tag_special": "tag-2"}},
                {"match": {"title": {"query": "tag-2", "operator": "and"}}}
            ]
        }
    }

如果这样做不起作用,请确保已将标签和tag_special字段设置为not_analyzedGET index1/_mapping应该显示此结果
"index1": {
  "mappings": {
     "index1": {
        "properties": {
           "tags": {
              "type": "string",
              "index": "not_analyzed"
           },
           "title": {
              "type": "string"
           }
        }
     }
  }
}

如果标签和tag_special字段是analyzed,则字词查询不会给出任何结果

关于search - 跨多个字段的not_analyzed字符串数组完全匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40600972/

相关文章:

php - 从 db 查询 blob 时,如何在查询中将 blob 转换为文本?

php - elasticsearch php客户端忽略主机端口

regex - Mongo中不区分大小写的搜索

python - 如何在 Python 3.10 的匹配大小写中使用带空格的字符串

c - 查找是否存在任何 i 以便 array[i] 等于 i 的算法

ios - Swift3 iOS - 如何在 URL 数组 [URL?] 中搜索 URL

Java新手: infinite loop searching for specific text in a file

ruby-on-rails - ElasticSearch删除查询

javascript - Elasticsearch javascript 数组中的搜索字段

unix - 根据匹配字段删除第一个重复行并保留第二个匹配行