Mailing List Archive

MultiFieldQueryParser on integer and string (8.6.0)
Hi,
I'm trying to index numeric, and then to query them using java api and
lucene 8.6. I tried several numeric Field types, but I can't make it
work.
Can someone help me to store numeric in the datastore and then to
query them ?
ThanksSt?phane

Here is a simple JUnit test
package test.data;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collection;

@RunWith(Parameterized.class)
public class MTest {
static Directory index = new RAMDirectory();
private String query;

public MTest(String query) {
this.query = query;
}

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
Object[][]
scannedClasses =
new Object[][]{{"TextField:Client"},
{"LongPoint:17"},
{"LongPoint:[16 TO 18]"},
{"BigIntegerPoint:21"},
{"BigIntegerPoint:[20 TO 22]"},
{"StringField:Stephane"},
{"Date:20200615"},
{"Date:[20200614 TO 20200616]"},
{"DoublePoint:37"},
{"DoublePoint:[37 TO 38]"},};
return Arrays.asList(scannedClasses);
}

@BeforeClass
public static void init() throws IOException, ParseException {
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
try (IndexWriter writer = new IndexWriter(index, config)) {
final Document doc = new Document();
doc.add(new TextField("TextField", "Client",
Field.Store.YES));
doc.add(new StringField("StringField", "Stephane",
Field.Store.YES));
doc.add(new LongPoint("LongPoint", 17));
doc.add(new BigIntegerPoint("BigIntegerPoint", new
BigInteger("21")));
doc.add(new DoublePoint("DoublePoint", new Double(37.7)));
doc.add(new StringField("Date", "20200615",
Field.Store.YES));
writer.addDocument(doc);
writer.commit();
}
}

private void query(Directory index, QueryParser
multiFieldQueryParser, String query) throws ParseException,
IOException {
System.out.println("query = " + query);
Query q = multiFieldQueryParser.parse(query);
int hitsPerPage = 10;
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs docs = searcher.search(q, hitsPerPage);
ScoreDoc[] hits = docs.scoreDocs;
Assert.assertEquals(query, 1, hits.length);
}

@Test
public void testString() throws IOException, ParseException {
StandardAnalyzer analyzer = new StandardAnalyzer();
final QueryParser multiFieldQueryParser = new
MultiFieldQueryParser(new String[]{"longPoint", "Entity"}, analyzer);
query(index, multiFieldQueryParser, query);
}
}
Re: MultiFieldQueryParser on integer and string (8.6.0) [ In reply to ]
Hi,

it seems I do not raise a lot of interest here... anyway I try again
with a simpler question.

Is MultiFieldQueryParser usable in 8.6.0 ?

thanks

-------- Message initial --------
De: Stephane Passignat <passignat@hotmail.com>
R?pondre ?: java-user@lucene.apache.org
?: java-user@lucene.apache.org
Objet: MultiFieldQueryParser on integer and string (8.6.0)
Date: Thu, 08 Oct 2020 16:48:40 +0200

