Elasticsearch查询字符串精确匹配

5

我有一个包含字段名为video且值为1.flv的索引。如果我执行以下查询:

"query": {
    "query_string": {
        "query": "2.flv"
    }
}

查询仍然会返回所有带有1.flv的记录。

有没有人能指点我正确的解决方案?

这是针对1.flv返回的示例数据(正如您所看到的,没有任何内容包含2.flv!)

  "hits" : {
    "total" : 8,
    "max_score" : 0.625,
    "hits" : [ {
      "_index" : "videos",
      "_type" : "comment",
      "_id" : "_meta",
      "_score" : 0.625,
      "fields" : {
        "video" : "1.flv",
        "body" : "Really?"
      }
    }, {
      "_index" : "videos",
      "_type" : "comment",
      "_id" : "0fYsYOTHT7O-7P6CVi7l3w",
      "_score" : 0.625,
      "fields" : {
        "video" : "1.flv",
        "body" : "fadsfasfas"
      }
    }, {
      "_index" : "videos",
      "_type" : "comment",
      "_id" : "O9VjgFdmQra6hYxwMdGuTg",
      "_score" : 0.48553526,
      "fields" : {
        "video" : "1.flv",
        "body" : "Search is hard. Search should be easy."
      }
    }, {
      "_index" : "videos",
      "_type" : "comment",
      "_id" : "A6k3FEKKSzKTSAVIT-4EbA",
      "_score" : 0.48553526,
      "fields" : {
        "video" : "1.flv",
        "body" : "Really?"
      }
    }, {
      "_index" : "videos",
      "_type" : "comment",
      "_id" : "eFnnM4PrTSyW6wfxHWdE8A",
      "_score" : 0.48553526,
      "fields" : {
        "video" : "1.flv",
        "body" : "Hello!"
      }
    }, {
      "_index" : "videos",
      "_type" : "comment",
      "_id" : "ZogAiyanQy6ddXA3o7tivg",
      "_score" : 0.48553526,
      "fields" : {
        "video" : "1.flv",
        "body" : "dcxvxc"
      }
    }, {
      "_index" : "videos",
      "_type" : "comment",
      "_id" : "O0HcT7aGTrqKQxF25KsOwQ",
      "_score" : 0.37158427,
      "fields" : {
        "video" : "1.flv",
        "body" : "Hello!"
      }
    }, {
      "_index" : "videos",
      "_type" : "comment",
      "_id" : "l2d53OFITb-etooWEAI0_w",
      "_score" : 0.37158427,
      "fields" : {
        "video" : "1.flv",
        "body" : "dasdas"
      }
    } ]
  }
}

你能发布你的映射和期望的结果吗? - moliware
还有分析器的设置吗? - shyos
@shyos 我不确定如何获取它。我需要1.flv完全匹配1.flv和2.flv仅完全匹配2.flv(而不是1.flv)。 - user2786037
1个回答

6
您所看到的是标准分词器(默认/标准分析器的一部分)的结果,它会在许多字符上进行分词,如句号(.)。请参见此示例以了解它的分析过程。
有许多方法可以使用Elasticsearch完成您想要的操作,例如更新映射并将video字段的分析器更改为例如keyword分析器,可能使用多字段类型,将字段映射配置为index:not_analyzed等。但对于您来说,一个可能足够好的简单解决方案是确保使用了AND运算符。
默认情况下,query string query使用OR运算符:

default_operator:如果未指定显式运算符,则使用默认运算符。例如,默认运算符为OR,则查询“匈牙利首都”被转换为capital OR of OR Hungary,而如果默认运算符为AND,则相同查询将被转换为capital AND of AND Hungary。默认值为OR。

因此,要么明确指定运算符,要么将其设置为默认值。此示例还展示了这两种技术的使用方法(右下方的Search#1和Search#2选项卡)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接