如何使用现有的ElasticSearch索引实现自动完成搜索字段(建议者)?

时间:2016-04-28 16:21:00

标签: java elasticsearch vaadin

ES索引由两种隐式映射的类型组成(默认映射)。一种类型是" person"或作者,第二类是"文件"。 该索引有大约500k条目。 我要做的是:实现自动完成(建议)功能,其中只有字段" title","分类" (文件)和"名称" (作者)与向用户显示的建议相关。

可以在不更改索引中的500k文档的情况下完成吗? 我找到了一些教程,建议准备一个特定的映射,并改变文件(我希望尽可能避免)等等,但我是新手,我不知道如何解决这个问题? 下面是索引的JSON以及文档的外观:

//a Document
{
  "rawsource": "Phys.Rev. D67 (2003) 084031",
  "pubyear": 2003,
  "citedFrom": 19,
  "topics": [
    {
      "name": "General Relativity and Quantum Cosmology"
    }
  ],
  "cited": [
    {
      "ref": 0,
      "id": "PN132433"
    },
    {
      "ref": 1,
      "id": "PN206900"
    }
  ],
  "id": "PN120001",
  "collection": "PN",
  "source": "Phys Rev D",
  "classification": "Physics",
  "title": "Observables in causal set cosmology",
  "url": "http://arxiv.org/abs/gr-qc/0210061",
  "authors": [
    {
      "name": "Brightwell, Graham"
    },
    {
      "name": "Dowker, H. Fay"
    },
    {
      "name": "Garcia, Raquel S."
    },
    {
      "name": "Henson, Joe"
    },
    {
      "name": "Sorkin, Rafael D."
    }
  ]
}

//a Person (author)

{
    "name": "Terasawa, M.",
    "documents": [
        {
            "citedFrom": 0,
            "id": "PN039187"
        }
    ],
    "coAuthors": [
        {
            "name": "Famiano, M. A.",
            "count": "1"
        },
        {
            "name": "Boyd, R. N.",
            "count": "1"
        }
    ],
    "topics": [
        {
            "name": "Astrophysics",
            "count": "1"
        }
    ]
}

//the mapping (implicit/default)

