elasticsearch Elastic Search给出不一致的结果

23c0lvtd  于 5个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(78)

我有一个ElasticSearch查询,它给了我5条记录的未过滤结果集。

{
    "from": 0,
    "size": 250,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "status": {
                            "value": "Active",
                            "boost": 1.0
                        }
                    }
                },
                {
                    "match_phrase_prefix": {
                        "primaryEmail": {
                            "query": "Richard",
                            "slop": 0,
                            "max_expansions": 50,
                            "boost": 1.0
                        }
                    }
                },
                {
                    "match_phrase_prefix": {
                        "firstname": {
                            "query": "Richard",
                            "slop": 0,
                            "max_expansions": 50,
                            "boost": 1.0
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
        }
    }
}

字符串
响应为5条具有不同国家/地区ID的记录:

[
    {
        "firstname": "Richard Maxwell",
        "mobile": 23523453,
        "primaryEmail": "[email protected]",
        "profileVisibility": null,
        "thumbnail": null,
        "updatedBy": null,
        "countryCode": "16539",
        "status":"Active"
    },
    {
        "firstname": "Richard James",
        "mobile": 23523453,
        "primaryEmail": "[email protected]",
        "profileVisibility": null,
        "thumbnail": null,
        "updatedBy": null,
        "countryCode": "92383",
        "status":"Active"
    },
    {
        "firstname": "Richard King",
        "mobile": 23523453,
        "primaryEmail": "[email protected]",
        "profileVisibility": null,
        "thumbnail": null,
        "updatedBy": null,
        "countryCode": "38764",
        "status":"Active"
    },
    {
        "firstname": "Richard Martyn",
        "mobile": 23523453,
        "primaryEmail": "[email protected]",
        "profileVisibility": null,
        "thumbnail": null,
        "updatedBy": null,
        "countryCode": "92383",
        "status":"Active"
    },
    {
        "firstname": "Richard Jules",
        "mobile": 23523453,
        "primaryEmail": "[email protected]",
        "profileVisibility": null,
        "thumbnail": null,
        "updatedBy": null,
        "countryCode": "92383",
        "status":"Active"
    }
]


现在,如果我对coutryId使用过滤器,

{
    "from": 0,
    "size": 250,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "countryId": {
                            "value": "92383",
                            "boost": 1.0
                        }
                    }
                },
                {
                    "term": {
                        "status": {
                            "value": "Active",
                            "boost": 1.0
                        }
                    }
                },
                {
                    "match_phrase_prefix": {
                        "primaryEmail": {
                            "query": "Richard",
                            "slop": 0,
                            "max_expansions": 50,
                            "boost": 1.0
                        }
                    }
                },
                {
                    "match_phrase_prefix": {
                        "firstname": {
                            "query": "Richard",
                            "slop": 0,
                            "max_expansions": 50,
                            "boost": 1.0
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
        }
    }
}


它可以工作,过滤掉一些countryId上的记录,但它对单个countrId不起作用。这种行为的可能根本原因是什么?
Elastic search term query应该在所有可能的场景中对字段进行过滤,但奇怪的是,它只适用于国家代码的某些值,而deos不适用于特定的国家代码。查询或ElasticSearch设置是否存在问题。是什么导致了这种行为?

vmjh9lq9

vmjh9lq91#

Elasticsearch查询的结果不一致可能由多种因素引起。以下是一些常见的原因和潜在的解决方案:
索引问题:
确保数据已正确编入索引。如果您对数据进行了更改但尚未重新编入索引,则搜索结果可能不是最新的。请检查索引和查询期间使用的分析器。如果它们不匹配,则可能导致意外结果。查询结构:
检查您的查询结构以确保其准确反映您的搜索要求。确保您的查询格式正确,并且您使用的查询类型适合您的用例(例如,匹配、术语、范围)。Map冲突:
请确认您的Map在索引中的所有文档之间都是一致的。不一致的Map可能会导致意外的结果。使用_mapping端点检查索引的Map。分片分配:
Elasticsearch将数据分布在分片上,有时分布可能不均匀。这可能会导致结果不一致。检查分片分配并考虑优化它以获得更均匀的分布。节点故障:
如果Elasticsearch集群中有多个节点,请检查是否存在节点故障或连接问题。如果查询被路由到遇到问题的节点,可能会出现不一致的结果。刷新间隔:
Elasticsearch有一个刷新间隔,在此期间,搜索看不到更改。如果您在更新后立即进行查询,您可能看不到最新的更改。您可以手动刷新索引或根据您的需求调整刷新间隔。分数归一化:
如果您要按相关性分数排序或过滤,请注意分数可能会因文档长度等因素而异。使用field_value_factor等函数可基于特定字段规范化分数。查询缓存:
Elasticsearch对查询使用缓存,有时缓存的结果会导致明显的不一致。尝试禁用查询缓存,看看它是否会影响结果。
使用Elasticsearch的调试工具,例如_validate和_explain端点,检查查询的解释和评分方式。版本兼容性:
请确保您的客户端应用程序的Elasticsearch版本与Elasticsearch服务器版本兼容。如果问题仍然存在,请提供有关查询,Map和数据结构的特定详细信息将有助于更准确的诊断。此外,检查Elasticsearch日志中的任何错误消息或警告可以深入了解潜在问题。

相关问题