查询 returns 两个文件而不是一个

Query returns both documents instead of just one

var res = esclient.Search<MyClass>(q => q
                .Query(fq => fq
                    .Filtered(fqq => fqq
                        .Query(qq => qq.MatchAll())
                        .Filter(ff => ff
                            .Bool(b => b
                                .Must(m1 => m1.Term("macaddress", "mac"))
                                .Must(m2 => m2.Term("another_field", 123))
                            )
                        )
                    )
                )
            );

据我所知,boolmust 一起相当于 AND 运算符。我有一个包含两个文档的索引,只有一个与两个约束(macaddressanother_field)匹配,但 Elasticsearch returns 两者都匹配。

这是映射:

{
   "reviewer-test-index": {
      "aliases": {},
      "mappings": {
         "historyRecord": {
            "properties": {
               "groupName": {
                  "type": "string"
               },
               "groupNo": {
                  "type": "integer"
               },
               "instrType": {
                  "type": "integer"
               },
               "instrumentAddress": {
                  "type": "string"
               },
               "insturmentName": {
                  "type": "string"
               },
               "macAddr": {
                  "type": "string"
               },
               "uhhVersion": {
                  "type": "string"
               }
            }
         },         
      "settings": {
         "index": {
            "creation_date": "1434557536720",
            "number_of_shards": "1",
            "number_of_replicas": "0",
            "version": {
               "created": "1050299"
            },
            "uuid": "FfQADLGVQVOPV3913exKsw"
         }
      },
      "warmers": {}
   }
}

我也尝试进行 JSON 查询,但我得到 0 个匹配:

GET _search
{
  "query" :{
  "filtered": {
    "query": {
      "match_all": { }
    },
   "filter": {
      "bool" : {
            "must" : [
                {"term" : { "macAddr" : "000A8D810F5A" } },
                {"term" : { "insturmentName" : "Amin's furnace" } },
                {"term" : { "instrumentAddress" : "8D810F5A"}},
                {"term" : { "uhhVersion" :  "v2.5"}},
                {"term" : { "groupName" :  "Amin's Group"}},
                {"term" : { "groupNo" :  2}},
                {"term" : { "instrType" :  60}}
            ]
         }
    }
  }
  }
}

Response:

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 4,
      "successful": 3,
      "failed": 0
   },
   "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
   }
}

term 过滤器不分析要搜索的文本。意思是,如果您搜索 000A8D810F5A,这正是要搜索的内容(包括大写字母)。

但是,您的 macAddrinsturmentName 字段以及其他字段只是 string。意思是,他们使用 standard 分析器将术语小写。因此,您正在搜索 000A8D810F5A 但在索引中您可能有 000a8d810f5a(注意小写字母)。

因此,您需要拥有这些字段 not_analyzed 或使用 keyword 分析器进行分析,或者,如果您想让它们默认使用 standard 分析器进行分析,请添加一个.raw 应该 not_analyzedkeyword 分析的多字段,并在您的搜索中使用它。例如,"term" : { "macAddr.raw" : "000A8D810F5A" }.

建议的映射:

  "mappings": {
     "historyRecord": {
        "properties": {
           "groupName": {
              "type": "string",
              "fields": {
                 "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                 }
              }
           },
           "groupNo": {
              "type": "integer"
           },
           "instrType": {
              "type": "integer"
           },
           "instrumentAddress": {
              "type": "string",
              "fields": {
                 "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                 }
              }
           },
           "insturmentName": {
              "type": "string",
              "fields": {
                 "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                 }
              }
           },
           "macAddr": {
              "type": "string",
              "fields": {
                 "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                 }
              }
           },
           "uhhVersion": {
              "type": "string",
              "fields": {
                 "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                 }
              }
           }
        }
     }

建议查询:

{
  "query" :{
  "filtered": {
    "query": {
      "match_all": { }
    },
   "filter": {
      "bool" : {
            "must" : [
                {"term" : { "macAddr.raw" : "000A8D810F5A" } },
                {"term" : { "insturmentName.raw" : "Amin's furnace" } },
                {"term" : { "instrumentAddress.raw" : "8D810F5A"}},
                {"term" : { "uhhVersion.raw" :  "v2.5"}},
                {"term" : { "groupName.raw" :  "Amin's Group"}},
                {"term" : { "groupNo" :  2}},
                {"term" : { "instrType" :  60}}
            ]
         }
    }
  }
  }
}