{
  "dlsnew": {
    "aliases": {

    },
    "mappings": {
      "person": {
        "properties": {
          "coAuthors": {
            "properties": {
              "count": {
                "type": "string"
              },
              "name": {
                "type": "string"
              }
            }
          },
          "documents": {
            "properties": {
              "citedFrom": {
                "type": "long"
              },
              "id": {
                "type": "string"
              }
            }
          },
          "name": {
            "type": "string"
          },
          "referenced": {
            "properties": {
              "count": {
                "type": "string"
              },
              "id": {
                "type": "string"
              }
            }
          },
          "topics": {
            "properties": {
              "count": {
                "type": "string"
              },
              "name": {
                "type": "string"
              }
            }
          }
        }
      },
      "document": {
        "properties": {
          "abstract": {
            "type": "string"
          },
          "authors": {
            "properties": {
              "name": {
                "type": "string"
              }
            }
          },
          "cited": {
            "properties": {
              "id": {
                "type": "string"
              },
              "ref": {
                "type": "long"
              }
            }
          },
          "citedFrom": {
            "type": "long"
          },
          "classification": {
            "type": "string"
          },
          "collection": {
            "type": "string"
          },
          "id": {
            "type": "string"
          },
          "pubyear": {
            "type": "long"
          },
          "rawsource": {
            "type": "string"
          },
          "source": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "topics": {
            "properties": {
              "name": {
                "type": "string"
              }
            }
          },
          "url": {
            "type": "string"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1454247029258",
        "number_of_shards": "5",
        "uuid": "k_CyQaxwSAaae67wW98HyQ",
        "version": {
          "created": "1050299"
        },
        "number_of_replicas": "1"
      }
    },
    "warmers": {

    }
  }
}

实现将使用JAVA和Vaadin Framework完成(此时此内容并不相关,但Java / Vaadin中的示例将受到欢迎)。 感谢。

2 个答案:

答案 0 :(得分:0)

所以,我认为我在Elasticsearch方面解决了我的问题,或者至少为我和手头的任务解决了问题。我跟着这个ruby example

我必须重新索引所有文档以适应索引的新设置并明确更改我的映射。

他们的关键在于在这种情况下定义合适的分析器和edgeNGram过滤器,如下所示:

"settings": {
     "index": {
        "analysis": {
           "filter": {
              "def_ngram_filter": {
                 "min_gram": "1",
                 "side": "front",
                 "type": "edgeNGram",
                 "max_gram": "16"
              }
           },
           "analyzer": {
              "def_search_analyzer": {
                 "filter": [
                    "lowercase",
                    "asciifolding"
                 ],
                 "type": "custom",
                 "tokenizer": "def_tokenizer"
              },
              "def_ngram_analyzer": {
                 "filter": [
                    "lowercase",
                    "asciifolding",
                    "def_ngram_filter"
                 ],
                 "type": "custom",
                 "tokenizer": "def_tokenizer"
              },
              "def_shingle_analyzer": {
                 "filter": [
                    "shingle",
                    "lowercase",
                    "asciifolding"
                 ],
                 "type": "custom",
                 "tokenizer": "def_tokenizer"
              },
              "def_default_analyzer": {
                 "filter": [
                    "lowercase",
                    "asciifolding"
                 ],
                 "type": "custom",
                 "tokenizer": "def_tokenizer"
              }
           },
           "tokenizer": {
              "def_tokenizer": {
                 "type": "whitespace"
              }
           }
        }
     }
  }

并在映射中使用这些来搜索字段,如下所示:

"mappings": {
     "person": {
        "properties": {
           "coAuthors": {
              "properties": {
                 "count": {
                    "type": "string"
                 },
                 "name": {
                    "type": "string"
                 }
              }
           },
           "documents": {
              "properties": {
                 "citedFrom": {
                    "type": "long"
                 },
                 "id": {
                    "type": "string"
                 }
              }
           },
           "name": {
              "type": "string",
              "analyzer": "def_default_analyzer",
              "fields": {
                 "ngrams": {
                    "type": "string",
                    "index_analyzer": "def_ngram_analyzer",
                    "search_analyzer": "def_search_analyzer"
                 },
                 "shingles": {
                    "type": "string",
                    "analyzer": "def_shingle_analyzer"
                 },
                 "stemmed": {
                    "type": "string",
                    "analyzer": "def_snowball_analyzer"
                 }
              }
           },
           "referenced": {
              "properties": {
                 "count": {
                    "type": "string"
                 },
                 "id": {
                    "type": "string"
                 }
              }
           },
           "topics": {
              "properties": {
                 "count": {
                    "type": "string"
                 },
                 "name": {
                    "type": "string"
                 }
              }
           }
        }
     },
     "document": {
        "properties": {
           "abstract": {
              "type": "string"
           },
           "authors": {
              "properties": {
                 "name": {
                    "type": "string",
                    "analyzer": "def_default_analyzer",
                    "fields": {
                       "ngrams": {
                          "type": "string",
                          "index_analyzer": "def_ngram_analyzer",
                          "search_analyzer": "def_search_analyzer"
                       },
                       "shingles": {
                          "type": "string",
                          "analyzer": "def_shingle_analyzer"
                       },
                       "stemmed": {
                          "type": "string",
                          "analyzer": "def_snowball_analyzer"
                       }
                    }
                 }
              }
           },
           "cited": {
              "properties": {
                 "id": {
                    "type": "string"
                 },
                 "ref": {
                    "type": "long"
                 }
              }
           },
           "citedFrom": {
              "type": "long"
           },
           "classification": {
              "type": "string"
           },
           "collection": {
              "type": "string"
           },
           "id": {
              "type": "string"
           },
           "pubyear": {
              "type": "long"
           },
           "rawsource": {
              "type": "string"
           },
           "source": {
              "type": "string"
           },
           "title": {
              "type": "string",
              "analyzer": "def_default_analyzer",
              "fields": {
                 "ngrams": {
                    "type": "string",
                    "index_analyzer": "def_ngram_analyzer",
                    "search_analyzer": "def_search_analyzer"
                 },
                 "shingles": {
                    "type": "string",
                    "analyzer": "def_shingle_analyzer"
                 },
                 "stemmed": {
                    "type": "string",
                    "analyzer": "def_snowball_analyzer"
                 }
              }
           },
           "topics": {
              "properties": {
                 "name": {
                    "type": "string",
                    "analyzer": "def_default_analyzer",
                    "fields": {
                       "ngrams": {
                          "type": "string",
                          "index_analyzer": "def_ngram_analyzer",
                          "search_analyzer": "def_search_analyzer"
                       },
                       "shingles": {
                          "type": "string",
                          "analyzer": "def_shingle_analyzer"
                       },
                       "stemmed": {
                          "type": "string",
                          "analyzer": "def_snowball_analyzer"
                       }
                    }
                 }
              }
           },
           "url": {
              "type": "string"
           }
        }
     }
  }

然后按预期使用以下工作查询索引:

curl -XGET "http://localhost:9200/_search  " -d'
{
   "size": 5,
   "query": {
      "multi_match": {
         "query": "physics",
         "type": "most_fields",
         "fields": [
             "document.title^10",
             "document.title.shingles^2",
             "document.title.ngrams",
             "person.name^10",
             "person.name.shingles^2",
             "person.name.ngrams",
             "document.topics.name^10",
             "document.topics.name.shingles^2",
             "document.topics.name.ngrams"
          ],
          "operator": "and"
      }
   }
}'

希望这会对某人有所帮助,这可能不是最好的例子,因为我是一个完整的菜鸟,但它对我有用。

答案 1 :(得分:-1)

Vaadin存在不同的自动完成组件。 请查看this链接。

根据您选择的附加组件,数据绑定的执行方式不同,但您必须将其“连接”到索引。

相关问题