The JSON object would be serialized and stored as it is, but I would like to index it in a way to enable me to search within that same field by key and value. The main idea is to search multiple values within the same Lucene Field.
Any suggestions on how to structure the indexing?
Lets imagine for example that I would like to search using following query:
A common "problem" is to think about indexes and documents as having a consistent set of fields. It is not the same as a relational database with tables of a fixed set of columns.
in a previous life I had an entity with a set of "attributes". A key/value collection (much like your grades).
Each document was created with fields named for each attribute ie "attr-thing" with the value added "NOT_ANALYZED".
So, in your example I'd create fields like
new Field("grade-"+gradeID, grade, Field.Store.NO, Field.Index.NOT_ANALYZED)
Then you can search with a query like "grade-DP01:excellent".
Alternatively you can just have a fixed field name (similar to @cris-almodovar) and set the value to something like "id=grade". Again NOT_ANALYZED. The search for "grade:DP01=excellent".
Either will work. I've used both approaches with success but typically prefer the first.
Additional in response to edit...
I think I understand the problem... If you had "scenery=industrial style=vintage" and "scenery=nature style=modern" you wouldn't want it to match if you searched "nature AND vintage", right?
You could add an "imageType" field for each set with a value like "scenery=industrial style=vintage abc=xyz" with the KeywordAnalyzer (just splits by space).
Then search with imageType:"scenery=industrial style=vintage"~2. Using a slop phrase guarantees that the values are in the same field and the slop allows for the order to be different or for there to be extra values. The number you'd have to figure out based on the number of properties you expect in each field. Simplistically, if you expect for there to be a max of N values then the slop should be N too.
Asked in February 2016Viewed 3,187 timesVoted 13Answered 2 times