Elasticsearch中的字符串查询返回不带查询字段的值

时间:2020-09-22 11:23:20

标签: elasticsearch

我在ES中有一些文档,其中包含一些字段和“团队”字段。始终选中“团队”字段,通常还会选中其他字段。问题是,当我检查某些不存在的字段时,会得到很多结果。例如。这样的查询:

GET localhost:9200/index/_search

{
    "query": {
        "query_string": {
            "query": "(asdf:Ssdfdfdf) AND team.keyword:(\"my-team\")"
        }
    }
}

返回数千个结果,很明显,它们都不包含字段asdf

要摆脱这种结果,我需要设置什么?

编辑:

我尝试过"explain": true,这就是我得到的:

            "_explanation": {
                "value": 8.43526E-4,
                "description": "weight(team.keyword:my-team in 9) [PerFieldSimilarity], result of:",
                "details": [
                    {
                        "value": 8.43526E-4,
                        "description": "score(doc=9,freq=1.0 = termFreq=1.0\n), product of:",
                        "details": [
                            {
                                "value": 8.43526E-4,
                                "description": "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:",
                                "details": [
                                    {
                                        "value": 592.0,
                                        "description": "docFreq",
                                        "details": []
                                    },
                                    {
                                        "value": 592.0,
                                        "description": "docCount",
                                        "details": []
                                    }
                                ]
                            },
                            {
                                "value": 1.0,
                                "description": "tfNorm, computed as (freq * (k1 + 1)) / (freq + k1) from:",
                                "details": [
                                    {
                                        "value": 1.0,
                                        "description": "termFreq=1.0",
                                        "details": []
                                    },
                                    {
                                        "value": 1.2,
                                        "description": "parameter k1",
                                        "details": []
                                    },
                                    {
                                        "value": 0.0,
                                        "description": "parameter b (norms omitted for field)",
                                        "details": []
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }

编辑2:

我上传的文档:

{
  "first_name":"Paweł",
  "last_name":"Stawicki",
  "height":180,
  "birth_date":"28/01/1979",
  "team":"my-team",
  "children": [
    {
      "name": "Lidka",
      "metrics": [ 0.38, 1.33, 0.3, 0.32 ]
    },
    {
      "name": "Tymek",
      "metrics": [ 5.3, 0.2, 3.22, 4.00, 5.02 ]
    }
  ],
  "relation": "capsule"
}

编辑3:

索引设置:

{
  "test_index": {
    "aliases": {},
    "mappings": {
        "_doc": {
            "properties": {
                "birth_date": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "children": {
                    "properties": {
                        "metrics": {
                            "type": "float"
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                },
                "first_name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "height": {
                    "type": "long"
                },
                "last_name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "relation": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "team": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    },
    "settings": {
        "index": {
            "creation_date": "1587478667465",
            "number_of_shards": "5",
            "number_of_replicas": "1",
            "uuid": "ziQLIaqUQcSOtpokChzGCg",
            "version": {
                "created": "6070099"
            },
            "provided_name": "test_index"
        }
    }
  }
}

我也注意到了一些奇怪的事情。我有2个ES实例。其中一个行为不当(即使按不存在的字段进行查询也返回文档),另一方面则不行,当查询中不存在的字段时不返回任何文档。

1 个答案:

答案 0 :(得分:0)

您可以在查询字符串前面加上_exists_子查询,以仔细检查并预先过滤掉不适用的文档:

{
  "query": {
    "query_string": {
      "query": "_exists_:asdf AND asdf:Ssdfdfdf AND team.keyword:(\"my-team\")"
    }
  }
}

尽管这并不必要,因为您已经有了一个AND查询...