0

I am building an application that searches a database using Lucene.net.The user decides the fields of the table that want to search on.Now here's the question.How do i implement the multiqueryparser so that it can dynamicly search the specific field that the user has selected.

Here's what i've done if i knew the fields :

document.Add(new Field("FullText",
                    string.Format("{0} {1} {2} {3}", row["firstName"], row["LastName"], row["Job"], row["ID"]), Field.Store.YES, Field.Index.ANALYZED));

Which is the string.format syntax so that i can search the fields that user has selected?If the user selected FirstName and LastName only how to i change the string.format so i can dynamicly search for the two specific fields?

My try:

for (int i = 0; i < Variables.selectedfieldsname.Count() ; i++)
                {
                    document.Add(new Field(Variables.selectedfieldsname[i], row[Variables.selectedfieldsname[i]].ToString(), Field.Store.YES, Field.Index.ANALYZED));
                    fieldNum += "{" + i + "} ";
                    fields += row[Variables.selectedfieldsname[i]].ToString() + " ";

                }
document.Add(new Field("FullText", string.Format(fieldNum, fields), Field.Store.YES, Field.Index.ANALYZED));

writer.AddDocument(document);
Nomik
  • 145
  • 2
  • 10
  • I'm confused. You are asking about searching and query parsing, but your code appears to be adding a document, rather than searching. Are you trying to add a document with user-defined fields? Or are you trying to construct a query? – femtoRgon Dec 02 '15 at 17:17
  • Yeah you are right, my question about parser wasnt that correct.What i mean is i want the first and the second part of 'string.Format' in my case 'fieldNum'. 'fields' to be dynamic and not prefixed like the first example. – Nomik Dec 02 '15 at 19:14

1 Answers1

0

I'd recommend you just construct your field text as you iterate through the for loop. Trying to construct a big, burly format string and then collect all the arguments to the String.Format dynamically is just over-complicating things. You can simply concatenate your string the old fashioned way.

string fieldText = "";
for (int i = 0; i < Variables.selectedfieldsname.Count() ; i++)
{
    fieldText += row[Variables.selectedfieldsname[i]].ToString() + " ";
}

document.Add(new Field("FullText", fieldText, Field.Store.YES, Field.Index.ANALYZED));

writer.AddDocument(document);

One note, if you are dealing with a long list of fields, you may need to use StringBuilder instead of concatenating. I've seen string concatenation in long loops become a serious performance bottleneck in similar situations in java. Usually not worth worrying about, but if the list is long and performance suffers, it might not hurt to bear it in mind.

femtoRgon
  • 32,893
  • 7
  • 60
  • 87