【问题标题】:Find occurence of a term in elastic search在弹性搜索中查找术语的出现
【发布时间】:2019-02-16 22:33:53
【问题描述】:

我在 JAVA 中使用 elastic search for autosuggest 并且需要将术语及其出现存储在索引。在索引产品时,可以多次索引特定字符串。为避免这种情况,如果它已经存储,我们必须更新索引词的出现。 弹性搜索 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
    }
  }

如果术语已在弹性搜索中编入索引,我如何更新出现次数?

【问题讨论】:

  • 嗨 Priancy,您可以在索引数据之前对其进行验证。检查它是否在索引中可用。如果是,那么您可以使用递增的出现来更新/替换整个文档。
  • @PKhode 使用了这个查询: curl -X GET "localhost:9200/autosuggest/autosuggest/_search" -H 'Content-Type: application/json' -d' {"size": 0, "query": {"match": { "input": "男士休闲衬衫" }}}' 得到 {"took":0,"timed_out":false,"_shards":{"total":5, “成功”:5,“跳过”:0,“失败”:0},“点击”:{“总”:0,“最大得分”:0.0,“点击”:[]}}。它没有返回确切的文档

标签: java elasticsearch autocomplete autosuggest


【解决方案1】:

由于无法发表评论,因此将其发布为答案。

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

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

还请通过this 链接了解如何跳过此方法并增加出现次数。

谢谢

【讨论】:

  • 这给出了一个解析异常:{"error":{"root_cause":[{"type":"parsing_exception","re​​ason":"预期 [START_OBJECT] 但发现 [VALUE_STRING]", "line":2,"col":1}],"type":"parsing_exception","re​​ason":"预期为 [START_OBJECT] 但找到 [VALUE_STRING]","line":2,"col":1}
  • 您使用的是哪个版本的 ES?在将查询发布到此处之前,我已经在我的系统上运行了查询。此外,您作为答案发布的查询将匹配(读取查询)“完成”下的所有字段。所以如果有多个文本类型的字段,它可能会给你错误的结果。
【解决方案2】:

这行得通:

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

【讨论】:

    猜你喜欢
    • 2020-06-12
    • 2021-07-25
    • 2014-06-02
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多