在弹性搜索中查找术语的出现

时间:2018-09-12 10:03:18

标签: java elasticsearch autocomplete autosuggest

  

我正在 JAVA 中使用 弹性搜索来自动建议 ,并且需要将这些术语及其出现的位置存储在在索引产品的同时,可以对一个特定的字符串进行多次索引。为避免这种情况,如果已经存储了该字符串,我们必须更新索引项的出现情况。   弹性搜索POJO:

@Document(indexName = "autosuggest", type = "autosuggest")
public class Autosuggest {

@Id
@Field(pattern ="id")
private String id;

@Field(pattern ="completion")
private Completion completion;

@Field(pattern ="occurence")
private Integer occurence;

public String getId() {
    return id;
}

public Completion getCompletion() {
    return completion;
}

public void setCompletion(Completion completion) {
    this.completion = completion;
}

public Integer getOccurence() {
    return occurence;
}

public void setOccurence(Integer occurence) {
    this.occurence = occurence;
}

}
  

完成对象

public class Completion {

private List<String> input;
private Integer weight;

public List<String> getInput() {
    return input;
}
public void setInput(List<String> input) {
    this.input = input;
}
public Integer getWeight() {
    return weight;
}
public void setWeight(Integer weight) {
    this.weight = weight;
}
public Completion(List<String> input, Integer weight) {
    super();
    this.input = input;
    this.weight = weight;
}
}
  

在弹性搜索中采样对象

  {
    "_index" : "autosuggest",
    "_type" : "autosuggest",
    "_id" : "BUj0zGUBr5AQqSH41l0m",
    "_score" : 1.0,
    "_source" : {
      "completion" : {
        "input" : [
          "Casual Shirts for Men"
        ],
        "weight" : 2
      },
      "occurence" : 1
    }
  }
  

如果该词已在弹性搜索中编入索引,如何更新该词的出现?

2 个答案:

答案 0 :(得分:0)

将其发布为答案,因为我无法发表评论。

@Priancy:您使用的查询似乎不正确。请在下面找到正确的查询。我已经使用问题中提供的示例对象测试了此查询。

"query": {
    "match": {
      "completion.input": "Casual Shirts for Men"
    }
}

也请通过this链接了解如何跳过此方法并增加事件的发生。

谢谢

答案 1 :(得分:0)

这有效:

  "query": {
     "match": {
         "completion": "Casual Shirts for Men"
      }
  }