org.elasticsearch.common.unit.Fuzziness Java Examples
The following examples show how to use
org.elasticsearch.common.unit.Fuzziness.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueryHelper.java From fess with Apache License 2.0 | 6 votes |
protected QueryBuilder convertFuzzyQuery(final QueryContext context, final FuzzyQuery fuzzyQuery, final float boost) { final Term term = fuzzyQuery.getTerm(); final String field = getSearchField(context, term.field()); // TODO fuzzy value if (Constants.DEFAULT_FIELD.equals(field)) { context.addFieldLog(field, term.text()); return buildDefaultQueryBuilder((f, b) -> QueryBuilders.fuzzyQuery(f, term.text()) .fuzziness(Fuzziness.fromEdits(fuzzyQuery.getMaxEdits())).boost(b * boost)); } else if (isSearchField(field)) { context.addFieldLog(field, term.text()); return QueryBuilders.fuzzyQuery(field, term.text()).boost(boost).fuzziness(Fuzziness.fromEdits(fuzzyQuery.getMaxEdits())); } else { final String origQuery = fuzzyQuery.toString(); context.addFieldLog(Constants.DEFAULT_FIELD, origQuery); context.addHighlightedQuery(origQuery); return buildDefaultQueryBuilder((f, b) -> QueryBuilders.fuzzyQuery(f, origQuery) .fuzziness(Fuzziness.fromEdits(fuzzyQuery.getMaxEdits())).boost(b * boost)); } }
Example #2
Source File: CompletionSuggestionFuzzyBuilder.java From Elasticsearch with Apache License 2.0 | 6 votes |
@Override protected XContentBuilder innerToXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { builder.startObject("fuzzy"); if (fuzziness != Fuzziness.ONE) { fuzziness.toXContent(builder, params); } if (fuzzyTranspositions != XFuzzySuggester.DEFAULT_TRANSPOSITIONS) { builder.field("transpositions", fuzzyTranspositions); } if (fuzzyMinLength != XFuzzySuggester.DEFAULT_MIN_FUZZY_LENGTH) { builder.field("min_length", fuzzyMinLength); } if (fuzzyPrefixLength != XFuzzySuggester.DEFAULT_NON_FUZZY_PREFIX) { builder.field("prefix_length", fuzzyPrefixLength); } if (unicodeAware != XFuzzySuggester.DEFAULT_UNICODE_AWARE) { builder.field("unicode_aware", unicodeAware); } builder.endObject(); return builder; }
Example #3
Source File: MatchQueryDemo.java From elasticsearch-full with Apache License 2.0 | 6 votes |
@Test public void testForClient() throws Exception { QueryBuilder qb = QueryBuilders.matchQuery("title","quack dog") .boost(100.00f) .fuzziness(Fuzziness.ONE) .prefixLength(0) // .operator(Operator.AND) ; SearchResponse response = client.prepareSearch() .setIndices("my_index") .setTypes("my_type") .setQuery(qb) .execute() .actionGet(); println(response); }
Example #4
Source File: DateFieldMapper.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { long iValue = parseValue(value); long iSim; try { iSim = fuzziness.asTimeValue().millis(); } catch (Exception e) { // not a time format iSim = fuzziness.asLong(); } return NumericRangeQuery.newLongRange(names().indexName(), numericPrecisionStep(), iValue - iSim, iValue + iSim, true, true); }
Example #5
Source File: FuzzySearchTest.java From Spring-Boot-Book with Apache License 2.0 | 5 votes |
@Test /** * Description: 分词模糊查询。 */ public void fuzzyQuery() { // 查询条件 NativeSearchQueryBuilder nativeSearchQueryBuilderQueryBuilder = new NativeSearchQueryBuilder(); nativeSearchQueryBuilderQueryBuilder.withQuery(QueryBuilders.fuzzyQuery("name","士").fuzziness(Fuzziness.ONE)); // 搜索,获取结果 Page<Product> products= productRepository.search(nativeSearchQueryBuilderQueryBuilder.build()); for (Product product : products) { System.out.println(product); } }
Example #6
Source File: ShortFieldMapper.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { short iValue = parseValue(value); short iSim = fuzziness.asShort(); return NumericRangeQuery.newIntRange(names().indexName(), numericPrecisionStep(), iValue - iSim, iValue + iSim, true, true); }
Example #7
Source File: FloatFieldMapper.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { float iValue = parseValue(value); final float iSim = fuzziness.asFloat(); return NumericRangeQuery.newFloatRange(names().indexName(), numericPrecisionStep(), iValue - iSim, iValue + iSim, true, true); }
Example #8
Source File: DoubleFieldMapper.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { double iValue = parseDoubleValue(value); double iSim = fuzziness.asDouble(); return NumericRangeQuery.newDoubleRange(names().indexName(), numericPrecisionStep(), iValue - iSim, iValue + iSim, true, true); }
Example #9
Source File: IntegerFieldMapper.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { int iValue = parseValue(value); int iSim = fuzziness.asInt(); return NumericRangeQuery.newIntRange(names().indexName(), numericPrecisionStep(), iValue - iSim, iValue + iSim, true, true); }
Example #10
Source File: ByteFieldMapper.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { byte iValue = parseValue(value); byte iSim = fuzziness.asByte(); return NumericRangeQuery.newIntRange(names().indexName(), numericPrecisionStep(), iValue - iSim, iValue + iSim, true, true); }
Example #11
Source File: LongFieldMapper.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { long iValue = parseLongValue(value); final long iSim = fuzziness.asLong(); return NumericRangeQuery.newLongRange(names().indexName(), numericPrecisionStep(), iValue - iSim, iValue + iSim, true, true); }
Example #12
Source File: MultiMatchQueryDemo.java From elasticsearch-full with Apache License 2.0 | 5 votes |
@Test public void testForClient() throws Exception { /** * @see <a href='https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-multi-match-query.html'></a> * MultiMatchQuery依赖于match query ,也就是其核心是基于MatchQuery构建的 * */ MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("elasticsearch match query","title", "descrption"); multiMatchQueryBuilder.analyzer("standard"); multiMatchQueryBuilder.cutoffFrequency(0.001f); multiMatchQueryBuilder.field("title",20); multiMatchQueryBuilder.fuzziness(Fuzziness.TWO); multiMatchQueryBuilder.maxExpansions(100); multiMatchQueryBuilder.prefixLength(10); multiMatchQueryBuilder.tieBreaker(20); multiMatchQueryBuilder.type(MultiMatchQueryBuilder.Type.BEST_FIELDS); multiMatchQueryBuilder.boost(20); SearchResponse searchResponse = client.prepareSearch() .setIndices("blogs") .setTypes("blog") .setQuery(multiMatchQueryBuilder) .execute() .actionGet(); }
Example #13
Source File: FuzzyQueryDemo.java From elasticsearch-full with Apache License 2.0 | 5 votes |
@Test public void testForClient() throws Exception { /** * fuzzyQuery基于编辑距离(Levenshtein)来进行相似搜索,比如搜索kimzhy,可以搜索出kinzhy(编辑距离为1) * 为了进行测试说明,前创建一个索引,插入几条数据ka,kab,kib,ba,我们的搜索源为ki * 了解更多关于编辑距离(Levenshtein)的概念,请参考:<a href='http://www.cnblogs.com/biyeymyhjob/archive/2012/09/28/2707343.html'></a> * 了解更多编辑距离的算法,请参考:<a href='http://blog.csdn.net/ironrabbit/article/details/18736185'></a> * ki — ka 编辑距离为1 * ki — kab 编辑距离为2 * ki — kbia 编辑距离为3 * ki — kib 编辑距离为1 * 所以当我们设置编辑距离(ES中使用fuzziness参数来控制)为0的时候,没有结果 * 所以当我们设置编辑距离(ES中使用fuzziness参数来控制)为1的时候,会出现结果ka,kib * 所以当我们设置编辑距离(ES中使用fuzziness参数来控制)为2的时候,会出现结果ka,kib,kab * 所以当我们设置编辑距离(ES中使用fuzziness参数来控制)为3的时候,会出现结果ka,kib,kab,kbaa(很遗憾,ES本身最多只支持到2,因此不会出现此结果) */ QueryBuilder qb = QueryBuilders.fuzzyQuery("username","ki") // .fuzziness(Fuzziness.ZERO); 没有结果 // .fuzziness(Fuzziness.ONE); 会出现结果ka,kib // .fuzziness(Fuzziness.TWO);会出现结果ka,kib,kab .fuzziness(Fuzziness.AUTO) ////会出现结果ka,kib,kab,但是这里以java-api的方式好像不太好使,原因未定 .prefixLength(0) .boost(1) .maxExpansions(100); SearchResponse response = client.prepareSearch() .setIndices("index") .setTypes("type") .setQuery(qb) .execute() .actionGet(); println(response); }
Example #14
Source File: EsSearch.java From es-service-parent with Apache License 2.0 | 5 votes |
/** * 无奈es的版本太低了,无法在建议器中做过滤操作(笔者es版本1.1),在更高版本的es中可以通过Context Suggester做过滤 * * @param indices * @param suggestQuery * @return */ @SuppressWarnings({ "unchecked" }) private static List<Map<String, Object>> getCompletionSuggest(String indices, SuggestQuery suggestQuery) { // 查询体 CompletionSuggestionFuzzyBuilder suggestionsBuilder = new CompletionSuggestionFuzzyBuilder( suggestQuery.suggestName); suggestionsBuilder.setFuzziness(Fuzziness.build(suggestQuery.getFuzziness())); suggestionsBuilder.text(suggestQuery.getText()); suggestionsBuilder.field(suggestQuery.getField()); suggestionsBuilder.size(suggestQuery.getSize()); SuggestRequestBuilder suggestRequestBuilder = ESClient.getClient() .prepareSuggest(indices.split(",")).addSuggestion(suggestionsBuilder); SuggestResponse resp = suggestRequestBuilder.execute().actionGet(); // 查询结果 List<? extends Entry<? extends Option>> entries = (List<? extends Entry<? extends Option>>) resp .getSuggest().getSuggestion(suggestQuery.suggestName).getEntries(); if (entries == null) { return Lists.newArrayList(); } List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); for (Entry<? extends Option> e : entries) { for (Option option : e) { Map<String, Object> map_payload = option.getPayloadAsMap(); map_payload.put("NAME", option.getText().toString()); result.add(map_payload); } } // 调试模式 if (Constants.isDebug) { logger.info(suggestRequestBuilder.toString()); logger.info(suggestionsBuilder.toString()); logger.info(JsonUtil.toJson(result)); } return result; }
Example #15
Source File: ElasticSearchQueryManualTest.java From tutorials with MIT License | 5 votes |
@Test public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "spring date elasticserch").operator(Operator.AND) .fuzziness(Fuzziness.ONE) .prefixLength(3)) .build(); final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); assertEquals(1, articles.getTotalHits()); }
Example #16
Source File: StringFieldType.java From crate with Apache License 2.0 | 5 votes |
@Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { failIfNotIndexed(); return new FuzzyQuery(new Term(name(), indexedValueForSearch(value)), fuzziness.asDistance(BytesRefs.toString(value)), prefixLength, maxExpansions, transpositions); }
Example #17
Source File: ParsedOptions.java From crate with Apache License 2.0 | 5 votes |
public ParsedOptions(Float boost, String analyzer, MatchQuery.ZeroTermsQuery zeroTermsQuery, int maxExpansions, Fuzziness fuzziness, int prefixLength, boolean transpositions) { this.boost = boost; this.analyzer = analyzer; this.zeroTermsQuery = zeroTermsQuery; this.maxExpansions = maxExpansions; this.fuzziness = fuzziness; this.prefixLength = prefixLength; this.transpositions = transpositions; }
Example #18
Source File: IpFieldMapper.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { long iValue = parseValue(value); long iSim; try { iSim = ipToLong(fuzziness.asString()); } catch (IllegalArgumentException e) { iSim = fuzziness.asLong(); } return NumericRangeQuery.newLongRange(names().indexName(), numericPrecisionStep(), iValue - iSim, iValue + iSim, true, true); }
Example #19
Source File: FuzzySearchTest.java From Spring-Boot-Book with Apache License 2.0 | 5 votes |
@Test /** * Description: 分词模糊查询。 */ public void fuzzyQuery() { // 查询条件 NativeSearchQueryBuilder nativeSearchQueryBuilderQueryBuilder = new NativeSearchQueryBuilder(); nativeSearchQueryBuilderQueryBuilder.withQuery(QueryBuilders.fuzzyQuery("name","士").fuzziness(Fuzziness.ONE)); // 搜索,获取结果 Page<Product> products= productRepository.search(nativeSearchQueryBuilderQueryBuilder.build()); for (Product product : products) { System.out.println(product); } }
Example #20
Source File: MapperQueryParser.java From Elasticsearch with Apache License 2.0 | 5 votes |
private Query getFuzzyQuerySingle(String field, String termStr, String minSimilarity) throws ParseException { currentFieldType = parseContext.fieldMapper(field); if (currentFieldType != null) { try { return currentFieldType.fuzzyQuery(termStr, Fuzziness.build(minSimilarity), fuzzyPrefixLength, settings.fuzzyMaxExpansions(), FuzzyQuery.defaultTranspositions); } catch (RuntimeException e) { if (settings.lenient()) { return null; } throw e; } } return super.getFuzzyQuery(field, termStr, Float.parseFloat(minSimilarity)); }
Example #21
Source File: ParsedOptions.java From Elasticsearch with Apache License 2.0 | 5 votes |
public ParsedOptions(Float boost, String analyzer, MatchQuery.ZeroTermsQuery zeroTermsQuery, int maxExpansions, Fuzziness fuzziness, int prefixLength, boolean transpositions) { this.boost = boost; this.analyzer = analyzer; this.zeroTermsQuery = zeroTermsQuery; this.maxExpansions = maxExpansions; this.fuzziness = fuzziness; this.prefixLength = prefixLength; this.transpositions = transpositions; }
Example #22
Source File: ReferenceMapper.java From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 | 4 votes |
@Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { throw new UnsupportedOperationException(); }
Example #23
Source File: ParsedOptions.java From crate with Apache License 2.0 | 4 votes |
@Nullable public Fuzziness fuzziness() { return fuzziness; }
Example #24
Source File: MappedFieldType.java From crate with Apache License 2.0 | 4 votes |
public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { throw new IllegalArgumentException("Can only use fuzzy queries on keyword and text fields - not on [" + name + "] which is of type [" + typeName() + "]"); }
Example #25
Source File: ArrayFieldType.java From crate with Apache License 2.0 | 4 votes |
@Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { return innerFieldType.fuzzyQuery(value, fuzziness, prefixLength, maxExpansions, transpositions); }
Example #26
Source File: SearchService.java From openvsx with Eclipse Public License 2.0 | 4 votes |
public Page<ExtensionSearch> search(String queryString, String category, Pageable pageRequest, String sortOrder, String sortBy) { var queryBuilder = new NativeSearchQueryBuilder() .withIndices("extensions") .withPageable(pageRequest); if (!Strings.isNullOrEmpty(queryString)) { var boolQuery = QueryBuilders.boolQuery(); // Fuzzy matching of search query in multiple fields var multiMatchQuery = QueryBuilders.multiMatchQuery(queryString) .field("name").boost(5) .field("displayName").boost(5) .field("tags").boost(3) .field("namespace").boost(2) .field("description") .fuzziness(Fuzziness.AUTO) .prefixLength(2); boolQuery.should(multiMatchQuery).boost(5); // Prefix matching of search query in display name and namespace var prefixString = queryString.trim().toLowerCase(); var namePrefixQuery = QueryBuilders.prefixQuery("displayName", prefixString); boolQuery.should(namePrefixQuery).boost(2); var namespacePrefixQuery = QueryBuilders.prefixQuery("namespace", prefixString); boolQuery.should(namespacePrefixQuery); queryBuilder.withQuery(boolQuery); } if (!Strings.isNullOrEmpty(category)) { // Filter by selected category queryBuilder.withFilter(QueryBuilders.matchPhraseQuery("categories", category)); } if (!"asc".equalsIgnoreCase(sortOrder) && !"desc".equalsIgnoreCase(sortOrder)) { throw new ErrorResultException("sortOrder parameter must be either 'asc' or 'desc'."); } if ("relevance".equals(sortBy)) { queryBuilder.withSort(SortBuilders.scoreSort()); } if ("relevance".equals(sortBy) || "averageRating".equals(sortBy)) { queryBuilder.withSort( SortBuilders.fieldSort(sortBy).unmappedType("float").order(SortOrder.fromString(sortOrder))); } else if ("timestamp".equals(sortBy)) { queryBuilder.withSort( SortBuilders.fieldSort(sortBy).unmappedType("long").order(SortOrder.fromString(sortOrder))); } else if ("downloadCount".equals(sortBy)) { queryBuilder.withSort( SortBuilders.fieldSort(sortBy).unmappedType("integer").order(SortOrder.fromString(sortOrder))); } else { throw new ErrorResultException( "sortBy parameter must be 'relevance', 'timestamp', 'averageRating' or 'downloadCount'"); } try { rwLock.readLock().lock(); return searchOperations.queryForPage(queryBuilder.build(), ExtensionSearch.class); } finally { rwLock.readLock().unlock(); } }
Example #27
Source File: MatchQuery.java From crate with Apache License 2.0 | 4 votes |
public void setFuzziness(Fuzziness fuzziness) { this.fuzziness = fuzziness; }
Example #28
Source File: SearchServiceImpl.java From dk-fitting with Apache License 2.0 | 4 votes |
/** * 模糊查询,通配符查询,一个问号代表一个字符,*号代表通配 * @param hostIps ES集群的ip地址 * @param clusterName ES集群集群名称 * @param indexName ES集群的索引名称,可使用多个索引 indexName="test2,test1,test"; * @param typeName 索引类型,可多个 typeName="doc,pdf,test"; * @param port ES集群的端口号 * @param start 记录偏移 , null-默认为0 * @param size 记录偏移 , null-默认为10 * @param sentence 需要搜索的词语, * @return * @throws TException */ @Override public Map<String, String> FuzzyQuery(String hostIps, String clusterName, String indexName, String typeName, int port, int start, int size, String sentence, String field) throws TException { Client client=null; try { client = ESUtils.getClient( hostIps, port, clusterName ); } catch (Exception e) { e.printStackTrace(); } String[] index; if (indexName.contains( "," )){ index = indexName.split( "," ); }else { index = new String[]{indexName}; } String[] type; if (typeName.contains( "," )){ type = typeName.split( "," ); }else { type = new String[]{typeName}; } int start2 = (Integer)start == null ? 0 : start; int size2 = (Integer)size == null ? 10 : size; Map map=new LinkedHashMap( ); long totalHits = 0; QueryBuilder qb=null; if ((sentence.contains( "?" )||sentence.contains( "*" )||sentence.contains( "?" ))&&(field!=null&&!field.equals( " " ))) { if(sentence.contains( "?" )){ StringBuilder sb=new StringBuilder( ); sb.append( sentence.replaceAll( "?","?" ) ); sentence=sb.toString(); } qb = wildcardQuery( field, sentence ); }else if (field!=null&&!field.equals( " " )){ qb = fuzzyQuery( field, sentence).fuzziness(Fuzziness.TWO); } SearchResponse response = client.prepareSearch(index)//可以是多个index .setTypes(type)//可以是多个类型 .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)//精确查询 //.setQuery(QueryBuilders.matchAllQuery()) // Query 查询全部 .setQuery( qb ) .setFrom(start2).setSize(size2).setExplain(true)////设置是否按查询匹配度排序 .setTerminateAfter(1000)//达到1000提前结束 .get(); SearchHit[] searchHits = response.getHits().getHits(); String resp=null; for (SearchHit searchHit : searchHits) { resp = JSON.toJSONString( searchHit.getSource(), SerializerFeature.PrettyFormat ); totalHits++; map.put( String.valueOf(totalHits ),resp ); } long totalHits1 = response.getHits().totalHits; map.put( "count",String.valueOf(totalHits1 ) ); return map; }
Example #29
Source File: EsAbstractConditionQuery.java From fess with Apache License 2.0 | 4 votes |
protected MatchQueryBuilder regFuzzyQ(String name, Object value) { checkEsInvalidQuery(name, value); MatchQueryBuilder fuzzyQuery = QueryBuilders.matchQuery(name, value).fuzziness(Fuzziness.AUTO); regQ(fuzzyQuery); return fuzzyQuery; }
Example #30
Source File: EsAbstractConditionQuery.java From fess with Apache License 2.0 | 4 votes |
protected MatchQueryBuilder regFuzzyQ(String name, Object value) { checkEsInvalidQuery(name, value); MatchQueryBuilder fuzzyQuery = QueryBuilders.matchQuery(name, value).fuzziness(Fuzziness.AUTO); regQ(fuzzyQuery); return fuzzyQuery; }