Hi,I'm trying to index numeric, and then to query them using java api
andlucene 8.6. I tried several numeric Field types, but I can't make
itwork.Can someone help me to store numeric in the datastore and then
toquery them ?ThanksSt?phane
Here is a simple JUnit testpackage test.data;import
org.apache.lucene.analysis.standard.StandardAnalyzer;import
org.apache.lucene.document.*;import
org.apache.lucene.index.DirectoryReader;import
org.apache.lucene.index.IndexReader;import
org.apache.lucene.index.IndexWriter;import
org.apache.lucene.index.IndexWriterConfig;import
org.apache.lucene.queryparser.classic.MultiFieldQueryParser;import
org.apache.lucene.queryparser.classic.ParseException;import
org.apache.lucene.queryparser.classic.QueryParser;import
org.apache.lucene.search.IndexSearcher;import
org.apache.lucene.search.Query;import
org.apache.lucene.search.ScoreDoc;import
org.apache.lucene.search.TopDocs;import
org.apache.lucene.store.Directory;import
org.apache.lucene.store.RAMDirectory;import org.junit.Assert;import
org.junit.BeforeClass;import org.junit.Test;import
org.junit.runner.RunWith;import org.junit.runners.Parameterized;
import java.io.IOException;import java.math.BigInteger;import
java.util.Arrays;import java.util.Collection;
@RunWith(Parameterized.class)public class MTest { static Directory
index = new RAMDirectory(); private String query;
public MTest(String query) { this.query = query; }
@Parameterized.Parameters(name = "{0}") public static
Collection<Object[]> data()
{ Object[][] scannedClasses = new
Object[][]{{"TextField:Client"}, {"LongPoint:17
"}, {"LongPoint:[16 TO
18]"}, {"BigIntegerPoint:21"},
{"BigIntegerPoint:[20 TO
22]"}, {"StringField:Stephane"},
{"Date:20200615"}, {"Date:[20200614 TO
20200616]"}, {"DoublePoint:37"},
{"DoublePoint:[37 TO 38]"},}; return
Arrays.asList(scannedClasses); }
@BeforeClass public static void init() throws IOException,
ParseException { StandardAnalyzer analyzer = new
StandardAnalyzer(); IndexWriterConfig config = new
IndexWriterConfig(analyzer); config.setOpenMode(IndexWriterConfig
.OpenMode.CREATE_OR_APPEND); try (IndexWriter writer = new
IndexWriter(index, config)) { final Document doc = new
Document(); doc.add(new TextField("TextField",
"Client",Field.Store.YES)); doc.add(new
StringField("StringField",
"Stephane",Field.Store.YES)); doc.add(new
LongPoint("LongPoint", 17)); doc.add(new
BigIntegerPoint("BigIntegerPoint",
newBigInteger("21"))); doc.add(new DoublePoint("DoublePoint",
new Double(37.7))); doc.add(new StringField("Date",
"20200615",Field.Store.YES)); writer.addDocument(doc);
writer.commit(); } }
private void query(Directory index,
QueryParsermultiFieldQueryParser, String query) throws
ParseException,IOException { System.out.println("query = " +
query); Query q = multiFieldQueryParser.parse(query); int
hitsPerPage = 10; IndexReader reader =
DirectoryReader.open(index); IndexSearcher searcher = new
IndexSearcher(reader); TopDocs docs = searcher.search(q,
hitsPerPage); ScoreDoc[] hits =
docs.scoreDocs; Assert.assertEquals(query, 1, hits.length); }
@Test public void testString() throws IOException, ParseException
{ StandardAnalyzer analyzer = new StandardAnalyzer(); final
QueryParser multiFieldQueryParser = newMultiFieldQueryParser(new
String[]{"longPoint", "Entity"}, analyzer); query(index,
multiFieldQueryParser, query); }}
Re: MultiFieldQueryParser on integer and string (8.6.0) [ In reply to ]
I think you will need to subclass MultiFieldQueryParser so that the
proper Query is created when the field is the numeric one. Maybe
overriding createFieldQuery().

El 8/10/20 a las 11:48, Stephane Passignat escribi?:
> Hi,
> I'm trying to index numeric, and then to query them using java api and
> lucene 8.6. I tried several numeric Field types, but I can't make it
> work.
> Can someone help me to store numeric in the datastore and then to
> query them ?
> ThanksSt?phane
>
> Here is a simple JUnit test
> package test.data;
> import org.apache.lucene.analysis.standard.StandardAnalyzer;
> import org.apache.lucene.document.*;
> import org.apache.lucene.index.DirectoryReader;
> import org.apache.lucene.index.IndexReader;
> import org.apache.lucene.index.IndexWriter;
> import org.apache.lucene.index.IndexWriterConfig;
> import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
> import org.apache.lucene.queryparser.classic.ParseException;
> import org.apache.lucene.queryparser.classic.QueryParser;
> import org.apache.lucene.search.IndexSearcher;
> import org.apache.lucene.search.Query;
> import org.apache.lucene.search.ScoreDoc;
> import org.apache.lucene.search.TopDocs;
> import org.apache.lucene.store.Directory;
> import org.apache.lucene.store.RAMDirectory;
> import org.junit.Assert;
> import org.junit.BeforeClass;
> import org.junit.Test;
> import org.junit.runner.RunWith;
> import org.junit.runners.Parameterized;
>
> import java.io.IOException;
> import java.math.BigInteger;
> import java.util.Arrays;
> import java.util.Collection;
>
> @RunWith(Parameterized.class)
> public class MTest {
> static Directory index = new RAMDirectory();
> private String query;
>
> public MTest(String query) {
> this.query = query;
> }
>
> @Parameterized.Parameters(name = "{0}")
> public static Collection<Object[]> data() {
> Object[][]
> scannedClasses =
> new Object[][]{{"TextField:Client"},
> {"LongPoint:17"},
> {"LongPoint:[16 TO 18]"},
> {"BigIntegerPoint:21"},
> {"BigIntegerPoint:[20 TO 22]"},
> {"StringField:Stephane"},
> {"Date:20200615"},
> {"Date:[20200614 TO 20200616]"},
> {"DoublePoint:37"},
> {"DoublePoint:[37 TO 38]"},};
> return Arrays.asList(scannedClasses);
> }
>
> @BeforeClass
> public static void init() throws IOException, ParseException {
> StandardAnalyzer analyzer = new StandardAnalyzer();
> IndexWriterConfig config = new IndexWriterConfig(analyzer);
> config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
> try (IndexWriter writer = new IndexWriter(index, config)) {
> final Document doc = new Document();
> doc.add(new TextField("TextField", "Client",
> Field.Store.YES));
> doc.add(new StringField("StringField", "Stephane",
> Field.Store.YES));
> doc.add(new LongPoint("LongPoint", 17));
> doc.add(new BigIntegerPoint("BigIntegerPoint", new
> BigInteger("21")));
> doc.add(new DoublePoint("DoublePoint", new Double(37.7)));
> doc.add(new StringField("Date", "20200615",
> Field.Store.YES));
> writer.addDocument(doc);
> writer.commit();
> }
> }
>
> private void query(Directory index, QueryParser
> multiFieldQueryParser, String query) throws ParseException,
> IOException {
> System.out.println("query = " + query);
> Query q = multiFieldQueryParser.parse(query);
> int hitsPerPage = 10;
> IndexReader reader = DirectoryReader.open(index);
> IndexSearcher searcher = new IndexSearcher(reader);
> TopDocs docs = searcher.search(q, hitsPerPage);
> ScoreDoc[] hits = docs.scoreDocs;
> Assert.assertEquals(query, 1, hits.length);
> }
>
> @Test
> public void testString() throws IOException, ParseException {
> StandardAnalyzer analyzer = new StandardAnalyzer();
> final QueryParser multiFieldQueryParser = new
> MultiFieldQueryParser(new String[]{"longPoint", "Entity"}, analyzer);
> query(index, multiFieldQueryParser, query);
> }
> }
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org
RE: MultiFieldQueryParser on integer and string (8.6.0) [ In reply to ]
For LongPoint field, you need to use methods that returns Query in LongPoint
class.




static
<https://lucene.apache.org/core/8_6_1/core/org/apache/lucene/search/Query.ht


<https://lucene.apache.org/core/8_6_1/core/org/apache/lucene/document/LongPo
int.html#newExactQuery-java.lang.String-long-> newExactQuery(
<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html?is-external
=true> String field, long value)

