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



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

可以在不更改索引中的50万个文档的情况下完成吗?我发现了一些教程,建议准备一个特定的映射,并修改文档(如果可能的话,我想避免这样做)等等,但我是新手,我不知道如何解决这个问题?以下是索引的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框架来完成(目前这并不相关,但最受欢迎的是JAVA/Vadin中的示例)。谢谢

所以,我认为我在Elasticsearch方面解决了我的问题,或者至少对我和手头的任务来说已经足够好了。我以这个红宝石为例。

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

在这种情况下,它们的关键是定义合适的分析仪和边缘NGram滤波器,如下所示:

"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"
      }
   }
}'

希望这能帮助到一些人,这可能不是最好的例子,因为我对此一无所知,但它对我有效。

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

根据您选择的加载项,数据绑定的方式不同,但您必须将其"连接"到索引。

最新更新