private Query CreateLuceneQuery(ChemicalStructure queryStructure, SearchType searchType)
        {
            BooleanQuery query = new BooleanQuery();

            switch (searchType)
            {
            // For exact searches, the search results MUST match the exact key.
            case SearchType.Exact:
                query.Add(new TermQuery(new Term(ChemicalStructureIndexer.FIELD_EXACTKEY, queryStructure.GetUniqueKey())), Occur.MUST);
                break;

            // For similarity searches, the search results SHOULD contain the query similarity fingerprint positions.
            case SearchType.Similarity:
                foreach (int fingerprintPosition in queryStructure.GetSimilarityFingerprintPositions())
                {
                    query.Add(new TermQuery(new Term(ChemicalStructureIndexer.FIELD_FINGERPRINT_POSITION_SIMILARITY, fingerprintPosition.ToString())), Occur.SHOULD);
                }
                break;

            // For substructure searches, the search results MUST contain ALL of the query substructure fingerprint positions.
            case SearchType.Substructure:
                foreach (int fingerprintPosition in queryStructure.GetSubstructureFingerprintPositions())
                {
                    query.Add(new TermQuery(new Term(ChemicalStructureIndexer.FIELD_FINGERPRINT_POSITION_SUBSTRUCTURE, fingerprintPosition.ToString())), Occur.MUST);
                }
                break;
            }

            return(query);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a chemical structure to the index.
        /// </summary>
        /// <param name="chemicalStructure">The chemical structure to add to the index.</param>
        public void AddChemicalStructure(ChemicalStructure chemicalStructure)
        {
            // Create a new Lucene document for the chemical structure.
            Document doc = new Document();

            // Add stored fields for the chemical structure name and molfile; this will
            // allow us to retrieve them later.
            doc.Add(new Field(FIELD_NAME, chemicalStructure.Name, Field.Store.YES, Field.Index.NO));
            doc.Add(new Field(FIELD_MOLFILE, chemicalStructure.MolfileContents, Field.Store.YES, Field.Index.NO));

            // Add searchable fields for exact key and similarity/substructure fingerprints.
            doc.Add(new Field(FIELD_EXACTKEY, chemicalStructure.GetUniqueKey(), Field.Store.NO, Field.Index.NOT_ANALYZED));
            foreach (int fingerprintPosition in chemicalStructure.GetSimilarityFingerprintPositions())
            {
                doc.Add(new Field(FIELD_FINGERPRINT_POSITION_SIMILARITY, fingerprintPosition.ToString(), Field.Store.NO, Field.Index.NOT_ANALYZED));
            }
            foreach (int fingerprintPosition in chemicalStructure.GetSubstructureFingerprintPositions())
            {
                doc.Add(new Field(FIELD_FINGERPRINT_POSITION_SUBSTRUCTURE, fingerprintPosition.ToString(), Field.Store.NO, Field.Index.NOT_ANALYZED));
            }

            // Add the document to the index.
            this.IndexWriter.AddDocument(doc);
        }