提升查询,从文档中复制

时间:2015-01-15 11:19:19

标签: elasticsearch

我试图像documentation中所示创建提升查询 但是当我尝试

{
  "query": {
    "match_all":{}
  },
  "boosting": {
    "positive": {
      "term": {
        "is_job_seeking": 1
      }
    },
    "negative": {
      "term": {
        "is_job_seeking": 0
      }
    }
  },
  "negative_boost" : 0.2
}

它不起作用我收到错误

error: SearchPhaseExecutionException[Failed to execute phase [query], all shards failed;
        shardFailures {[bP7jZnVHSRu83G30B0uSmw][idx_users_all_backup_dev][0]: 
        SearchParseException[[idx_users_all_backup_dev][0]: from[-1],size[-1]: 
        Parse Failure [Failed to parse source [
          {
            "boosting": {
              "positive": {
                "term": {
                  "field1":"value1"
                }
              },
              "negative": {
                "term": {
                  "field2":"value2"
                }
              },
              "negative_boost":0.2
            }
          }
        ]]];
        nested: SearchParseException[[idx_users_all_backup_dev][0]: from[-1],size[-1]: 
        Parse Failure [No parser for element [boosting]]]; }{[bP7jZnVHSRu83G30B0uSmw][idx_users_all_backup_dev][1]: 
        SearchParseException[[idx_users_all_backup_dev][1]: from[-1],size[-1]: 
        Parse Failure [Failed to parse source [
          {
            "boosting": {
              "positive": {
                "term": {
                  "field1":"value1"
                }
              },
              "negative": {
                "term": {
                  "field2":"value2"
                }
              },
              "negative_boost":0.2
            }
          }  
        ]]]; 
        nested: SearchParseException[[idx_users_all_backup_dev][1]: from[-1],size[-1]: 
        Parse Failure [No parser for element [boosting]]]; }{[bP7jZnVHSRu83G30B0uSmw][idx_users_all_backup_dev][2]: 
        SearchParseException[[idx_users_all_backup_dev][2]: from[-1],size[-1]: 
        Parse Failure [Failed to parse source [
          {
            "boosting": {
              "positive": {
                "term": {
                  "field1":"value1"
                }
              },
              "negative": {
                "term": {
                  "field2":"value2"
                }
              },
              "negative_boost":0.2
            }
          }
        ]]]; nested: SearchParseException[[idx_users_all_backup_dev][2]: from[-1],size[-1]: 
        Parse Failure [No parser for element [boosting]]]; }{[bP7jZnVHSRu83G30B0uSmw][idx_users_all_backup_dev][3]: 
        SearchParseException[[idx_users_all_backup_dev][3]: from[-1],size[-1]: 
        Parse Failure [Failed to parse source [
          {
            "boosting": {
              "positive": {
                "term": {
                  "field1":"value1"
                }
              },
              "negative": {
                "term": {
                  "field2":"value2"
                }
              },
              "negative_boost":0.2
            }
          }
        ]]]; nested: SearchParseException[[idx_users_all_backup_dev][3]: from[-1],size[-1]: 
        Parse Failure [No parser for element [boosting]]]; }{[bP7jZnVHSRu83G30B0uSmw][idx_users_all_backup_dev][4]: 
        SearchParseException[[idx_users_all_backup_dev][4]: from[-1],size[-1]: 
        Parse Failure [Failed to parse source [
          {
            "boosting": {
              "positive": {
                "term": {
                  "field1":"value1"
                }
              },
              "negative": {
                "term": {
                  "field2":"value2"
                }
              },
              "negative_boost":0.2
            }
          }
        ]]]; 
        nested: SearchParseException[[idx_users_all_backup_dev][4]: from[-1],size[-1]: 
        Parse Failure [No parser for element [boosting]]]; }]
status: 400

我还尝试添加explain: true,但它没有向我提供有关查询错误的任何额外信息。

修改 我写了这样的查询

{
  "sort": [
    {
      "is_active": "asc"
    }
  ],
  "fields": [
    "is_job_seeking", "is_active"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": {
              "term": {
                "is_job_seeking": 1
              }
            }
          }
        },
        {
          "boosting": {
            "positive": {
              "term": {
                "is_active": 1
              }
            },
            "negative": {
              "term": {
                "is_active": 0
              }
            },
            "negative_boost": 0.3
          }
        }
      ]
    }
  }
}

并且此查询为我提供了1000个结果,但没有包含is_active=0的文档,当我删除boosting部分时,我使用文档is_active=0更正结果

1 个答案:

答案 0 :(得分:2)

您的查询应该是这样的:

{
  "query": {
    "boosting": {
      "positive": {
        "term": {
          "is_job_seeking": 1
        }
      },
      "negative": {
        "term": {
          "is_job_seeking": 0
        }
      },
      "negative_boost": 0.2
    }
  }
}

使用bool

{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "text": {
              "value": "something"
            }
          }
        },
        {
          "boosting": {
            "positive": {
              "term": {
                "is_job_seeking": 1
              }
            },
            "negative": {
              "term": {
                "is_job_seeking": 0
              }
            },
            "negative_boost": 0.2
          }
        }
      ]
    }
  }
}