Create a query for matching an exact long value.


static
<https://lucene.apache.org/core/8_6_1/core/org/apache/lucene/search/Query.ht


<https://lucene.apache.org/core/8_6_1/core/org/apache/lucene/document/LongPo
int.html#newRangeQuery-java.lang.String-long:A-long:A-> newRangeQuery(
<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html?is-external
=true> String field, long[] lowerValue, long[] upperValue)

Create a range query for n-dimensional long values.


static
<https://lucene.apache.org/core/8_6_1/core/org/apache/lucene/search/Query.ht


<https://lucene.apache.org/core/8_6_1/core/org/apache/lucene/document/LongPo
int.html#newRangeQuery-java.lang.String-long-long-> newRangeQuery(
<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html?is-external
=true> String field, long lowerValue, long upperValue)

Create a range query for long values.


static
<https://lucene.apache.org/core/8_6_1/core/org/apache/lucene/search/Query.ht


<https://lucene.apache.org/core/8_6_1/core/org/apache/lucene/document/LongPo
int.html#newSetQuery-java.lang.String-java.util.Collection-> newSetQuery(
<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html?is-external
=true> String field,
<https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html?is-exte
rnal=true> Collection<
<https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html?is-external=t

Create a query matching any of the specified 1D values.


static
<https://lucene.apache.org/core/8_6_1/core/org/apache/lucene/search/Query.ht


<https://lucene.apache.org/core/8_6_1/core/org/apache/lucene/document/LongPo
int.html#newSetQuery-java.lang.String-long...-> newSetQuery(
<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html?is-external
=true> String field, long... values)

Create a query matching any of the specified 1D values.



Regards,

Karthick



-----Original Message-----
From: Nicol?s Lichtmaier [mailto:nicolasl@wolfram.com.INVALID]
Sent: Tuesday, November 24, 2020 1:25 PM
To: java-user@lucene.apache.org; Stephane Passignat <passignat@hotmail.com>
Subject: Re: MultiFieldQueryParser on integer and string (8.6.0)



I think you will need to subclass MultiFieldQueryParser so that the proper
Query is created when the field is the numeric one. Maybe overriding
createFieldQuery().



El 8/10/20 a las 11:48, Stephane Passignat escribi?:

