Skip to content

Add type to compound word - #16680

Open
renatoh wants to merge 8 commits into
apache:mainfrom
renatoh:add-type-to-compound-word
Open

renatoh wants to merge 8 commits into
apache:mainfrom
renatoh:add-type-to-compound-word

Conversation

@renatoh

@renatoh renatoh commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Hi @uschindler
Is there a reason not to have a specific type on the compound words, so that I could inject a payload base on the type and make sure the extracted compound does get a lower boost than the original term?

Renato Haeberli and others added 3 commits May 18, 2026 16:21
# Conflicts:
#	lucene/analysis/common/src/java/org/apache/lucene/analysis/compound/CompoundWordTokenFilterBase.java
@uschindler

Copy link
Copy Markdown
Contributor

Hi,
I think this is a good addition. I am not sure if the type name "compound" is correct, as this is more about the parts of the compound. The compound itsself inherits the type of the original term (e.g., from the tokenizer).

As a general question: Do we also add separate types to synonyms or other generated tokens at same position at the moment?

@renatoh

renatoh commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

I think you are right, compound is the entire thing, maybe something like 'subword', let me know if you come up with something better.
Yes, we do that for the synonyms, but it is kind of broken in the SynonymGraphFilter, since all the tokens its emits, even the original, have the type 'synonyms', so it is kind of useless.

@rmuir

rmuir commented Sep 17, 2026

Copy link
Copy Markdown
Member

I think TypeAttribute is an antipattern for cases like this. You can only set single value for the type and so it is no better than the old Token days.

Maybe consider SubwordAttribute which wraps a boolean, or something like that.

@renatoh

renatoh commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

@rmuir you are suggesting to create a brand new boolean attribute? I see you point, but with a custom attribute we could not use a generic filter like NumericPayloadTokenFilter to inject a payload and as far as I understand, it would require custom code to do so.
At the end, I think we should have a way to boost/demote subwords, compare to the original token, same applies for synonyms.

@uschindler

Copy link
Copy Markdown
Contributor

I think both of you have good opinions. The problem of the type attribute is that it only allows one type. In current code of analyzers the original type is copied over.

As you see in the test, the tokenizer adds "word" as type (maybe different for other tokenizers, e.g. Japanese). The old code cloned all attributes, so the sub words also get "word" as type.

The good idea here is that additional (generated tokens) get a new type assigned, e.g "subword". That helps with generic filters added later like those for payloads. It is not ideal, but it helps to classify later token additions.

So in my opinion, this is fine with type attribute.

@rmuir

rmuir commented Sep 17, 2026

Copy link
Copy Markdown
Member

I'm fine with subword as a type, really. But there is a limitation that you can't have a type of both synonym and subword.

@uschindler

Copy link
Copy Markdown
Contributor

I'm fine with subword as a type, really. But there is a limitation that you can't have a type of both synonym and subword.

Yes, if you expand synonyms and decompound then this hits you. But your filter chain could handle that. After synonyms you can add a payload filter that would trigger on synonyms and then later add decompounding with another payload filter.

The whole token filter chain is problematic with that type of combinations. The whole type concept is feeling like Doug Cuttings original Token class. 😝

@uschindler

Copy link
Copy Markdown
Contributor

Can you add a changes entry. I think Lucene 10.6 should be fine as it's easy to backport

@uschindler uschindler self-assigned this Sep 18, 2026
@uschindler uschindler added this to the 10.6.0 milestone Sep 18, 2026
@renatoh

renatoh commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

@uschindler something like this?

@renatoh

renatoh commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

@rmuir SynonymGraphFilter emits all the token, even the original one, as type:Synonym, this seems broken to me. Do you have an idea why this is like this? I have observed this in Lucene 9.11

Comment thread lucene/CHANGES.txt Outdated
New Features
---------------------

* GITHUB#16680: Add a "subwords" type to the subword tokens generated by

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not plural, otherwise fine.

/** Base class for decomposition token filters. */
public abstract class CompoundWordTokenFilterBase extends TokenFilter {
/** Type assigned to subword tokens generated by {@link #decompose()}. */
public static final String TOKEN_TYPE_COMPOUND = "subword";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constant should be named TOKEN_TYPE_SUBWORD

new String[] {
"word",
CompoundWordTokenFilterBase.TOKEN_TYPE_SUBWORD,
CompoundWordTokenFilterBase.TOKEN_TYPE_SUBWORD

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a conception to substituting constant to tests that let to detect a change in constant value which may break a client otherwise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, thanks for your input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants