hibernate查询不搜索超过2个字符的某些字符串,而不是在多字搜索上工作

问题描述 投票:0回答:1

我正在尝试通过hibernate搜索实现全文搜索功能。我们需要搜索姓名,地址等。用户可以搜索名称“John”,“Johm Murphy”,“Mark”,“Mark L Thomas”以及地址太像“20601 Blvd”,“first floor”等等。

虽然目前的逻辑适用于少数几个单词超过2个字符,如“约翰”是可搜索但不是“马克”,如果我说“马”然后我有结果,但如果我写马或马克,它不给任何记录。我也可以搜索城市哥伦比亚。

多字搜索也不起作用。

当我没有使用任何分析器(如下面的当前代码)时,上述语句是有效的,如果我使用的是edgengram,text,标准分析器,那么我的输出就不同了。但是没有一个分析仪工作。以下是完整代码:

我正在尝试检索数据的索引结构:

  > {
>         "_index" : "client_master_index_0300",
>         "_type" : "com.csc.pt.svc.data.to.Basclt0300TO",
>         "_id" : "518,1",
>         "_score" : 4.0615783,
>         "_source" : {
>           "id" : "518,1",
>           "cltseqnum" : 518,
>           "addrseqnum" : "1",
>           "addrln1" : "Dba",
>           "addrln2" : "Betsy Evans",
>           "city" : "SDA",
>           "state" : "SC",
>           "zipcode" : "89756-4531",
>           "country" : "USA",
>           "basclt0100to" : {
>             "cltseqnum" : 518,
>             "clientname" : "Betsy Evans",
>             "longname" : "Betsy Evans",
>             "id" : "518"
>           },
>           "basclt0900to" : {
>             "cltseqnum" : 518,
>             "id" : "518"
>           }
>         }
>       }

同一索引的索引定义:

    {
>   "client_master_index_0300" : {
>     "aliases" : { },
>     "mappings" : {
>       "com.csc.pt.svc.data.to.Basclt0300TO" : {
>         "dynamic" : "strict",
>         "properties" : {
>           "addrln1" : {
>             "type" : "text",
>             "store" : true
>           },
>           "addrln2" : {
>             "type" : "text",
>             "store" : true
>           },
>           "addrln3" : {
>             "type" : "text",
>             "store" : true
>           },
>           "addrseqnum" : {
>             "type" : "text",
>             "store" : true
>           },
>           "basclt0100to" : {
>             "properties" : {
>               "clientname" : {
>                 "type" : "text",
>                 "store" : true
>               },
>               "cltseqnum" : {
>                 "type" : "long",
>                 "store" : true
>               },
>               "firstname" : {
>                 "type" : "text",
>                 "store" : true
>               },
>               "id" : {
>                 "type" : "keyword",
>                 "store" : true,
>                 "norms" : true
>               },
>               "longname" : {
>                 "type" : "text",
>                 "store" : true
>               },
>               "midname" : {
>                 "type" : "text",
>                 "store" : true
>               }
>             }
>           },
>           "basclt0900to" : {
>             "properties" : {
>               "cltseqnum" : {
>                 "type" : "long",
>                 "store" : true
>               },
>               "email1" : {
>                 "type" : "text",
>                 "store" : true
>               },
>               "id" : {
>                 "type" : "keyword",
>                 "store" : true,
>                 "norms" : true
>               }
>             }
>           },
>           "city" : {
>             "type" : "text",
>             "store" : true
>           },
>           "cltseqnum" : {
>             "type" : "long",
>             "store" : true
>           },
>           "country" : {
>             "type" : "text",
>             "store" : true
>           },
>           "id" : {
>             "type" : "keyword",
>             "store" : true
>           },
>           "state" : {
>             "type" : "text",
>             "store" : true
>           },
>           "zipcode" : {
>             "type" : "text",
>             "store" : true
>           }
>         }
>       }
>     },
>     "settings" : {
>       "index" : {
>         "creation_date" : "1535607176216",
>         "number_of_shards" : "5",
>         "number_of_replicas" : "1",
>         "uuid" : "x4R71LNCTBSyO9Taf8siOw",
>         "version" : {
>           "created" : "6030299"
>         },
>         "provided_name" : "client_master_index_0300"
>       }
>     }
>   }
> }

包含字段的java对象:

    @Field(name = "longname", index = Index.YES, store = Store.YES,
            analyze = Analyze.YES)
    private String longname = "";

@Field(name = "firstname", index = Index.YES, store = Store.YES,
    analyze = Analyze.YES)
    private String firstname = "";

此外,目前我正在使用通配符上下文查询:

    public synchronized void searchClienData() {
   String lowerCasedSearchTerm = this.data.getSearchText().toLowerCase();

    SearchFactory searchFactory = fullTextSession.getSearchFactory();
    QueryBuilder buildQuery = searchFactory.buildQueryBuilder().forEntity(Basclt0300TO.class).get();

    String[] projections = {"basclt0100to.longname", "basclt0100to.cltseqnum", "addrln1", "addrln2", 
            "city","state","zipcode", "country","basclt0900to.email1" };

     Query query = queryBuilder.keyword()
    .onField("basclt0100to.longname").andField("addrln1").andField("addrln2")
    .andField("city").andField("state").andField("country").matching(lowerCasedSearchTerm)
    .createQuery();

    FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, Basclt0300TO.class);
    fullTextQuery.setMaxResults(this.data.getPageSize()).setFirstResult(this.data.getPageSize());

    List<String> projectedFields = new ArrayList<String>();
    for (String fieldName : projections)
            projectedFields.add(fieldName);

    @SuppressWarnings("unchecked")
    List<Cltj001ElasticSearchResponseTO> results = fullTextQuery.
    setProjection(projectedFields.toArray(new String[projectedFields.size()]))
    .setResultTransformer( new BasicTransformerAdapter() {
        private static final long serialVersionUID = 1L;
        @Override
        public Cltj001ElasticSearchResponseTO transformTuple(Object[] tuple, String[] aliases) {
            return   new Cltj001ElasticSearchResponseTO((String) tuple[0], (long) tuple[1],
                        (String) tuple[2], (String) tuple[3], (String) tuple[4],
                        (String) tuple[5],(String) tuple[6], (String) tuple[7], (String) tuple[8]);

        }
    })
    .getResultList();
    resultsClt0300MasterIndexList = results;

}
elasticsearch lucene hibernate-search
1个回答
0
投票

首先,您需要将分析仪定义实际分配给您的字段。仅仅定义分析仪是不够的。

@Field(name = "longname", index = Index.YES, store = Store.YES,
        analyze = Analyze.YES, analyzer = @Analyzer(definition = "theNameOfSomeAnalyzerDefinition"))
private String longname = "";

@Field(name = "firstname", index = Index.YES, store = Store.YES,
    analyze = Analyze.YES, analyzer = @Analyzer(definition = "theNameOfSomeAnalyzerDefinition"))
private String firstname = "";

然后,您需要选择一个策略并坚持下去:

  • 要么使用通配符查询,这些查询易于使用且不需要EdgeNGram令牌过滤器,但往往会导致problems due to the query terms not being analyzed
  • 或者您将EdgeNGram令牌过滤器应用于您的字段,并在查询时: 使用不带通配符选项的关键字查询 和override the analyzers使用不同的,它们应该与分配给您的字段的分析器具有相同的定义,除非它们不应使用EdgeNGram令牌过滤器。

但不要混淆这两种方法。决不。它不会起作用。

© www.soinside.com 2019 - 2024. All rights reserved.