> Hi,

> I'm trying to index numeric, and then to query them using java api and

> lucene 8.6. I tried several numeric Field types, but I can't make it

> work.

> Can someone help me to store numeric in the datastore and then to

> query them ?

> ThanksSt?phane

>

> Here is a simple JUnit test

> package test.data;

> import org.apache.lucene.analysis.standard.StandardAnalyzer;

> import org.apache.lucene.document.*;

> import org.apache.lucene.index.DirectoryReader;

> import org.apache.lucene.index.IndexReader;

> import org.apache.lucene.index.IndexWriter;

> import org.apache.lucene.index.IndexWriterConfig;

> import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;

> import org.apache.lucene.queryparser.classic.ParseException;

> import org.apache.lucene.queryparser.classic.QueryParser;

> import org.apache.lucene.search.IndexSearcher;

> import org.apache.lucene.search.Query; import

> org.apache.lucene.search.ScoreDoc;

> import org.apache.lucene.search.TopDocs; import

> org.apache.lucene.store.Directory;

> import org.apache.lucene.store.RAMDirectory;

> import org.junit.Assert;

> import org.junit.BeforeClass;

> import org.junit.Test;

> import org.junit.runner.RunWith;

> import org.junit.runners.Parameterized;

>

> import java.io.IOException;

> import java.math.BigInteger;

> import java.util.Arrays;

> import java.util.Collection;

>

> @RunWith(Parameterized.class)

> public class MTest {

> static Directory index = new RAMDirectory();

> private String query;

>

> public MTest(String query) {

> this.query = query;

> }

>

> @Parameterized.Parameters(name = "{0}")

> public static Collection<Object[]> data() {

> Object[][]

> scannedClasses =

> new Object[][]{{"TextField:Client"},

> {"LongPoint:17"},

> {"LongPoint:[16 TO 18]"},

> {"BigIntegerPoint:21"},

> {"BigIntegerPoint:[20 TO 22]"},

> {"StringField:Stephane"},

> {"Date:20200615"},

> {"Date:[20200614 TO 20200616]"},

> {"DoublePoint:37"},

> {"DoublePoint:[37 TO 38]"},};

> return Arrays.asList(scannedClasses);

> }

>

> @BeforeClass

> public static void init() throws IOException, ParseException {

> StandardAnalyzer analyzer = new StandardAnalyzer();

> IndexWriterConfig config = new IndexWriterConfig(analyzer);

> config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

> try (IndexWriter writer = new IndexWriter(index, config)) {

> final Document doc = new Document();

> doc.add(new TextField("TextField", "Client",

> Field.Store.YES));

> doc.add(new StringField("StringField", "Stephane",

> Field.Store.YES));

> doc.add(new LongPoint("LongPoint", 17));

> doc.add(new BigIntegerPoint("BigIntegerPoint", new

> BigInteger("21")));

> doc.add(new DoublePoint("DoublePoint", new Double(37.7)));

> doc.add(new StringField("Date", "20200615",

> Field.Store.YES));

> writer.addDocument(doc);

> writer.commit();

> }

> }

>

> private void query(Directory index, QueryParser

> multiFieldQueryParser, String query) throws ParseException,

> IOException {

> System.out.println("query = " + query);

> Query q = multiFieldQueryParser.parse(query);

> int hitsPerPage = 10;

> IndexReader reader = DirectoryReader.open(index);

> IndexSearcher searcher = new IndexSearcher(reader);

> TopDocs docs = searcher.search(q, hitsPerPage);

> ScoreDoc[] hits = docs.scoreDocs;

> Assert.assertEquals(query, 1, hits.length);

> }

>

> @Test

> public void testString() throws IOException, ParseException {

> StandardAnalyzer analyzer = new StandardAnalyzer();

> final QueryParser multiFieldQueryParser = new

> MultiFieldQueryParser(new String[]{"longPoint", "Entity"}, analyzer);

> query(index, multiFieldQueryParser, query);

> }

> }

>

>



---------------------------------------------------------------------

To unsubscribe, e-mail: <mailto:java-user-unsubscribe@lucene.apache.org>
java-user-unsubscribe@lucene.apache.org

For additional commands, e-mail: <mailto:java-user-help@lucene.apache.org>
java-user-help@lucene.apache.org




--
Learn more
<https://www.trigent.com/lib/signature/test-automation/?utm_source=trigent-sign&utm_medium=email&utm_campaign=automate>
about our Test Automation Service.

<https://www.trigent.com/lib/signature/test-automation/?utm_source=trigent-sign&utm_medium=email&utm_campaign=automate>