簡體   English   中英

使用另一個搜索對 Elastic 結果進行后處理(從 Solr 遷移)

[英]Postprocessing Elastic results with another search (migrating from Solr)

I'm currently migrating an application from Solr to Elastic and stumbled over an interesting Solr feature that I cannot reproduce in Elastic: The query to Solr returns a postprocessing flag that does a quality check on the result, indicating wether all tokens are found in the結果字段。

q  = some_field:(the brown fox)
fl = some_field, full_match:exists(query({!edismax v='some_field:(the brown fox)' mm='100%'}))

Solr 結果如下所示:

{
    "response": {
        "docs": [
            {
                "some_field": "The Brown Bear",
                "full_match": false
            },
            {
                "some_field": "The Quick Brown Fox",
                "full_match": true
            }
        ]
    }
}

客戶端使用該標志來進一步處理結果文檔,與分數無關(我在示例中省略了)。 我發現這很聰明,因為使用 Solr 的標記化和分布式計算能力,而不是在客戶端做所有事情。

現在在 Elastic 中,我假設這應該在script_fields塊中完成,但實際上我不知道如何使用無痛腳本執行子查詢,經過兩天的調查,我懷疑這是否可能:

{
    "query": {
        "match": {
            "some_field": "the brown fox"
        }
    },
    "_source": [
        "some_field"
    ],
    "script_fields": {
        "full_match": {
            "script": "???" <-- Search with Painless script?
        }
    }
}

歡迎任何有創意的想法。

將 Elasticsearch 的命名查詢minimum_should_match參數結合使用並將其設置為 100% 以僅匹配所有標記匹配的文檔怎么樣?

然后,您將能夠檢測到響應中所有標記都匹配的查詢。 您還可以設置 "boost": 0 以避免影響主查詢的分數。

這是一個示例請求:

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "message": {
                            "query": "the brown fox",
                            "_name": "main_query"
                        }
                    }
                },
                {
                    "match": {
                        "message": {
                            "query": "the brown fox",
                            "_name": "all_tokens_match",
                            "minimum_should_match": "100%",
                            "boost": 0
                        }
                    }
                }
            ]
        }
    }
}

然后你會得到一個看起來有點像這樣的響應:

{
    "hits": [
        {
            "_score": 0.99938476,
            "_source": {
                "message": "The Quick Brown Fox"
            },
            "matched_queries": [
                "main_query",
                "all_tokens_match"
            ]
        },
        {
            "_score": 0.38727614,
            "_source": {
                "message": "The Brown Bear"
            },
            "matched_queries": [
                "main_query"
            ]
        }
    ]
}

查詢中所有標記匹配的文檔將在響應的matched_queries部分中包含all_tokens_match

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM