elasticsearch查询在相同字段上搜索2个不同的查询,并获得具有两个查询组合的结果

时间:2017-02-19 07:55:15

标签: elasticsearch

我有2个部分的查询 (一个问题 (b)背景, 我想对弹性搜索进行查询,它给出了(a)的结果,这是强制性的,并且应该提高(b)的结果。

例如,

如果查询/(a)是"通货膨胀率"和背景/(b)是"美国" - 结果应该只给出"通货膨胀率"和"美国"的结果应该得到提升。

我尝试了各种弹性搜索的概念,比如 - bool / should / filter /,但结果并没有像预期的那样。 下面我给出了我目前使用的查询模板。它给出了查询和上下文没有组合的结果,它只提供了匹配查询或上下文的文档。 我使用的是ES 2.4.0

function myFunction() {
    var str = 12.234556; 
    str = str.toString().split('.');
    var res = str[1].slice(0, 2);
    document.getElementById("demo").innerHTML = str[0]+'.'+res;
}

// output: 12.23

1 个答案:

答案 0 :(得分:0)

应该是这样的:
例如

{           
    "query": {
        "function_score": {
            "filter": {
                "bool": {
                    "must": [{  //here can be should, must etc ... This is your first condition.
                        "match": {
                            "attributes.definition": {
                                "query": "inflation rate",
                                "boost": 5
                            }
                        }
                    }, {
                        "match": {
                            "attributes.topics.name": {
                                "query": "inflation rate",
                                "boost": 7
                            }
                        }
                    }]
                }
            },
            "functions": [{  //This is your second condition. Just to give more relevance to the topics with United States.
                "filter": {
                    "term": {
                        "attributes.topics.name": "United States"
                    }
                },
                "weight": 3
            }, {
                "field_value_factor": {
                    "field": "views",
                    "modifier": "log1p"
                }
            }],
            "score_mode": "sum"
        }
    }
}

只是一个例子,根据您自己的要求进行调整。

相关问题