There are two issues here.
- Non-empty fields are not overwritten unless
overwrite is explicitly specified. (Empty fields are fine, though, this'll come in handy soon.)
- You probably do not want to overwrite the existing keywords, instead you want to append
hit to the keywords. If you do that, you also need to add a comma.
To catch both the cases with an undefined and non-empty keyword field, use
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map[overwrite]{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldsource=keywords, match=\regexp{\A.+\Z}, final]
\step[fieldset=keywords, fieldvalue={,hit}, append]
}
\map{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldset=keywords, fieldvalue={hit}]
}
}
}
The first \map only matches non-empty keywords field and adds ,hit so hit does not meld into the last keyword as keywords = {muster, etwashit}. The second \map does not overwrite existing fields, so it only applies if no keyword field is defined. It simply makes the keywords field read hit.
MWE (This MWE uses filecontents. It therefore overwrites an existing .bib file with the same name as the .tex without advance warning. To be on the safe side test this in an empty folder.)
\documentclass{article}
\usepackage[backend=biber]{biblatex}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{key,
author = {Max Muster},
maintitle = {Der Werktitel},
title = {Titel des zweiten Bandes},
volume = {2},
location = {Ort},
year = {2002},
keywords = {muster, etwas},
}
@book{keya,
author = {Max Muster},
maintitle = {Der Werktitel},
title = {Titel des Dritten Bandes},
volume = {3},
location = {Ort},
year = {2002},
}
@book{other,
author = {Michael Karomann},
maintitle = {Etwas},
title = {Nichts},
volume = {3},
location = {Ort},
year = {2002},
keywords = {test, etwas},
}
\end{filecontents}
\addbibresource{\jobname.bib}
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map[overwrite]{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldsource=keywords, match=\regexp{\A.+\Z}, final]
\step[fieldset=keywords, fieldvalue={,hit}, append]
}
\map{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldset=keywords, fieldvalue={hit}]
}
}
}
\begin{document}
\nocite{*}
\printbibliography[keyword=hit]
\end{document}

Naturally
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map[overwrite]{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldset=keywords, fieldvalue={,hit}, append]
}
}
}
would have been shorter, but that leaves an "empty keyword" in the .bbl if the keywords field was empty (\keyw{,hit}). This should not cause problems, but I consider this bad form.
If you are one of those people who are into the perverse habit of leaving fields empty as in keywords = {}, you need one further \map
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map[overwrite]{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldsource=keywords, match=\regexp{\A.+\Z}, final]
\step[fieldset=keywords, fieldvalue={,hit}, append]
}
\map[overwrite]{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldsource=keywords, notmatch=\regexp{.+}, final]
\step[fieldset=keywords, fieldvalue={hit}]
}
\map{
\step[fieldsource=author, match=\regexp{Muster}, final]
\step[fieldset=keywords, fieldvalue={hit}]
}
}
}
finalin the source maps' steps? Even though I do not consider myself "uninitiated", the documentation on Source Maps eludes me to a large extent. And this is one of the pieces I lack. – gusbrs Mar 15 '18 at 14:31finalstops execution of the entire "parent"\map(i.e. the\mapthe\steplives in) if the conditions for the current\stepare not met. Otherwise the next step would be executed. Check out what happens in the MWE iffinalwere not set in\step[fieldsource=author, match=\regexp{Muster}]. In particularfinalaborts the\stepif afieldsourceis not defined or if nomatchis found. – moewe Mar 15 '18 at 14:40finalwas not present in\step[fieldsource=author, match=\regexp{Muster}]the keyword would be added to all entries. Is this correct? – gusbrs Mar 15 '18 at 14:47finalif you do sequential steps. But leaving awayfinalallows you to put several unrelated\steps into the same\map. – moewe Mar 15 '18 at 14:52final. If the MWE does not include a test case where the mapping should not apply, it is quite easy to miss. – moewe Mar 15 '18 at 15:01