【问题标题】:Should I map all my indexes with my analyzer on ElasticSearch我应该在 ElasticSearch 上用我的分析器映射我的所有索引吗
【发布时间】:2015-11-02 14:18:18
【问题描述】:

我“几乎”是 Elastic Search 的新手。我已经使用了一段时间,但之前从未使用过分析器。

我可以对我的项目进行全文搜索,但问题是,当我尝试查找像“Alex”这样的名称时,我应该完全正确地输入名称。它不适用于“Al”或“Ale”。它说“找不到匹配项”。

我从不同的站点找到了一些源代码,但这让我感到困惑。

我该怎么做:

1) 创建 nGram 分词器

2)然后将其与我的所有索引进行映射?

我已经创建了很多索引,但在对它们创建映射时出现错误。

我是否应该在开始索引我的记录之前创建我的分析器设置和映射?

我正在处理一个 Java 项目,因此非常感谢有关 JAVA API 的答案。

非常感谢!

【问题讨论】:

    标签: java elasticsearch full-text-search mappings


    【解决方案1】:

    应始终先创建映射,然后再索引数据。如果可能,请删除旧索引并使用新映射重新创建。如果您担心丢失数据,那么只需为现有索引创建一个新类型。新类型可以使用新映射。

    例如,这里有一段使用 Java API 创建自定义映射

    public class MappingCreator {
    
        static Logger log = Logger.getLogger(MappingCreator.class.getName());
    
        final static String indexName =  "indexName";
    
        final static String typeName = "typeName";
    
        final static String mappingFileName = "pathToMapping.jsonFile";
    
        final static String clusterName = "elasticsearch"; // or name of your cluster
    
        final static String hostName = "localhost";
    
        public static void main(String args[]) throws IOException
        {
    
            MappingCreator mapCreator = new MappingCreator();
    
            Client myESclient = getClient();
    
            IndicesExistsResponse res = myESclient.admin().indices().prepareExists(indexName).execute().actionGet();
    
            if (res.isExists()) {
    
                log.warn("Index "+indexName +" already exists. Will be deleted");
    
                final DeleteIndexRequestBuilder deleteIndexBuilder = myESclient.admin().indices().prepareDelete(indexName);
    
                deleteIndexBuilder.execute().actionGet();
            }
    
            final CreateIndexRequestBuilder createIndexBuilder = myESclient.admin().indices().prepareCreate(indexName)
                    .addMapping(typeName, mapCreator.getIndexFieldMapping());
    
            CreateIndexResponse createIndexResponse = createIndexBuilder.execute().actionGet();
    
            log.debug("Created mapping "+createIndexResponse.toString());
    
            myESclient.close();
    
        }
    
        private String getIndexFieldMapping() throws IOException {
    
            return IOUtils.toString(getClass().getClassLoader().getResourceAsStream(mappingFileName));
        }
    
        private static Client getClient() {
    
            TransportClient transportClient = null;
    
            try
            {
                Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName).build();
    
                transportClient = new TransportClient(settings);
    
                transportClient = transportClient.addTransportAddress(new InetSocketTransportAddress(hostName, 9300)); 
    
    /* be very careful about the port number here. by default its 9300. note that this is the TCP port which the java api will use. unlike the http port which is 9200 */
    
            }
            catch (Exception e)
            {
                log.error("Error in MappingCreator creating Elastic Search Client\n"
                        + "Message "+e.getMessage()+"\n"
                                + "StackTrace "+e.getStackTrace()
                        );
            }
    
            return (Client) transportClient;
    
        }
    
    }
    

    我希望这会有所帮助。顺便说一句,您制作自己的 nGram 标记器真的很酷。我很想看看它的代码以及它是如何完成的:)

    【讨论】:

      猜你喜欢
      • 2015-04-20
      • 1970-01-01
      • 2017-03-19
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多