我有两部分查询(a)查询(b)上下文,我想对elasticsearch进行查询,它给出了(a)的结果,这是强制的,并且(b)的结果应该被提升。
例如,如果查询/(A)是“通货膨胀率”,而上下文/(B)是“美国”--结果应该只给出“通货膨胀率”的结果,而“美国”的结果应该得到提升。
我尝试了elasticsearch的各种概念,如- bool/should/filter/,但结果并不理想。下面我给出了我目前正在使用的查询模板。它给出了查询和上下文没有组合的结果,它只给出了匹配查询或上下文的文档。我使用的是ES 2.4.0
{
"template":{
"from":"0",
"size":"10",
"_source": ["*"],
"query":{
"function_score": {
"query":{
"bool":{
"should":[{
"match":{
"display":{
"query": "inflation rate",
"boost": 2
}
}
},
{
"match":{
"attributes.definition": {
"query": "inflation rate",
"boost": 5
}
}
},
{
"match":{
"attributes.topics.name": {
"query": "inflation rate",
"boost": 5
}
}
},
{
"match":{
"attributes.titles": {
"query": "inflation rate",
"boost": 7
}
}
},
{
"match":{
"attributes.definition": {
"query": "United States",
"boost": 4
}
}
},
{
"match":{
"attributes.topics.name": {
"query": "United States",
"boost": 4
}
}
},
{
"match":{
"attributes.titles": {
"query": "United States",
"boost": 4
}
}
}],
"must": [
{
"match":{
"type":"news"
}
}]
}
},
"functions": [{
"gauss": {
"attributes.published_ts": {
"scale": "10d"
}
},
"weight" : 0.1
}]
}
}
}
}发布于 2017-02-19 16:34:21
应该是这样的:
例如:
{
"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"
}
}
}只是一个示例,请根据您自己的需求对其进行调整。
https://stackoverflow.com/questions/42324904
复制相似问题