首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >elasticsearch查询,用于在相同字段上搜索两个不同的查询,并获得包含这两个查询的组合的结果

elasticsearch查询,用于在相同字段上搜索两个不同的查询,并获得包含这两个查询的组合的结果
EN

Stack Overflow用户
提问于 2017-02-19 15:55:15
回答 1查看 50关注 0票数 0

我有两部分查询(a)查询(b)上下文,我想对elasticsearch进行查询,它给出了(a)的结果,这是强制的,并且(b)的结果应该被提升。

例如,如果查询/(A)是“通货膨胀率”,而上下文/(B)是“美国”--结果应该只给出“通货膨胀率”的结果,而“美国”的结果应该得到提升。

我尝试了elasticsearch的各种概念,如- bool/should/filter/,但结果并不理想。下面我给出了我目前正在使用的查询模板。它给出了查询和上下文没有组合的结果,它只给出了匹配查询或上下文的文档。我使用的是ES 2.4.0

代码语言:javascript
复制
{
  "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
        }]
      }
    }
  } 
}
EN

回答 1

Stack Overflow用户

发布于 2017-02-19 16:34:21

应该是这样的:

例如:

代码语言:javascript
复制
{           
    "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"
        }
    }
}

只是一个示例,请根据您自己的需求对其进行调整。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42324904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档